fix: fix&refactor
This commit is contained in:
parent
8eef7a95fb
commit
16c689403f
2 changed files with 18 additions and 9 deletions
|
@ -4,3 +4,10 @@ type Pagination struct {
|
|||
Page int32 `json:"page"`
|
||||
Total int32 `json:"total"`
|
||||
}
|
||||
|
||||
func Total(count int32, pageSize int32) int32 {
|
||||
if count%pageSize == 0 {
|
||||
return count / pageSize
|
||||
}
|
||||
return count/pageSize + 1
|
||||
}
|
||||
|
|
|
@ -215,26 +215,28 @@ func (c *Caller) ListUsers(ctx context.Context, filters models.UsersListFilters)
|
|||
return nil, pkg.Wrap(pkg.ErrBadInput, nil, op, "limit > 20")
|
||||
}
|
||||
|
||||
usersList := &models.UsersList{
|
||||
Users: make([]*models.User, 0),
|
||||
Pagination: models.Pagination{},
|
||||
}
|
||||
var list []*models.User
|
||||
query := c.db.Rebind(ListUsers)
|
||||
|
||||
err := c.db.SelectContext(ctx, &usersList.Users, query, filters.PageSize, filters.Offset())
|
||||
err := c.db.SelectContext(ctx, &list, query, filters.PageSize, filters.Offset())
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err, op)
|
||||
}
|
||||
|
||||
query = c.db.Rebind(CountUsers)
|
||||
err = c.db.GetContext(ctx, &usersList.Pagination.Total, query)
|
||||
var count int32
|
||||
err = c.db.GetContext(ctx, &count, query)
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err, op)
|
||||
}
|
||||
|
||||
usersList.Pagination.Page = filters.Page
|
||||
|
||||
return usersList, nil
|
||||
return &models.UsersList{
|
||||
Users: list,
|
||||
Pagination: models.Pagination{
|
||||
Total: models.Total(count, filters.PageSize),
|
||||
Page: filters.Page,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func handlePgErr(err error, op string) error {
|
||||
|
|
Loading…
Add table
Reference in a new issue