Skip to content

Commit

Permalink
squash! api: refactor pagination
Browse files Browse the repository at this point in the history
api: return Pagination instead of just Total
  • Loading branch information
altergui committed Jul 8, 2024
1 parent 09ef309 commit d3fae84
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
4 changes: 2 additions & 2 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ func (a *API) sendAccountList(ctx *httprouter.HTTPContext, paramPage string) err
return ErrIndexerQueryFailed.WithErr(err)
}
list := &AccountsList{
Accounts: accounts,
Total: total,
Accounts: accounts,
Pagination: calculatePagination(page, total),
}
return marshalAndSend(ctx, list)
}
19 changes: 14 additions & 5 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ type CountResult struct {
Count uint64 `json:"count" example:"10"`
}

// Pagination contains all the values needed for the UI to easily organize the returned data
type Pagination struct {
TotalItems uint64 `json:"total_items"`
TotalPages uint64 `json:"total_pages"`
PreviousPage uint64 `json:"previous_page"`
CurrentPage uint64 `json:"current_page"`
NextPage uint64 `json:"next_page"`
}

type OrganizationSummary struct {
OrganizationID types.HexBytes `json:"organizationID" example:"0x370372b92514d81a0e3efb8eba9d036ae0877653"`
ElectionCount uint64 `json:"electionCount" example:"1"`
Expand All @@ -26,7 +35,7 @@ type OrganizationSummary struct {
// and return an empty object if the list does not contains any result
type OrganizationsList struct {
Organizations []OrganizationSummary `json:"organizations"`
Total uint64 `json:"total"`
Pagination *Pagination `json:"pagination"`
}

type ElectionSummary struct {
Expand All @@ -45,8 +54,8 @@ type ElectionSummary struct {
// ElectionsList wraps the elections list to consistently return the list inside an object,
// and return an empty object if the list does not contains any result
type ElectionsList struct {
Elections []ElectionSummary `json:"elections"`
Total uint64 `json:"total"`
Elections []ElectionSummary `json:"elections"`
Pagination *Pagination `json:"pagination"`
}

// ElectionResults is the struct used to wrap the results of an election
Expand Down Expand Up @@ -246,8 +255,8 @@ type Account struct {
}

type AccountsList struct {
Accounts []indexertypes.Account `json:"accounts"`
Total uint64 `json:"total"`
Accounts []indexertypes.Account `json:"accounts"`
Pagination *Pagination `json:"pagination"`
}

type AccountSet struct {
Expand Down
2 changes: 1 addition & 1 deletion api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (a *API) sendOrganizationList(ctx *httprouter.HTTPContext,
}

list := &OrganizationsList{
Total: total,
Pagination: calculatePagination(page, total),
}
for _, org := range entities {
list.Organizations = append(list.Organizations, OrganizationSummary{
Expand Down
2 changes: 1 addition & 1 deletion api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (a *API) sendElectionList(ctx *httprouter.HTTPContext, params *ElectionFilt
}

list := &ElectionsList{
Total: total,
Pagination: calculatePagination(params.Page, total),
}
for _, eid := range eids {
e, err := a.indexer.ProcessInfo(eid)
Expand Down
20 changes: 20 additions & 0 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"math/big"
"strconv"
"strings"
Expand Down Expand Up @@ -235,3 +236,22 @@ func parseBool(s string) (bool, error) {
}
return b, nil
}

func calculatePagination(page int, totalItems uint64) *Pagination {
totalp := math.Ceil(float64(totalItems) / ItemsPerPage)
prevp := page - 1
if prevp < 0 {
prevp = 0
}
nextp := page + 1
if nextp > int(totalp) {
nextp = int(totalp)
}
return &Pagination{
TotalItems: totalItems,
TotalPages: uint64(totalp),
PreviousPage: uint64(prevp),
CurrentPage: uint64(page),
NextPage: uint64(nextp),
}
}
16 changes: 8 additions & 8 deletions test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ func TestAPIAccountsList(t *testing.T) {
qt.Assert(t, el["1"], qt.DeepEquals, el["p1"])

// 2 accounts pre-exist: the faucet account, and the burn address
qt.Assert(t, el["0"].Total, qt.Equals, uint64(2+20))
qt.Assert(t, el["1"].Total, qt.Equals, el["0"].Total)
qt.Assert(t, el["p0"].Total, qt.Equals, el["0"].Total)
qt.Assert(t, el["p1"].Total, qt.Equals, el["0"].Total)
qt.Assert(t, el["0"].Pagination.TotalItems, qt.Equals, uint64(2+20))
qt.Assert(t, el["1"].Pagination.TotalItems, qt.Equals, el["0"].Pagination.TotalItems)
qt.Assert(t, el["p0"].Pagination.TotalItems, qt.Equals, el["0"].Pagination.TotalItems)
qt.Assert(t, el["p1"].Pagination.TotalItems, qt.Equals, el["0"].Pagination.TotalItems)

for _, item := range el {
qt.Assert(t, len(item.Accounts), qt.Equals, api.ItemsPerPage)
Expand Down Expand Up @@ -943,10 +943,10 @@ func TestAPIElectionsList(t *testing.T) {
qt.Assert(t, el["0"], qt.DeepEquals, el["p0"])
qt.Assert(t, el["1"], qt.DeepEquals, el["p1"])

qt.Assert(t, el["0"].Total, qt.Equals, uint64(20))
qt.Assert(t, el["1"].Total, qt.Equals, el["0"].Total)
qt.Assert(t, el["p0"].Total, qt.Equals, el["0"].Total)
qt.Assert(t, el["p1"].Total, qt.Equals, el["0"].Total)
qt.Assert(t, el["0"].Pagination.TotalItems, qt.Equals, uint64(20))
qt.Assert(t, el["1"].Pagination.TotalItems, qt.Equals, el["0"].Pagination.TotalItems)
qt.Assert(t, el["p0"].Pagination.TotalItems, qt.Equals, el["0"].Pagination.TotalItems)
qt.Assert(t, el["p1"].Pagination.TotalItems, qt.Equals, el["0"].Pagination.TotalItems)

for _, item := range el {
qt.Assert(t, len(item.Elections), qt.Equals, api.ItemsPerPage)
Expand Down

0 comments on commit d3fae84

Please sign in to comment.