From 4710617f23bfe0b96d50efd84f59084d3ef17ae8 Mon Sep 17 00:00:00 2001 From: Flashfyre Date: Sun, 17 Mar 2024 13:36:32 -0400 Subject: [PATCH] Add limit and pagination to rankings query --- api/daily.go | 16 +++++++++++++++- db/daily.go | 6 ++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/api/daily.go b/api/daily.go index ceb422d..19a2f34 100644 --- a/api/daily.go +++ b/api/daily.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "net/http" + "strconv" "time" "github.com/Flashfyre/pokerogue-server/db" @@ -40,7 +41,20 @@ func (s *Server) HandleSeed(w http.ResponseWriter, r *http.Request) { // /daily/rankings - fetch daily rankings func (s *Server) HandleRankings(w http.ResponseWriter, r *http.Request) { - rankings, err := db.GetRankings() + var err error + var page int + + if r.URL.Query().Has("page") { + page, err = strconv.Atoi(r.URL.Query().Get("page")) + if err != nil { + http.Error(w, fmt.Sprintf("failed to convert page: %s", err), http.StatusBadRequest) + return + } + } else { + page = 1 + } + + rankings, err := db.GetRankings(page) if err != nil { log.Print("failed to retrieve rankings") } diff --git a/db/daily.go b/db/daily.go index 1372649..e3c55ca 100644 --- a/db/daily.go +++ b/db/daily.go @@ -13,10 +13,12 @@ func TryAddDailyRun(seed string) error { return nil } -func GetRankings() ([]defs.DailyRanking, error) { +func GetRankings(page int) ([]defs.DailyRanking, error) { var rankings []defs.DailyRanking - results, err := handle.Query("SELECT RANK() OVER (ORDER BY sc.score DESC, sc.timestamp), a.username, sc.score FROM seedCompletions sc JOIN dailyRuns dr ON dr.seed = sc.seed JOIN accounts a ON sc.uuid = a.uuid WHERE dr.date = UTC_DATE()") + offset := (page - 1) * 10 + + results, err := handle.Query("SELECT RANK() OVER (ORDER BY sc.score DESC, sc.timestamp), a.username, sc.score FROM seedCompletions sc JOIN dailyRuns dr ON dr.seed = sc.seed JOIN accounts a ON sc.uuid = a.uuid WHERE dr.date = UTC_DATE() LIMIT 10 OFFSET ?", offset) if err != nil { return rankings, err }