generated from PenaSide/GolangTemplate
refactor pagination
This commit is contained in:
parent
ccbc975957
commit
38bb4fb43c
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module penahub.gitlab.yandexcloud.net/pena-services/customer
|
||||
|
||||
go 1.20
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/deepmap/oapi-codegen v1.12.4
|
||||
|
@ -171,11 +171,6 @@ func (api *API2) AddAccount(ctx echo.Context) error {
|
||||
|
||||
account, err = api.account.Insert(ctx.Request().Context(), &models.Account{UserID: user.ID, Wallet: models.Wallet{Currency: defaultCurrency}})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to create account on <CreateAccountByUserID> of <AccountService>",
|
||||
zap.Error(err),
|
||||
zap.String("userID", userID),
|
||||
)
|
||||
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -185,10 +180,6 @@ func (api *API2) AddAccount(ctx echo.Context) error {
|
||||
func (api *API2) DeleteDirectAccount(ctx echo.Context, userID string) error {
|
||||
account, err := api.account.Remove(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to remove account on <RemoveAccount> of <AccountService>",
|
||||
zap.Error(err),
|
||||
zap.String("userID", userID),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -198,11 +189,6 @@ func (api *API2) DeleteDirectAccount(ctx echo.Context, userID string) error {
|
||||
func (api *API2) GetDirectAccount(ctx echo.Context, userID string) error {
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get account by id on <GetAccountByUserID> of <AccountService>",
|
||||
zap.Error(err),
|
||||
zap.String("userID", userID),
|
||||
)
|
||||
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -210,21 +196,15 @@ func (api *API2) GetDirectAccount(ctx echo.Context, userID string) error {
|
||||
}
|
||||
|
||||
func (api *API2) PaginationAccounts(ctx echo.Context, params PaginationAccountsParams) error {
|
||||
// TODO refactor utils
|
||||
pagination := utils.DeterminePagination(params.Page, params.Limit)
|
||||
|
||||
if pagination == nil {
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("pagination is nil on <GetAccountsList> of <AccountService>: %w", errors.ErrInternalError),
|
||||
errors.ErrInternalError,
|
||||
))
|
||||
if params.Page == nil || params.Limit == nil {
|
||||
return api.error(ctx, http.StatusInternalServerError, "default values missing for PaginationAccounts")
|
||||
}
|
||||
|
||||
page := int64(max(*params.Page, 1))
|
||||
limit := min(int64(max(*params.Limit, 1)), models.DefaultLimit)
|
||||
|
||||
count, err := api.account.CountAll(ctx.Request().Context())
|
||||
if err != nil {
|
||||
api.logger.Error("failed to count accounts on <GetAccountsList> of <AccountService>",
|
||||
zap.Error(err),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -233,15 +213,10 @@ func (api *API2) PaginationAccounts(ctx echo.Context, params PaginationAccountsP
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
totalPages := int64(math.Ceil(float64(count) / float64(pagination.Limit)))
|
||||
totalPages := int64(math.Ceil(float64(count) / float64(limit)))
|
||||
|
||||
accounts, err := api.account.FindMany(ctx.Request().Context(), pagination.Page, pagination.Limit)
|
||||
accounts, err := api.account.FindMany(ctx.Request().Context(), page, limit)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get accounts list on <GetAccountsList> of <AccountService>",
|
||||
zap.Error(err),
|
||||
zap.Int64("page", pagination.Page),
|
||||
zap.Int64("limit", pagination.Limit),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user