This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
132 lines (113 loc) · 2.87 KB
/
page.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package apiutil
import (
"net/http"
"net/url"
"strconv"
)
var (
// DefaultPageSize is the number NewPage
DefaultPageSize = 50
// DefaultMaxPageSize is the max size a page can be by default, set to -1 to
// ignore maximum sizes
DefaultMaxPageSize = 100
)
// Page represents pagination information
type Page struct {
Number int `json:"page,omitempty"`
Size int `json:"pageSize,omitempty"`
ResultCount int `json:"resultCount,omitempty"`
NextURL string `json:"nextUrl"`
PrevURL string `json:"prevUrl"`
}
// NewPage constructs a basic page struct, setting sensible defaults
func NewPage(number, size int) Page {
if number <= 0 {
number = 1
}
if DefaultMaxPageSize != -1 && size > DefaultMaxPageSize {
size = DefaultMaxPageSize
}
return Page{
Number: number,
Size: size,
}
}
// Limit is a convenience accessor for page size
func (p Page) Limit() int {
return p.Size
}
// Offset calculates the starting index for pagination based on page
// size & number
func (p Page) Offset() int {
return (p.Number - 1) * p.Size
}
// Next returns a page with the number advanced by one
func (p Page) Next() Page {
return Page{
Number: p.Number + 1,
Size: p.Size,
ResultCount: p.ResultCount,
NextURL: p.NextURL,
PrevURL: p.PrevURL,
}
}
// Prev returns a page with the number decremented by 1
func (p Page) Prev() Page {
return Page{
Number: p.Number - 1,
Size: p.Size,
ResultCount: p.ResultCount,
NextURL: p.NextURL,
PrevURL: p.PrevURL,
}
}
// SetQueryParams adds pagination info to a url as query parameters
func (p Page) SetQueryParams(u *url.URL) *url.URL {
q := u.Query()
q.Set("page", strconv.Itoa(p.Number))
if p.Size != DefaultPageSize {
q.Set("pageSize", strconv.Itoa(p.Size))
}
u.RawQuery = q.Encode()
return u
}
// NextPageExists returns false if the next page.ResultCount is a postive
// number and the starting offset of the next page exceeds page.ResultCount
func (p Page) NextPageExists() bool {
return p.ResultCount <= 0 || !(p.Number*p.Size >= p.ResultCount)
}
// PrevPageExists returns false if the page number is 1
func (p Page) PrevPageExists() bool {
return p.Number > 1
}
// PageFromRequest extracts pagination params from an http request
func PageFromRequest(r *http.Request) Page {
number := ReqParamInt(r, "page", 1)
if number <= 0 {
number = 1
}
size := ReqParamInt(r, "pageSize", DefaultPageSize)
if size <= 0 {
size = DefaultPageSize
}
if DefaultMaxPageSize != -1 && size > DefaultMaxPageSize {
size = DefaultMaxPageSize
}
return Page{
Number: number,
Size: size,
}
}
// NewPageFromOffsetAndLimit converts a offset and Limit to a Page struct
func NewPageFromOffsetAndLimit(offset, limit int) Page {
var number, size int
size = limit
if size <= 0 {
size = DefaultPageSize
}
number = offset/size + 1
return Page{
Number: number,
Size: size,
}
}