fix pagination issues on backend

This commit is contained in:
Hayden 2022-12-31 11:43:55 -09:00
parent 188bd054e5
commit c379a73a63
No known key found for this signature in database
GPG key ID: 17CF79474E257545
2 changed files with 17 additions and 10 deletions

View file

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

View file

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