forked from gaodoo/golang-restful-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
35 lines (30 loc) · 739 Bytes
/
util.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
package apis
import (
"strconv"
"github.com/go-ozzo/ozzo-routing"
"github.com/qiangxue/golang-restful-starter-kit/util"
)
const (
DEFAULT_PAGE_SIZE int = 100
MAX_PAGE_SIZE int = 1000
)
func getPaginatedListFromRequest(c *routing.Context, count int) *util.PaginatedList {
page := parseInt(c.Query("page"), 1)
perPage := parseInt(c.Query("per_page"), DEFAULT_PAGE_SIZE)
if perPage <= 0 {
perPage = DEFAULT_PAGE_SIZE
}
if perPage > MAX_PAGE_SIZE {
perPage = MAX_PAGE_SIZE
}
return util.NewPaginatedList(page, perPage, count)
}
func parseInt(value string, defaultValue int) int {
if value == "" {
return defaultValue
}
if result, err := strconv.Atoi(value); err == nil {
return result
}
return defaultValue
}