From 0d9f98a2e06eafc9e58f18b819cf27aeecfe2103 Mon Sep 17 00:00:00 2001 From: mehdi Date: Sun, 28 Apr 2024 11:27:23 +0000 Subject: [PATCH] feat(repo): add pagination to getallkindboxreq (#32) Co-authored-by: mohammad mahdi rezaei Reviewed-on: https://git.gocasts.ir/ebhomengo/niki/pulls/32 Co-authored-by: mehdi Co-committed-by: mehdi --- config.yml | 8 ++-- config/config.go | 2 +- .../http_server/admin/kind_box_req/get_all.go | 13 +++++- param/admin/kind_box_req/get_all.go | 6 ++- param/pagination.go | 40 ++++++++++++++++++ repository/mysql/kind_box_req/get_all.go | 41 +++++++++++++++---- service/admin/kind_box_req/get_all.go | 14 +++++-- service/admin/kind_box_req/service.go | 3 +- 8 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 param/pagination.go diff --git a/config.yml b/config.yml index 35fa4cb..e5ce107 100644 --- a/config.yml +++ b/config.yml @@ -7,16 +7,16 @@ auth: http_server: port: 1313 -mysql: +mariadb: port: 3306 - host: niki-mariadb + host: localhost db_name: niki_db username: niki password: nikiappt0lk2o20 redis: - port: 6379 - host: niki-redis + port: 6380 + host: localhost password: "" db: 0 diff --git a/config/config.go b/config/config.go index fb4c3d3..921adbc 100644 --- a/config/config.go +++ b/config/config.go @@ -14,7 +14,7 @@ type HTTPServer struct { type Config struct { HTTPServer HTTPServer `koanf:"http_server"` - Mysql mysql.Config `koanf:"mysql"` + Mysql mysql.Config `koanf:"mariadb"` Auth authservice.Config `koanf:"auth"` AdminAuth authservice.Config `koanf:"admin_auth"` Redis redis.Config `koanf:"redis"` 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 305705d..796756d 100644 --- a/delivery/http_server/admin/kind_box_req/get_all.go +++ b/delivery/http_server/admin/kind_box_req/get_all.go @@ -2,7 +2,9 @@ package adminkindboxreqhandler import ( "net/http" + "strconv" + paginationparam "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" @@ -14,7 +16,16 @@ func (h Handler) GetAll(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest) } - resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req) + var paginationReq paginationparam.PaginationRequest + // TODO : pkg convert string to uint + //nolint + pageNumber, _ := strconv.ParseUint(c.QueryParam("page_number"), 0, 64) + //nolint + pageSize, _ := strconv.ParseUint(c.QueryParam("page_size"), 0, 64) + paginationReq.PageSize = uint(pageSize) + paginationReq.PageNumber = uint(pageNumber) + + resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req, paginationReq) if sErr != nil { msg, code := httpmsg.Error(sErr) diff --git a/param/admin/kind_box_req/get_all.go b/param/admin/kind_box_req/get_all.go index 5ac6c4c..27d4cdc 100644 --- a/param/admin/kind_box_req/get_all.go +++ b/param/admin/kind_box_req/get_all.go @@ -1,9 +1,13 @@ package adminkindboxreqparam -import entity "git.gocasts.ir/ebhomengo/niki/entity" +import ( + entity "git.gocasts.ir/ebhomengo/niki/entity" + paginationparam "git.gocasts.ir/ebhomengo/niki/param" +) type KindBoxReqGetAllRequest struct{} type KindBoxReqGetAllResponse struct { AllKindBoxReq []entity.KindBoxReq + Pagination paginationparam.PaginationResponse } diff --git a/param/pagination.go b/param/pagination.go new file mode 100644 index 0000000..8dcddcd --- /dev/null +++ b/param/pagination.go @@ -0,0 +1,40 @@ +package param + +const ( + defaultPageNumber = 1 + dafaultPageSize = 10 +) + +type PaginationRequest struct { + PageSize uint + PageNumber uint +} + +type PaginationResponse struct { + PageSize uint + PageNumber uint + Total uint +} + +func (p *PaginationRequest) GetPageNumber() uint { + if p.PageNumber <= 0 { + p.PageNumber = defaultPageNumber + } + + return p.PageNumber +} + +func (p *PaginationRequest) GetOffset() uint { + return (p.GetPageNumber() - 1) * p.GetPageSize() +} + +func (p *PaginationRequest) GetPageSize() uint { + validPageSizes := []uint{10, 25, 50, 100} + for _, size := range validPageSizes { + if p.PageSize == size { + return size + } + } + + return dafaultPageSize +} diff --git a/repository/mysql/kind_box_req/get_all.go b/repository/mysql/kind_box_req/get_all.go index cc5eefc..7c99714 100644 --- a/repository/mysql/kind_box_req/get_all.go +++ b/repository/mysql/kind_box_req/get_all.go @@ -4,17 +4,40 @@ import ( "context" "git.gocasts.ir/ebhomengo/niki/entity" + 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) ([]entity.KindBoxReq, error) { +func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) { const op = "mysqlkindboxreq.GetAllKindBoxReq" - // TODO - add sort and filter - rows, err := d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs") + // TODO: create getCount function + var count uint + rows, err := d.conn.Conn().QueryContext(ctx, "SELECT COUNT(*) FROM kind_box_reqs") if err != nil { - return nil, + return nil, paginationparam.PaginationResponse{}, + richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected) + } + defer rows.Close() + + // Iterate through the rows (should only be one) and extract the count: + for rows.Next() { + err := rows.Scan(&count) + if err != nil { + panic(err) + } + } + + if rErr := rows.Err(); rErr != nil { + return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithErr(rErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + // TODO - add sort and filter + rows, err = d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs limit ? offset ?", pagination.GetPageSize(), pagination.GetOffset()) + if err != nil { + return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected) } @@ -27,16 +50,20 @@ func (d DB) GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) { for rows.Next() { kindBoxReq, sErr := scanKindBoxReq(rows) if sErr != nil { - return nil, richerror.New(op).WithErr(sErr). + return nil, paginationparam.PaginationResponse{}, 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). + return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithErr(rErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - return kindBoxReqs, nil + return kindBoxReqs, paginationparam.PaginationResponse{ + PageSize: pagination.GetPageSize(), + PageNumber: pagination.GetPageNumber(), + Total: count, + }, nil } diff --git a/service/admin/kind_box_req/get_all.go b/service/admin/kind_box_req/get_all.go index a7d4061..9208e55 100644 --- a/service/admin/kind_box_req/get_all.go +++ b/service/admin/kind_box_req/get_all.go @@ -3,18 +3,26 @@ package adminkindboxreqservice import ( "context" + 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) (param.KindBoxReqGetAllResponse, error) { +func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest, paginationreq paginationparam.PaginationRequest) (param.KindBoxReqGetAllResponse, error) { const op = "adminkindboxreqservice.GetAll" - allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx) + allKindBoxReq, pagination, err := s.repo.GetAllKindBoxReq(ctx, paginationreq) if err != nil { return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil + return param.KindBoxReqGetAllResponse{ + AllKindBoxReq: allKindBoxReq, + Pagination: paginationparam.PaginationResponse{ + PageSize: pagination.PageSize, + PageNumber: pagination.PageNumber, + Total: pagination.Total, + }, + }, nil } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 8f010ee..97d79a1 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -4,6 +4,7 @@ import ( "context" "git.gocasts.ir/ebhomengo/niki/entity" + paginationparam "git.gocasts.ir/ebhomengo/niki/param" ) type Repository interface { @@ -11,7 +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) + GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) } type Service struct {