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:
10
utils/db.go
10
utils/db.go
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user