forked from ebhomengo/niki
1
0
Fork 0

feat(repo): add pagination to getallkindboxreq (#32)

Co-authored-by: mohammad mahdi rezaei <rezaei_21@yahoo.com>
Reviewed-on: ebhomengo/niki#32
Co-authored-by: mehdi <lmehdirezl@gmail.com>
Co-committed-by: mehdi <lmehdirezl@gmail.com>
This commit is contained in:
mehdi 2024-04-28 11:27:23 +00:00 committed by Alireza Mokhtari Garakani
parent 93b0f51911
commit 0d9f98a2e0
8 changed files with 109 additions and 18 deletions

View File

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

View File

@ -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"`

View File

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

View File

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

40
param/pagination.go Normal file
View File

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

View File

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

View File

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

View File

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