diff --git a/pkg/api/api.go b/pkg/api/api.go index 33ad7ab..5722516 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -188,11 +188,7 @@ func (a *amplifyAPI) GetV0JobsId(w http.ResponseWriter, r *http.Request, id stri } func parsePaginationParams(pageSize *int32, pageNumber *int32) item.ListParams { - paginationParams := item.ListParams{ - PageSize: 10, - PageNumber: 1, - Sort: "created_at", - } + paginationParams := item.NewListParams() // Use defaults lower down if pageSize != nil { paginationParams.PageSize = int(*pageSize) } @@ -203,11 +199,7 @@ func parsePaginationParams(pageSize *int32, pageNumber *int32) item.ListParams { } func parseGetQueueParams(params GetV0QueueParams) item.ListParams { - paginationParams := item.ListParams{ - PageSize: 10, - PageNumber: 1, - Sort: "created_at", - } + paginationParams := item.NewListParams() // Use defaults lower down if params.PageSize != nil { paginationParams.PageSize = int(*params.PageSize) } @@ -440,6 +432,7 @@ func (a *amplifyAPI) getJob(jobId string) (*JobSpec, error) { } func (a *amplifyAPI) getQueue(ctx context.Context, params item.ListParams) (*QueueCollection, error) { + log.Ctx(ctx).Trace().Msgf("GetQueue: %+v", params) e, err := a.er.List(ctx, params) if err != nil { return nil, err @@ -453,6 +446,7 @@ func (a *amplifyAPI) getQueue(ctx context.Context, params item.ListParams) (*Que return nil, err } totalPages := int(math.Ceil(float64(count) / float64(params.PageSize))) + log.Ctx(ctx).Trace().Msgf("Total pages: %d", totalPages) if params.PageNumber > totalPages || params.PageNumber < 1 { return nil, ErrPageOutOfRange } diff --git a/pkg/item/item_store.go b/pkg/item/item_store.go index 93c7ad6..ee96d1e 100644 --- a/pkg/item/item_store.go +++ b/pkg/item/item_store.go @@ -23,6 +23,14 @@ type ListParams struct { Sort string } +func NewListParams() ListParams { + return ListParams{ + PageSize: 10, + PageNumber: 1, + Sort: "-created_at", + } +} + // ItemStore is an interface to retrieve and store items type ItemStore interface { NewItem(ctx context.Context, params ItemParams) error @@ -57,15 +65,6 @@ var sort_map = map[string]string{ } func (r *itemStore) ListItems(ctx context.Context, params ListParams) ([]*Item, error) { - if params.PageNumber == 0 { - params.PageNumber = 0 - } - if params.PageSize == 0 { - params.PageSize = 10 - } - if params.Sort == "" { - params.Sort = "created_at" - } reverse := strings.HasPrefix(params.Sort, "-") var ok bool params.Sort, ok = sort_map[strings.TrimPrefix(params.Sort, "-")] diff --git a/pkg/item/item_store_test.go b/pkg/item/item_store_test.go index 134e26a..a5626fc 100644 --- a/pkg/item/item_store_test.go +++ b/pkg/item/item_store_test.go @@ -72,7 +72,7 @@ func TestPostgresIntegration(t *testing.T) { assert.Equal(t, itemDetail.CID, cid) // List items - items, err := r.ListItems(ctx, ListParams{}) + items, err := r.ListItems(ctx, NewListParams()) assert.NilError(t, err) assert.Assert(t, len(items) > 0) } diff --git a/pkg/item/queue_repository_test.go b/pkg/item/queue_repository_test.go index 556fa98..fccb5ed 100644 --- a/pkg/item/queue_repository_test.go +++ b/pkg/item/queue_repository_test.go @@ -65,7 +65,7 @@ func Test_QueueRepository_List(t *testing.T) { err = repo.Create(context.Background(), ItemParams{ID: id2, CID: "cid"}) assert.NilError(t, err) - l, err := repo.List(context.Background(), ListParams{}) + l, err := repo.List(context.Background(), NewListParams()) assert.NilError(t, err) assert.Equal(t, len(l), 2) for _, i := range l {