forked from ebhomengo/niki
feat: add order in get_all kindboxreq
This commit is contained in:
parent
b951c29462
commit
5ed7f73563
|
@ -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)
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue