From 3b3815ccec5362bc9cb9d0e855f2f7e283e57b7f Mon Sep 17 00:00:00 2001 From: masoodk Date: Sat, 3 Feb 2024 04:56:27 +0000 Subject: [PATCH] :sparkles:feat(service): add get all kindboxreq by admin (#26) Reviewed-on: https://git.gocasts.ir/ebhomengo/niki/pulls/26 Reviewed-by: hossein Co-authored-by: masoodk Co-committed-by: masoodk --- .../http_server/admin/kind_box_req/get_all.go | 24 +++++++++++ .../http_server/admin/kind_box_req/route.go | 2 + repository/mysql/kind_box/kind_box.go | 1 + repository/mysql/kind_box_req/get_all.go | 42 +++++++++++++++++++ service/admin/kind_box_req/get_all.go | 19 +++++++++ service/admin/kind_box_req/service.go | 1 + validator/admin/kind_box_req/get_all.go | 5 +++ 7 files changed, 94 insertions(+) create mode 100644 repository/mysql/kind_box_req/get_all.go create mode 100644 validator/admin/kind_box_req/get_all.go diff --git a/delivery/http_server/admin/kind_box_req/get_all.go b/delivery/http_server/admin/kind_box_req/get_all.go index b5635e7..305705d 100644 --- a/delivery/http_server/admin/kind_box_req/get_all.go +++ b/delivery/http_server/admin/kind_box_req/get_all.go @@ -1 +1,25 @@ package adminkindboxreqhandler + +import ( + "net/http" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + echo "github.com/labstack/echo/v4" +) + +func (h Handler) GetAll(c echo.Context) error { + var req param.KindBoxReqGetAllRequest + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + resp, sErr := h.adminKindBoxReqSvc.GetAll(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/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index a751fea..c92ed27 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -1,6 +1,7 @@ package adminkindboxreqhandler import ( + "git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware" echo "github.com/labstack/echo/v4" ) @@ -10,4 +11,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { // todo - add acl r.PATCH("/accept-kind-box-req/:id", h.Accept) r.PATCH("/reject-kind-box-req/:id", h.Reject) + r.GET("/", h.GetAll, middleware.Auth(h.authSvc, h.authConfig)) } diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index 0672d18..d147645 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,6 +2,7 @@ package mysqlkindbox import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" diff --git a/repository/mysql/kind_box_req/get_all.go b/repository/mysql/kind_box_req/get_all.go new file mode 100644 index 0000000..cc5eefc --- /dev/null +++ b/repository/mysql/kind_box_req/get_all.go @@ -0,0 +1,42 @@ +package mysqlkindboxreq + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (d DB) GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) { + const op = "mysqlkindboxreq.GetAllKindBoxReq" + + // TODO - add sort and filter + rows, err := d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs") + if err != nil { + return nil, + richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected) + } + + defer rows.Close() + + // An album slice to hold data from returned rows. + var kindBoxReqs []entity.KindBoxReq + + // Loop through rows, using Scan to assign column data to struct fields. + for rows.Next() { + kindBoxReq, sErr := scanKindBoxReq(rows) + if sErr != nil { + return nil, richerror.New(op).WithErr(sErr). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + kindBoxReqs = append(kindBoxReqs, kindBoxReq) + } + + if rErr := rows.Err(); rErr != nil { + return nil, richerror.New(op).WithErr(rErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return kindBoxReqs, nil +} diff --git a/service/admin/kind_box_req/get_all.go b/service/admin/kind_box_req/get_all.go index 475ca43..a7d4061 100644 --- a/service/admin/kind_box_req/get_all.go +++ b/service/admin/kind_box_req/get_all.go @@ -1 +1,20 @@ package adminkindboxreqservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +// TODO: Pagination, Filters, Sort. +func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest) (param.KindBoxReqGetAllResponse, error) { + const op = "adminkindboxreqservice.GetAll" + + allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx) + if err != nil { + return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil +} diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index d419e49..b0e5eed 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -12,6 +12,7 @@ type Repository interface { GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error RollbackKindBoxRequestStatus(ctx context.Context, id uint) error + GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) } type KindBoxClient interface { diff --git a/validator/admin/kind_box_req/get_all.go b/validator/admin/kind_box_req/get_all.go new file mode 100644 index 0000000..7623f43 --- /dev/null +++ b/validator/admin/kind_box_req/get_all.go @@ -0,0 +1,5 @@ +package adminkindboxreqvalidator + +func (v Validator) ValidateGetAllRequest() (map[string]string, error) { + return map[string]string{}, nil +}