1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat(wip): leaderboard pagination (resolve #417) [ci-skip]

This commit is contained in:
Ferdinand Mütsch
2022-10-16 18:59:19 +02:00
parent 8a21be4306
commit 41f6db8f34
9 changed files with 85 additions and 18 deletions

View File

@ -39,3 +39,13 @@ func WhereNullable(query *gorm.DB, col string, val any) *gorm.DB {
}
return query.Where(fmt.Sprintf("%s = ?", col), val)
}
func WithPaging(query *gorm.DB, limit, skip int) *gorm.DB {
if limit >= 0 {
query = query.Limit(limit)
}
if skip >= 0 {
query = query.Offset(skip)
}
return query
}

View File

@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"net/http"
"regexp"
"strconv"
@ -42,3 +43,16 @@ func IsNoCache(r *http.Request, cacheTtl time.Duration) bool {
}
return false
}
func ParsePageParams(r *http.Request) *models.PageParams {
pageParams := &models.PageParams{}
page := r.URL.Query().Get("page")
pageSize := r.URL.Query().Get("page_size")
if p, err := strconv.Atoi(page); err == nil {
pageParams.Page = p
}
if p, err := strconv.Atoi(pageSize); err == nil && pageParams.Page > 0 {
pageParams.PageSize = p
}
return pageParams
}