Skip to content

Commit

Permalink
Merge pull request #31 from rarimo/feat/pagination-improvements
Browse files Browse the repository at this point in the history
Improve pagination
  • Loading branch information
Zaptoss authored Aug 12, 2024
2 parents f962a57 + d552528 commit 4b2d471
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
9 changes: 8 additions & 1 deletion internal/service/handlers/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ func Leaderboard(w http.ResponseWriter, r *http.Request) {
return
}

leadersCount, err := BalancesQ(r).Count()
if err != nil {
Log(r).WithError(err).Error("Failed to leaders count")
ape.RenderErr(w, problems.InternalError())
return
}

resp := newLeaderboardResponse(leaders)
resp.Links = req.GetLinks(r)
resp.Links = req.GetLinks(r, uint64(leadersCount))
if req.Count {
leadersCount, err := BalancesQ(r).FilterDisabled().Count()
if err != nil {
Expand Down
26 changes: 12 additions & 14 deletions internal/service/handlers/list_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,17 @@ func ListEvents(w http.ResponseWriter, r *http.Request) {
}

var eventsCount int
if req.Count {
eventsCount, err = EventsQ(r).
FilterByNullifier(*req.FilterNullifier).
FilterByStatus(req.FilterStatus...).
FilterByType(req.FilterType...).
FilterInactiveNotClaimed(inactiveTypes...).
Count()
if err != nil {
Log(r).WithError(err).Errorf("Failed to count filtered events: nullifier=%s status=%v type=%v",
*req.FilterNullifier, req.FilterStatus, req.FilterType)
ape.RenderErr(w, problems.InternalError())
return
}
eventsCount, err = EventsQ(r).
FilterByNullifier(*req.FilterNullifier).
FilterByStatus(req.FilterStatus...).
FilterByType(req.FilterType...).
FilterInactiveNotClaimed(inactiveTypes...).
Count()
if err != nil {
Log(r).WithError(err).Errorf("Failed to count filtered events: nullifier=%s status=%v type=%v",
*req.FilterNullifier, req.FilterStatus, req.FilterType)
ape.RenderErr(w, problems.InternalError())
return
}

meta, err := getOrderedEventsMeta(events, r)
Expand All @@ -98,7 +96,7 @@ func ListEvents(w http.ResponseWriter, r *http.Request) {
}

resp := newEventsResponse(events, meta)
resp.Links = req.OffsetParams.GetLinks(r)
resp.Links = req.OffsetParams.GetLinks(r, uint64(eventsCount))
if req.Count {
_ = resp.PutMeta(struct {
EventsCount int `json:"events_count"`
Expand Down
11 changes: 9 additions & 2 deletions internal/service/page/offset_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ func (p *OffsetParams) Validate() error {
}.Filter()
}

func (p *OffsetParams) GetLinks(r *http.Request) *resources.Links {
func (p *OffsetParams) GetLinks(r *http.Request, resourceCount uint64) *resources.Links {
result := resources.Links{
Self: p.getLink(r, p.PageNumber),
Next: p.getLink(r, p.PageNumber+1),
}

if p.PageNumber != 0 {
result.Prev = p.getLink(r, p.PageNumber-1)
}

if p.Limit*p.PageNumber < resourceCount {
result.Next = p.getLink(r, p.PageNumber+1)
}
return &result
}
Expand Down

0 comments on commit 4b2d471

Please sign in to comment.