Skip to content

Commit

Permalink
fix(api): html pagination
Browse files Browse the repository at this point in the history
Fixes #96
  • Loading branch information
philwinder committed Apr 26, 2023
1 parent 8340f48 commit 6903ab1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
14 changes: 4 additions & 10 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/item/item_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, "-")]
Expand Down
2 changes: 1 addition & 1 deletion pkg/item/item_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/item/queue_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6903ab1

Please sign in to comment.