-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpagination.go
303 lines (255 loc) · 7.55 KB
/
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) 2022 Enver Bisevac
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package render
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
var (
// PageParam is query name param for current page
PageParam = "page"
// PerPageParam is number of items per page
PerPageParam = "per_page"
// PerPageDefault sets default number of items on response
PerPageDefault = 25
// PageHeader represents x-page key in header
PageHeader = "x-page"
// PerPageHeader represents x-per-page key in header
PerPageHeader = "x-per-page"
// NextPageHeader represents x-next key in header
NextPageHeader = "x-next-page"
// PrevPageHeader represents x-pprev key in header
PrevPageHeader = "x-prev-page"
// TotalItemsHeader represents x-total key in header
TotalItemsHeader = "x-total"
// TotalPagesHeader represents x-total-pages key in header
TotalPagesHeader = "x-total-pages"
// LinkHeader represents Link key in header
LinkHeader = "Link"
// Linkf is format for Link headers
Linkf = `<%s>; rel="%s"`
// PaginationInHeader write pagination in header
PaginationInHeader = true
// PaginationHeader generates pagination in header
PaginationHeader = DefaultPaginationHeader
// PaginationBody generates pagination in body
PaginationBody = DefaultPaginationBody
)
// Pagination holds all page related data.
type Pagination struct {
url *url.URL
page int
perPage int
last int
total int
}
// PaginationOption is prototype for functional options.
type PaginationOption func(p *Pagination)
// WithPerPage set perPage value.
func WithPerPage(val int) PaginationOption {
return func(p *Pagination) {
p.perPage = val
p.last = totalPages(p.perPage, p.total)
}
}
// PaginationFromRequest returns pagination object from parsed request url field
func PaginationFromRequest(r *http.Request, totalItems int, options ...PaginationOption) Pagination {
return NewPagination(r.URL, totalItems, options...)
}
// NewPagination parses url and return new pagination object.
func NewPagination(url *url.URL, totalItems int, options ...PaginationOption) Pagination {
queryParams := url.Query()
strPage := queryParams.Get(PageParam)
strPerPage := queryParams.Get(PerPageParam)
page, err := strconv.Atoi(strPage)
if err != nil {
page = 1
}
perPage, err := strconv.Atoi(strPerPage)
if err != nil {
perPage = PerPageDefault
}
last := totalPages(perPage, totalItems)
pagination := Pagination{
url: url,
page: page,
perPage: perPage,
last: last,
total: totalItems,
}
for _, option := range options {
option(&pagination)
}
return pagination
}
// URL returns non exported page value
func (p Pagination) URL() *url.URL {
return p.url
}
// Page returns non exported page value
func (p Pagination) Page() int {
return p.page
}
// PerPage returns perPage (per_page) value
func (p Pagination) PerPage() int {
return p.perPage
}
// Prev page
func (p Pagination) Prev() int {
return max(p.page-1, 1)
}
// PrevURL page
func (p Pagination) PrevURL() string {
if p.url == nil {
return ""
}
params := p.url.Query()
params.Set(PageParam, strconv.Itoa(p.page))
params.Set(PerPageParam, strconv.Itoa(p.perPage))
if p.page > 1 {
params.Set(PageParam, strconv.Itoa(p.Prev()))
p.url.RawQuery = params.Encode()
return p.url.String()
}
return ""
}
// Next page
func (p Pagination) Next() int {
return min(p.page+1, p.last)
}
// NextURL page
func (p Pagination) NextURL() string {
if p.url == nil {
return ""
}
params := p.url.Query()
params.Set(PageParam, strconv.Itoa(p.page))
params.Set(PerPageParam, strconv.Itoa(p.perPage))
if p.page != p.last {
params.Set(PageParam, strconv.Itoa(p.Next()))
p.url.RawQuery = params.Encode()
return p.url.String()
}
return ""
}
// Last page
func (p Pagination) Last() int {
return p.last
}
// LastURL page
func (p Pagination) LastURL() string {
if p.url == nil {
return ""
}
params := p.url.Query()
params.Set(PageParam, strconv.Itoa(p.page))
params.Set(PerPageParam, strconv.Itoa(p.perPage))
params.Set(PageParam, strconv.Itoa(p.last))
p.url.RawQuery = params.Encode()
return p.url.String()
}
// Total returns total number of elements
func (p Pagination) Total() int {
return p.total
}
func (p Pagination) shouldRedirect() bool {
last := p.last
switch {
case p.page == 0:
return true
case p.page > last:
return true
case p.perPage == 0:
return true
}
return false
}
func (p Pagination) redirect(w http.ResponseWriter, r *http.Request) {
uri := *r.URL
last := p.last
page := p.page
perPage := p.perPage
if page == 0 {
page = 1
}
if page > last {
page = last
}
if perPage == 0 {
perPage = PerPageDefault
}
params := uri.Query()
params.Set(PageParam, strconv.Itoa(page))
params.Set(PerPageParam, strconv.Itoa(perPage))
uri.RawQuery = params.Encode()
http.Redirect(w, r, uri.String(), http.StatusMovedPermanently)
}
// Render renders payload and respond to the client request.
func (p Pagination) Render(w http.ResponseWriter, r *http.Request, v interface{}, params ...interface{}) {
if p.shouldRedirect() {
p.redirect(w, r)
return
}
if PaginationInHeader {
PaginationHeader(w, p)
} else {
v = PaginationBody(p, v)
}
Render(w, r, v, params...)
}
// DefaultPaginationHeader returns pagination metadata in header.
func DefaultPaginationHeader(w http.ResponseWriter, p Pagination) {
w.Header().Set(PageHeader, strconv.Itoa(p.page))
w.Header().Set(PerPageHeader, strconv.Itoa(p.perPage))
last := p.last
if p.page != last {
w.Header().Set(NextPageHeader, strconv.Itoa(p.Next()))
w.Header().Add(LinkHeader, fmt.Sprintf(Linkf, p.NextURL(), "next"))
}
if p.page > 1 {
w.Header().Set(PrevPageHeader, strconv.Itoa(p.Prev()))
w.Header().Add(LinkHeader, fmt.Sprintf(Linkf, p.PrevURL(), "prev"))
}
w.Header().Set(TotalItemsHeader, strconv.Itoa(p.total))
w.Header().Set(TotalPagesHeader, strconv.Itoa(last))
w.Header().Add(LinkHeader, fmt.Sprintf(Linkf, p.LastURL(), "last"))
}
type simpleBody struct {
Page int `json:"page" xml:"page"`
PerPage int `json:"per_page" xml:"per_page"`
Total int `json:"total" xml:"total"`
Next string `json:"next,omitempty" xml:"next,omitempty"`
Prev string `json:"prev,omitempty" xml:"prev,omitempty"`
Last string `json:"last,omitempty" xml:"last,omitempty"`
Items interface{} `json:"items" xml:"items"`
}
// DefaultPaginationBody returns custom pagination body.
func DefaultPaginationBody(p Pagination, v interface{}) interface{} {
return simpleBody{
Page: p.page,
PerPage: p.perPage,
Total: p.total,
Next: p.NextURL(),
Prev: p.PrevURL(),
Last: p.LastURL(),
Items: v,
}
}