From d552528ce744f439f44f8ccda386f8b37e2a9e1f Mon Sep 17 00:00:00 2001 From: Zaptoss Date: Mon, 12 Aug 2024 11:51:34 +0300 Subject: [PATCH] Improve pagination --- internal/service/handlers/leaderboard.go | 9 +++++++- internal/service/handlers/list_events.go | 26 +++++++++++------------- internal/service/page/offset_params.go | 11 ++++++++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/internal/service/handlers/leaderboard.go b/internal/service/handlers/leaderboard.go index ec3b8f2..38a729c 100644 --- a/internal/service/handlers/leaderboard.go +++ b/internal/service/handlers/leaderboard.go @@ -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 { diff --git a/internal/service/handlers/list_events.go b/internal/service/handlers/list_events.go index 8dd40c1..b28d1b6 100644 --- a/internal/service/handlers/list_events.go +++ b/internal/service/handlers/list_events.go @@ -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) @@ -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"` diff --git a/internal/service/page/offset_params.go b/internal/service/page/offset_params.go index 03e144c..6292b78 100644 --- a/internal/service/page/offset_params.go +++ b/internal/service/page/offset_params.go @@ -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 }