mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-22 08:35:43 +00:00
fix pagination issues on backend
This commit is contained in:
parent
188bd054e5
commit
c379a73a63
2 changed files with 17 additions and 10 deletions
|
@ -1,6 +1,8 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/hay-kot/homebox/backend/internal/core/services"
|
||||
|
@ -41,6 +43,11 @@ func (ctrl *V1Controller) HandleItemsGetAll() server.HandlerFunc {
|
|||
ctx := services.NewContext(r.Context())
|
||||
items, err := ctrl.repo.Items.QueryByGroup(ctx, ctx.GID, extractQuery(r))
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return server.Respond(w, http.StatusOK, repo.PaginationResult[repo.ItemSummary]{
|
||||
Items: []repo.ItemSummary{},
|
||||
})
|
||||
}
|
||||
log.Err(err).Msg("failed to get items")
|
||||
return validate.NewRequestError(err, http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
@ -326,23 +326,23 @@ func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q Ite
|
|||
)
|
||||
}
|
||||
|
||||
count, err := qb.Count(ctx)
|
||||
if err != nil {
|
||||
return PaginationResult[ItemSummary]{}, err
|
||||
}
|
||||
|
||||
qb = qb.Order(ent.Asc(item.FieldName)).
|
||||
WithLabel().
|
||||
WithLocation()
|
||||
|
||||
if q.Page != -1 || q.PageSize != -1 {
|
||||
qb = qb.
|
||||
Offset(calculateOffset(q.Page, q.PageSize)).
|
||||
Limit(q.PageSize)
|
||||
}
|
||||
|
||||
items, err := mapItemsSummaryErr(
|
||||
qb.Order(ent.Asc(item.FieldName)).
|
||||
WithLabel().
|
||||
WithLocation().
|
||||
All(ctx),
|
||||
)
|
||||
if err != nil {
|
||||
return PaginationResult[ItemSummary]{}, err
|
||||
}
|
||||
items, err := mapItemsSummaryErr(qb.All(ctx))
|
||||
|
||||
count, err := qb.Count(ctx)
|
||||
if err != nil {
|
||||
return PaginationResult[ItemSummary]{}, err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue