diff --git a/backend/app/api/handlers/v1/v1_ctrl_items.go b/backend/app/api/handlers/v1/v1_ctrl_items.go index ea961f3..d7be19d 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items.go @@ -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) } diff --git a/backend/internal/data/repo/repo_items.go b/backend/internal/data/repo/repo_items.go index d8a3904..e22f30a 100644 --- a/backend/internal/data/repo/repo_items.go +++ b/backend/internal/data/repo/repo_items.go @@ -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 }