From 385fa2645a0f7916254c784b4c0f21c733526570 Mon Sep 17 00:00:00 2001 From: "Mohammadi, Erfan" Date: Sun, 22 Sep 2024 00:18:23 +0330 Subject: [PATCH] fix(repository): resolve pagination 500 error on page_number field (#160) --- pkg/query_builder/mysql/pagination.go | 4 ++++ repository/mysql/kind_box/get_all.go | 2 +- repository/mysql/kind_box_req/get_all.go | 2 +- service/admin/kind_box/get_all.go | 8 ++++++-- service/admin/kind_box_req/get_all.go | 8 ++++++-- service/agent/kind_box/get_all_return_awaiting.go | 8 ++++++-- service/agent/kind_box_req/get_all_delivery_awaiting.go | 8 ++++++-- service/benefactor/kind_box/get_all.go | 7 +++++-- service/benefactor/kind_box_req/get_all.go | 7 +++++-- 9 files changed, 40 insertions(+), 14 deletions(-) diff --git a/pkg/query_builder/mysql/pagination.go b/pkg/query_builder/mysql/pagination.go index 4e75ccd..ebf873c 100644 --- a/pkg/query_builder/mysql/pagination.go +++ b/pkg/query_builder/mysql/pagination.go @@ -5,5 +5,9 @@ import ( ) func BuildPaginationQuery(pagination param.PaginationRequest) (query string, args []any) { + if pagination.PageSize == 0 && pagination.PageNumber == 0 { + return "", nil + } + return "LIMIT ? OFFSET ?", []any{pagination.GetPageSize(), pagination.GetOffset()} } diff --git a/repository/mysql/kind_box/get_all.go b/repository/mysql/kind_box/get_all.go index 0a68ccc..089b858 100644 --- a/repository/mysql/kind_box/get_all.go +++ b/repository/mysql/kind_box/get_all.go @@ -37,7 +37,7 @@ func (d *DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pag var total uint baseQuery = `SELECT COUNT(*) FROM kind_boxes WHERE deleted_at IS NULL` - query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort) + query, args = builder.BuildGetAllQuery(baseQuery, filter, params.PaginationRequest{}, params.SortRequest{}) qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total) if qErr != nil { return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/repository/mysql/kind_box_req/get_all.go b/repository/mysql/kind_box_req/get_all.go index 0211256..1c0cb85 100644 --- a/repository/mysql/kind_box_req/get_all.go +++ b/repository/mysql/kind_box_req/get_all.go @@ -37,7 +37,7 @@ func (d *DB) GetAllKindBoxReq(ctx context.Context, filter param.FilterRequest, p var total uint baseQuery = `SELECT COUNT(*) FROM kind_box_reqs WHERE deleted_at IS NULL` - query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort) + query, args = builder.BuildGetAllQuery(baseQuery, filter, param.PaginationRequest{}, param.SortRequest{}) qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total) if qErr != nil { return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/service/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go index e4505d9..b1d564d 100644 --- a/service/admin/kind_box/get_all.go +++ b/service/admin/kind_box/get_all.go @@ -14,6 +14,10 @@ func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (pa if fieldErrors, vErr := s.vld.ValidateGetAll(req); vErr != nil { return param.KindBoxGetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } + + req.Pagination.GetPageSize() + req.Pagination.GetPageNumber() + allKindBox, total, err := s.repo.GetAllKindBox(ctx, req.Filter, req.Pagination, req.Sort) if err != nil { return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err) @@ -45,8 +49,8 @@ func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (pa return param.KindBoxGetAllResponse{ Data: data, Pagination: paginationparam.PaginationResponse{ - PageSize: req.Pagination.GetPageSize(), - PageNumber: req.Pagination.GetPageNumber(), + PageSize: req.Pagination.PageSize, + PageNumber: req.Pagination.PageNumber, Total: total, }, }, nil diff --git a/service/admin/kind_box_req/get_all.go b/service/admin/kind_box_req/get_all.go index f09d4e5..c8e0bab 100644 --- a/service/admin/kind_box_req/get_all.go +++ b/service/admin/kind_box_req/get_all.go @@ -14,6 +14,10 @@ func (s Service) GetAll(ctx context.Context, req param.KindBoxReqGetAllRequest) if fieldErrors, vErr := s.vld.ValidateGetAll(req); vErr != nil { return param.KindBoxReqGetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } + + req.Pagination.GetPageSize() + req.Pagination.GetPageNumber() + allKindBoxReq, total, err := s.repo.GetAllKindBoxReq(ctx, req.Filter, req.Pagination, req.Sort) if err != nil { return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) @@ -39,8 +43,8 @@ func (s Service) GetAll(ctx context.Context, req param.KindBoxReqGetAllRequest) return param.KindBoxReqGetAllResponse{ Data: data, Pagination: paginationparam.PaginationResponse{ - PageSize: req.Pagination.GetPageSize(), - PageNumber: req.Pagination.GetPageNumber(), + PageSize: req.Pagination.PageSize, + PageNumber: req.Pagination.PageNumber, Total: total, }, }, nil diff --git a/service/agent/kind_box/get_all_return_awaiting.go b/service/agent/kind_box/get_all_return_awaiting.go index 7464b42..6f1a94d 100644 --- a/service/agent/kind_box/get_all_return_awaiting.go +++ b/service/agent/kind_box/get_all_return_awaiting.go @@ -13,6 +13,10 @@ func (s Service) GetAll(ctx context.Context, req param.GetAllRequest) (param.Get if fieldErrors, vErr := s.vld.ValidateGetAll(req); vErr != nil { return param.GetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } + + req.Pagination.GetPageSize() + req.Pagination.GetPageNumber() + allKindBoxes, total, err := s.repo.GetAllKindBox(ctx, req.Filter, req.Pagination, req.Sort) if err != nil { return param.GetAllResponse{}, richerror.New(op).WithErr(err) @@ -44,8 +48,8 @@ func (s Service) GetAll(ctx context.Context, req param.GetAllRequest) (param.Get return param.GetAllResponse{ Data: data, Pagination: paginationparam.PaginationResponse{ - PageSize: req.Pagination.GetPageSize(), - PageNumber: req.Pagination.GetPageNumber(), + PageSize: req.Pagination.PageSize, + PageNumber: req.Pagination.PageNumber, Total: total, }, }, nil diff --git a/service/agent/kind_box_req/get_all_delivery_awaiting.go b/service/agent/kind_box_req/get_all_delivery_awaiting.go index defcda2..5664f1a 100644 --- a/service/agent/kind_box_req/get_all_delivery_awaiting.go +++ b/service/agent/kind_box_req/get_all_delivery_awaiting.go @@ -2,6 +2,7 @@ package agentkindboxreqservice import ( "context" + paginationparam "git.gocasts.ir/ebhomengo/niki/param" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -14,6 +15,9 @@ func (s Service) GetAllAwaitingDelivery(ctx context.Context, req param.DeliveryA return param.DeliveryAwaitingGetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } + req.Pagination.GetPageSize() + req.Pagination.GetPageNumber() + allAwaitingKindBoxReq, total, err := s.repo.GetAllKindBoxReq(ctx, req.Filter, req.Pagination, req.Sort) if err != nil { return param.DeliveryAwaitingGetAllResponse{}, richerror.New(op).WithErr(err) @@ -39,8 +43,8 @@ func (s Service) GetAllAwaitingDelivery(ctx context.Context, req param.DeliveryA return param.DeliveryAwaitingGetAllResponse{ Data: data, Pagination: paginationparam.PaginationResponse{ - PageSize: req.Pagination.GetPageSize(), - PageNumber: req.Pagination.GetPageNumber(), + PageSize: req.Pagination.PageSize, + PageNumber: req.Pagination.PageNumber, Total: total, }, }, nil diff --git a/service/benefactor/kind_box/get_all.go b/service/benefactor/kind_box/get_all.go index 1577fbe..271df2d 100644 --- a/service/benefactor/kind_box/get_all.go +++ b/service/benefactor/kind_box/get_all.go @@ -14,6 +14,9 @@ func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (pa return param.KindBoxGetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } + req.Pagination.GetPageSize() + req.Pagination.GetPageNumber() + allKindBox, total, err := s.repo.GetAllKindBox(ctx, req.Filter, req.Pagination, req.Sort) if err != nil { return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err) @@ -45,8 +48,8 @@ func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (pa return param.KindBoxGetAllResponse{ Data: data, Pagination: params.PaginationResponse{ - PageSize: req.Pagination.GetPageSize(), - PageNumber: req.Pagination.GetPageNumber(), + PageSize: req.Pagination.PageSize, + PageNumber: req.Pagination.PageNumber, Total: total, }, }, nil diff --git a/service/benefactor/kind_box_req/get_all.go b/service/benefactor/kind_box_req/get_all.go index 2f7e8be..dfe5bb1 100644 --- a/service/benefactor/kind_box_req/get_all.go +++ b/service/benefactor/kind_box_req/get_all.go @@ -14,6 +14,9 @@ func (s Service) GetAll(ctx context.Context, req param.GetAllRequest) (param.Get return param.GetAllResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr) } + req.Pagination.GetPageSize() + req.Pagination.GetPageNumber() + allKindBoxReq, total, err := s.repo.GetAllKindBoxReq(ctx, req.Filter, req.Pagination, req.Sort) if err != nil { return param.GetAllResponse{}, richerror.New(op).WithErr(err) @@ -40,8 +43,8 @@ func (s Service) GetAll(ctx context.Context, req param.GetAllRequest) (param.Get return param.GetAllResponse{ Data: data, Pagination: params.PaginationResponse{ - PageSize: req.Pagination.GetPageSize(), - PageNumber: req.Pagination.GetPageNumber(), + PageSize: req.Pagination.PageSize, + PageNumber: req.Pagination.PageNumber, Total: total, }, }, nil