Add limit and pagination to rankings query

pull/1/head
Flashfyre 8 months ago
parent ff98047caa
commit 4710617f23

@ -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")
}

@ -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
}

Loading…
Cancel
Save