feat(delivery): getting benefactor kind box request by id

This commit is contained in:
Sasan 2024-02-19 17:15:55 +03:30 committed by Alireza Mokhtari Garakani
parent 0d9f98a2e0
commit 8521cdbf20
5 changed files with 66 additions and 0 deletions

View File

@ -1 +1,32 @@
package benefactorkindboxreqhandler package benefactorkindboxreqhandler
import (
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
func (h Handler) Get(c echo.Context) error {
var req param.KindBoxReqGetRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateGetRequest(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
resp, sErr := h.benefactorKindBoxReqSvc.Get(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusCreated, resp)
}

View File

@ -11,4 +11,6 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.POST("/", h.Add, middleware.Auth(h.authSvc, h.authConfig), r.POST("/", h.Add, middleware.Auth(h.authSvc, h.authConfig),
middleware.BenefactorAuthorization(entity.UserBenefactorRole)) middleware.BenefactorAuthorization(entity.UserBenefactorRole))
r.GET("/:id", h.Get)
} }

View File

@ -90,3 +90,17 @@ func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
return nil return nil
} }
func (d DB) GetKindBoxReqByID(ctx context.Context, kindBoxReqID, benefactorID uint) (entity.KindBoxReq, error) {
op := richerror.Op("mysqlkindboxreq.GetKindBoxReqByID")
row := d.conn.Conn().QueryRowContext(ctx,
"select kind_box_reqs.* from kind_box_reqs where kind_box_reqs.id = ? and kind_box_reqs.benefactor_id = ?", kindBoxReqID, benefactorID,
)
k, err := scanKindBoxReq(row)
if err != nil {
return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return k, nil
}

View File

@ -1 +1,19 @@
package benefactorkindboxreqservice package benefactorkindboxreqservice
import (
"context"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) {
const op = "userkindboxreqservice.Get"
kindBoxReq, err := s.repo.GetKindBoxReqByID(ctx, req.KindBoxReqID, req.BenefactorID)
if err != nil {
return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil
}

View File

@ -8,6 +8,7 @@ import (
type Repository interface { type Repository interface {
AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error)
GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint, benefactorID uint) (entity.KindBoxReq, error)
} }
type Service struct { type Service struct {