-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagination.go
More file actions
151 lines (126 loc) · 3.24 KB
/
Copy pathpagination.go
File metadata and controls
151 lines (126 loc) · 3.24 KB
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
package pagination
import (
"math"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
type PaginationRequest struct {
Page int `json:"page" form:"page"`
PerPage int `json:"per_page" form:"per_page"`
Search string `json:"search" form:"search"`
Sort string `json:"sort" form:"sort"`
Order string `json:"order" form:"order"`
IsDisabled bool `json:"is_disabled,omitempty" form:"is_disabled"`
}
type PaginationResponse struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
MaxPage int64 `json:"max_page"`
Total int64 `json:"total"`
IsDisabled bool `json:"is_disabled,omitempty"`
}
type PaginatedResponse struct {
Code int `json:"code"`
Status string `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
Pagination PaginationResponse `json:"pagination"`
}
func (p *PaginationRequest) GetOffset() int {
if p.Page <= 0 {
p.Page = 1
}
return (p.Page - 1) * p.PerPage
}
func (p *PaginationRequest) GetLimit() int {
if p.PerPage <= 0 {
p.PerPage = 10
}
return p.PerPage
}
func (p *PaginationRequest) Validate() {
if p.Page <= 0 {
p.Page = 1
}
if p.PerPage <= 0 {
p.PerPage = 10
}
if p.Order == "" {
p.Order = "asc"
}
if p.Order != "asc" && p.Order != "desc" {
p.Order = "asc"
}
}
func BindPagination(ctx *gin.Context) PaginationRequest {
pagination := PaginationRequest{
Page: 1,
PerPage: 10,
Search: "",
Sort: "",
Order: "asc",
IsDisabled: false,
}
if pageStr := ctx.Query("page"); pageStr != "" {
if page, err := strconv.Atoi(pageStr); err == nil && page > 0 {
pagination.Page = page
}
}
if perPageStr := ctx.Query("per_page"); perPageStr != "" {
if perPage, err := strconv.Atoi(perPageStr); err == nil && perPage > 0 && perPage <= 100 {
pagination.PerPage = perPage
}
}
pagination.Search = ctx.Query("search")
pagination.Sort = ctx.Query("sort")
if order := ctx.Query("order"); order == "desc" || order == "asc" {
pagination.Order = order
}
if isDisabled := ctx.Query("is_disabled"); isDisabled != "" {
switch strings.ToLower(isDisabled) {
case "1", "true", "yes", "y", "on":
pagination.IsDisabled = true
default:
pagination.IsDisabled = false
}
}
pagination.Validate()
return pagination
}
func CalculatePagination(pagination PaginationRequest, totalCount int64) PaginationResponse {
// When pagination disabled, return minimal metadata
if pagination.IsDisabled {
return PaginationResponse{
Page: 1,
PerPage: int(totalCount),
MaxPage: 1,
Total: totalCount,
IsDisabled: true,
}
}
maxPage := int64(math.Ceil(float64(totalCount) / float64(pagination.PerPage)))
if maxPage == 0 {
maxPage = 1
}
return PaginationResponse{
Page: pagination.Page,
PerPage: pagination.PerPage,
MaxPage: maxPage,
Total: totalCount,
IsDisabled: false,
}
}
func NewPaginatedResponse(code int, message string, data interface{}, pagination PaginationResponse) PaginatedResponse {
status := "success"
if code >= 400 {
status = "error"
}
return PaginatedResponse{
Code: code,
Status: status,
Message: message,
Data: data,
Pagination: pagination,
}
}