cluade code review changes
This commit is contained in:
@@ -13,6 +13,9 @@ import (
|
||||
"code.patial.tech/go/appcore/ptr"
|
||||
)
|
||||
|
||||
// MaxPageSize is the maximum allowed page size to prevent resource exhaustion.
|
||||
var MaxPageSize = 1000
|
||||
|
||||
type Pager struct {
|
||||
OrderBy *string `json:"orderBy"`
|
||||
OrderAsc *bool `json:"orderAsc"`
|
||||
@@ -39,7 +42,9 @@ func (i *Pager) Offset() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
return (i.Page - 1) * i.Limit()
|
||||
page := max(i.Page, 1)
|
||||
|
||||
return (page - 1) * i.Limit()
|
||||
}
|
||||
|
||||
func (i *Pager) Limit() int {
|
||||
@@ -63,14 +68,17 @@ func GetPager(r *http.Request) Pager {
|
||||
}
|
||||
|
||||
if v := r.URL.Query().Get("pg"); v != "" {
|
||||
if vv, err := strconv.Atoi(v); err == nil {
|
||||
p.Page = int(vv)
|
||||
if vv, err := strconv.Atoi(v); err == nil && vv > 0 {
|
||||
p.Page = vv
|
||||
}
|
||||
}
|
||||
|
||||
if v := r.URL.Query().Get("pg_s"); v != "" {
|
||||
if vv, err := strconv.Atoi(v); err == nil {
|
||||
p.Size = int(vv)
|
||||
if vv, err := strconv.Atoi(v); err == nil && vv > 0 {
|
||||
if vv > MaxPageSize {
|
||||
vv = MaxPageSize
|
||||
}
|
||||
p.Size = vv
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user