From 5ed7f735631e81260d3687607272634854363239 Mon Sep 17 00:00:00 2001 From: hossein1990 Date: Tue, 11 Jun 2024 22:16:51 +0330 Subject: [PATCH] feat: add order in get_all kindboxreq --- .../http_server/admin/kind_box_req/get_all.go | 9 +++++++-- .../http_server/admin/kind_box_req/route.go | 2 +- param/ordering.go | 17 +++++++++++++++++ repository/mysql/kind_box_req/get_all.go | 6 ++++-- service/admin/kind_box_req/get_all.go | 6 ++++-- 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 param/ordering.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 fefebdc..eb12337 100644 --- a/delivery/http_server/admin/kind_box_req/get_all.go +++ b/delivery/http_server/admin/kind_box_req/get_all.go @@ -4,7 +4,8 @@ import ( "net/http" "strconv" - paginationparam "git.gocasts.ir/ebhomengo/niki/param" + "git.gocasts.ir/ebhomengo/niki/param" + orderparam "git.gocasts.ir/ebhomengo/niki/param" 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" @@ -28,6 +29,10 @@ func (h Handler) GetAll(c echo.Context) error { } var paginationReq paginationparam.PaginationRequest + var orderReq orderparam.OrderRequest + // get order default is ASC + ordering := c.QueryParam("order") + orderReq.Ordering = ordering // TODO : pkg convert string to uint //nolint pageNumber, _ := strconv.ParseUint(c.QueryParam("page_number"), 0, 64) @@ -36,7 +41,7 @@ func (h Handler) GetAll(c echo.Context) error { paginationReq.PageSize = uint(pageSize) paginationReq.PageNumber = uint(pageNumber) - resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req, paginationReq) + resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req, paginationReq, orderReq) if sErr != nil { msg, code := httpmsg.Error(sErr) diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index 81769d8..b801ffc 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -14,5 +14,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.PATCH("/reject-kind-box-req/:id", h.Reject, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqRejectPermission)) r.PATCH("/deliver-kind-box-req/:id", h.Deliver, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqDeliverPermission)) r.PATCH("/assign-sender-agent/:id", h.AssignSenderAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqAssignSenderAgentPermission)) - r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAllPermission)) + r.GET("get-all", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAllPermission)) } diff --git a/param/ordering.go b/param/ordering.go new file mode 100644 index 0000000..441d168 --- /dev/null +++ b/param/ordering.go @@ -0,0 +1,17 @@ +package param + +const ( + defaultOrder = "ASC" +) + +type OrderRequest struct { + Ordering string +} + +func (o *OrderRequest) GetOrder() string { + if len(o.Order) == 0 { + o.Order = defaultOrder + } + + return o.Order +} diff --git a/repository/mysql/kind_box_req/get_all.go b/repository/mysql/kind_box_req/get_all.go index 8d452f6..1f158eb 100644 --- a/repository/mysql/kind_box_req/get_all.go +++ b/repository/mysql/kind_box_req/get_all.go @@ -4,12 +4,14 @@ import ( "context" "git.gocasts.ir/ebhomengo/niki/entity" + orderparam "git.gocasts.ir/ebhomengo/niki/param" + paginationparam "git.gocasts.ir/ebhomengo/niki/param" 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, pagination paginationparam.PaginationRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) { +func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest, order orderparam.OrderRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) { const op = "mysqlkindboxreq.GetAllKindBoxReq" // TODO: create getCount function @@ -35,7 +37,7 @@ func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.Pag } // TODO - add sort and filter - rows, err = d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs where deleted_at is null limit ? offset ?", pagination.GetPageSize(), pagination.GetOffset()) + rows, err = d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs where deleted_at is null limit ? offset ? order by created_at ", pagination.GetPageSize(), pagination.GetOffset(), order.GetOrder()) if err != nil { return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected) diff --git a/service/admin/kind_box_req/get_all.go b/service/admin/kind_box_req/get_all.go index 9208e55..46a4a7a 100644 --- a/service/admin/kind_box_req/get_all.go +++ b/service/admin/kind_box_req/get_all.go @@ -3,16 +3,18 @@ package adminkindboxreqservice import ( "context" + orderparam "git.gocasts.ir/ebhomengo/niki/param" + paginationparam "git.gocasts.ir/ebhomengo/niki/param" 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, paginationreq paginationparam.PaginationRequest) (param.KindBoxReqGetAllResponse, error) { +func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest, paginationreq paginationparam.PaginationRequest, orderreq orderparam.OrderRequest) (param.KindBoxReqGetAllResponse, error) { const op = "adminkindboxreqservice.GetAll" - allKindBoxReq, pagination, err := s.repo.GetAllKindBoxReq(ctx, paginationreq) + allKindBoxReq, pagination, err := s.repo.GetAllKindBoxReq(ctx, paginationreq, orderreq) if err != nil { return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) }