feat: add order in get_all kindboxreq

This commit is contained in:
hossein1990 2024-06-11 22:16:51 +03:30
parent b951c29462
commit 5ed7f73563
5 changed files with 33 additions and 7 deletions

View File

@ -4,7 +4,8 @@ import (
"net/http" "net/http"
"strconv" "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" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
@ -28,6 +29,10 @@ func (h Handler) GetAll(c echo.Context) error {
} }
var paginationReq paginationparam.PaginationRequest 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 // TODO : pkg convert string to uint
//nolint //nolint
pageNumber, _ := strconv.ParseUint(c.QueryParam("page_number"), 0, 64) 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.PageSize = uint(pageSize)
paginationReq.PageNumber = uint(pageNumber) 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 { if sErr != nil {
msg, code := httpmsg.Error(sErr) msg, code := httpmsg.Error(sErr)

View File

@ -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("/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("/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.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))
} }

17
param/ordering.go Normal file
View File

@ -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
}

View File

@ -4,12 +4,14 @@ import (
"context" "context"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
orderparam "git.gocasts.ir/ebhomengo/niki/param"
paginationparam "git.gocasts.ir/ebhomengo/niki/param" paginationparam "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" 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" const op = "mysqlkindboxreq.GetAllKindBoxReq"
// TODO: create getCount function // TODO: create getCount function
@ -35,7 +37,7 @@ func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.Pag
} }
// TODO - add sort and filter // 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 { if err != nil {
return nil, paginationparam.PaginationResponse{}, return nil, paginationparam.PaginationResponse{},
richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected) richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected)

View File

@ -3,16 +3,18 @@ package adminkindboxreqservice
import ( import (
"context" "context"
orderparam "git.gocasts.ir/ebhomengo/niki/param"
paginationparam "git.gocasts.ir/ebhomengo/niki/param" paginationparam "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
// TODO: Pagination, Filters, Sort. // 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" const op = "adminkindboxreqservice.GetAll"
allKindBoxReq, pagination, err := s.repo.GetAllKindBoxReq(ctx, paginationreq) allKindBoxReq, pagination, err := s.repo.GetAllKindBoxReq(ctx, paginationreq, orderreq)
if err != nil { if err != nil {
return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }