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

58 lines
1.8 KiB
Go

package adminkindboxreqhandler
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
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"
"net/http"
)
// Deliver godoc
// @Summary Admin deliver a kindboxreq
// @Tags KindBoxReq
// @Accept json
// @Produce json
// @Param id path int true "KindBoxReq ID"
// @Param Request body param.DeliverKindBoxReqRequest true "KindBoxReq deliver request details"
// @Success 200 {object} param.DeliverKindBoxReqResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerAdmin
// @Router /admin/kindboxreqs/deliver-kind-box-req/{id} [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)
}
if fieldErrors, err := h.adminKindBoxReqVld.ValidateDeliver(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
q := querier.GetQuerierFromContextOrNew(c.Request().Context()).Begin()
ctx := context.WithValue(c.Request().Context(), querier.QuerierContextKey, q)
resp, sErr := h.adminKindBoxReqSvc.Deliver(ctx, req)
if sErr != nil {
rErr := q.Rollback()
if rErr != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errmsg.ErrorMsgSomethingWentWrong)
}
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
cErr := q.Commit()
if cErr != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errmsg.ErrorMsgSomethingWentWrong)
}
return c.JSON(http.StatusOK, resp)
}