forked from ebhomengo/niki
1
0
Fork 0
niki/delivery/http_server/agent/kind_box_req/deliver.go

57 lines
1.7 KiB
Go
Raw Normal View History

2024-09-15 07:36:09 +00:00
package agentkindboxreqhandler
import (
"context"
2024-09-15 07:36:09 +00:00
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req"
"net/http"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql"
echo "github.com/labstack/echo/v4"
)
2024-05-15 07:38:39 +00:00
// Deliver godoc
2024-09-15 07:36:09 +00:00
// @Summary Agent deliver a kindboxreq
// @Tags Agents KindBoxReqs
2024-05-15 07:38:39 +00:00
// @Accept json
// @Produce json
// @Param id path int true "KindBoxReq ID"
// @Param Request body param.DeliverKindBoxReqRequest true "KindBoxReq deliver request details"
2024-05-15 07:38:39 +00:00
// @Success 200 {object} param.DeliverKindBoxReqResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerAdmin
2024-09-15 07:36:09 +00:00
// @Router /agents/kindboxreqs/{id}/deliver-kind-box-req [patch].
func (h Handler) Deliver(c echo.Context) error {
var req param.DeliverKindBoxReqRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
q := querier.GetQuerierFromContextOrNew(c.Request().Context()).Begin()
ctx := context.WithValue(c.Request().Context(), querier.QuerierContextKey, q)
2024-09-15 07:36:09 +00:00
resp, sErr := h.agentKindBoxReqSvc.Deliver(ctx, req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
if resp.FieldErrors != nil {
return c.JSON(code, echo.Map{
"message": msg,
"errors": resp.FieldErrors,
})
}
rErr := q.Rollback()
if rErr != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errmsg.ErrorMsgSomethingWentWrong)
}
return echo.NewHTTPError(code, msg)
}
cErr := q.Commit()
if cErr != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errmsg.ErrorMsgSomethingWentWrong)
}
return c.JSON(http.StatusOK, resp)
}