From 8521cdbf203a36fee36424db721a9269b801e373 Mon Sep 17 00:00:00 2001 From: Sasan Date: Mon, 19 Feb 2024 17:15:55 +0330 Subject: [PATCH] feat(delivery): getting benefactor kind box request by id --- .../benefactor/kind_box_req/get.go | 31 +++++++++++++++++++ .../benefactor/kind_box_req/route.go | 2 ++ repository/mysql/kind_box_req/kind_box_req.go | 14 +++++++++ service/benefactor/kind_box_req/get.go | 18 +++++++++++ service/benefactor/kind_box_req/service.go | 1 + 5 files changed, 66 insertions(+) diff --git a/delivery/http_server/benefactor/kind_box_req/get.go b/delivery/http_server/benefactor/kind_box_req/get.go index 9ea2099..7622514 100644 --- a/delivery/http_server/benefactor/kind_box_req/get.go +++ b/delivery/http_server/benefactor/kind_box_req/get.go @@ -1 +1,32 @@ 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) +} diff --git a/delivery/http_server/benefactor/kind_box_req/route.go b/delivery/http_server/benefactor/kind_box_req/route.go index a4d2b0e..8f1e789 100644 --- a/delivery/http_server/benefactor/kind_box_req/route.go +++ b/delivery/http_server/benefactor/kind_box_req/route.go @@ -11,4 +11,6 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.POST("/", h.Add, middleware.Auth(h.authSvc, h.authConfig), middleware.BenefactorAuthorization(entity.UserBenefactorRole)) + + r.GET("/:id", h.Get) } diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 823d3c4..a8cc358 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -90,3 +90,17 @@ func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { 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 +} diff --git a/service/benefactor/kind_box_req/get.go b/service/benefactor/kind_box_req/get.go index cf248cf..253fd88 100644 --- a/service/benefactor/kind_box_req/get.go +++ b/service/benefactor/kind_box_req/get.go @@ -1 +1,19 @@ 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 +} diff --git a/service/benefactor/kind_box_req/service.go b/service/benefactor/kind_box_req/service.go index 1b336f2..6d3a2a2 100644 --- a/service/benefactor/kind_box_req/service.go +++ b/service/benefactor/kind_box_req/service.go @@ -8,6 +8,7 @@ import ( type Repository interface { AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) + GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint, benefactorID uint) (entity.KindBoxReq, error) } type Service struct {