-
Notifications
You must be signed in to change notification settings - Fork 164
/
pagination.go
32 lines (26 loc) · 707 Bytes
/
pagination.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package gorsk
// Pagination constants
const (
paginationDefaultLimit = 100
paginationMaxLimit = 1000
)
// PaginationReq holds pagination http fields and tags
type PaginationReq struct {
Limit int `query:"limit"`
Page int `query:"page" validate:"min=0"`
}
// Transform checks and converts http pagination into database pagination model
func (p PaginationReq) Transform() Pagination {
if p.Limit < 1 {
p.Limit = paginationDefaultLimit
}
if p.Limit > paginationMaxLimit {
p.Limit = paginationMaxLimit
}
return Pagination{Limit: p.Limit, Offset: p.Page * p.Limit}
}
// Pagination data
type Pagination struct {
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
}