-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagination.go
177 lines (156 loc) · 4.41 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
package pagination
import (
"github.com/getevo/evo/v2"
"github.com/getevo/evo/v2/lib/outcome"
"github.com/getevo/evo/v2/lib/ptr"
"gorm.io/gorm"
)
type Options struct {
Size int `json:"size"`
Page int `json:"page"`
MaxSize int `json:"maxSize"`
Debug bool `json:"debug"`
}
// Pagination represents a utility type for handling pagination in Go.
//
// Fields:
// - Records: Total number of rows.
// - CurrentPage: Current page loaded.
// - Pages: Total number of pages.
// - Limit: Number of rows per page.
// - First: First page.
// - Last: Last page.
// - PageRange: Range of visible pages.
//
// Methods:
// - SetCurrentPage: Sets the current page based on the provided value. If the value is 0, the current page is set to 1.
// - SetLimit: Sets the limit of rows per page. If the value is 0, the limit is set to the minimum limit of 10. If the limit is less than the minimum limit, it is set to the minimum
type Pagination struct {
Model *gorm.DB `json:"-"`
Executed bool `json:"-"`
Debug bool `json:"-"`
Success bool `json:"success"`
Error *string `json:"error,omitempty"`
Records int `json:"records"` // Total rows
CurrentPage int `json:"current_page"` // Current Page loaded
Pages int `json:"pages"` // total number of pages
Size int `json:"size"` // number of rows per page
MaxSize int `json:"max_size"`
First int `json:"first"` // First Page
Last int `json:"last"` // Last Page
Data interface{} `json:"data"`
}
func (p *Pagination) SetMaxSize(limit int) {
p.MaxSize = limit
}
// setPages sets the total number of pages in the pagination struct based on the number of records and the limit per page.
// If the number of records is 0, it sets the number of pages to 1.
// If there is no remainder when dividing the number of records by the limit, it sets the number of pages to the integer division.
// Otherwise, it sets the number of pages to the integer division plus 1.
// If the number of pages is 0, it sets it to 1.
// After setting the number of pages, it calls the SetLast and SetPageRange methods to update the last page indicator and the range of visible pages respectively.
func (p *Pagination) setPages() {
if p.Records == 0 {
p.Pages = 1
return
}
res := p.Records % p.Size
if res == 0 {
p.Pages = p.Records / p.Size
} else {
p.Pages = (p.Records / p.Size) + 1
}
if p.Pages == 0 {
p.Pages = 1
}
p.Last = p.GetOffset() + p.Size
if p.Last > p.Records {
p.Last = p.Records
}
if p.Size < 10 {
p.Size = 10
}
if p.Size > p.MaxSize {
if p.MaxSize == 0 {
p.MaxSize = 50
}
p.Size = p.MaxSize
}
if p.CurrentPage > 0 {
if p.CurrentPage > p.Pages {
p.CurrentPage = p.Pages
}
} else {
p.CurrentPage = 1
}
}
// GetOffset calculates the offset for paginating the data based on the current page and limit
func (p *Pagination) GetOffset() int {
return (p.GetPage() - 1) * p.Size
}
// GetPage returns the current page of the pagination struct
func (p *Pagination) GetPage() int {
if p.CurrentPage < 1 {
p.CurrentPage = 1
}
return p.CurrentPage
}
func New(model *gorm.DB, request *evo.Request, out interface{}, options ...Options) (*Pagination, error) {
var err error
var p = Pagination{
Model: model,
Size: 10,
}
p.Size = request.Query("size").Int()
p.CurrentPage = request.Query("page").Int()
for _, opt := range options {
if opt.MaxSize > 0 {
p.MaxSize = opt.MaxSize
}
if opt.Size > 0 {
p.Size = opt.Size
}
if opt.Page > 0 {
p.CurrentPage = opt.Page
}
if opt.Debug {
p.Debug = opt.Debug
}
}
n, err := p.LoadData(out)
return n, err
}
func (p *Pagination) LoadData(out interface{}) (*Pagination, error) {
if p.Debug {
p.Model = p.Model.Debug()
}
var total int64
if err := p.Model.Count(&total).Error; err != nil {
return p, err
}
p.Records = int(total)
p.setPages()
p.Model = p.Model.Limit(p.Size)
p.Model = p.Model.Offset(p.GetOffset())
if err := p.Model.Find(out).Error; err != nil {
if err != nil {
p.Error = ptr.String("unable to load data from the database")
}
return p, err
}
p.Success = true
p.Data = out
return p, nil
}
func (p *Pagination) GetResponse() outcome.Response {
var response = outcome.Response{
Data: p,
}
if p.Success {
response.Status(200)
} else {
response.Status(500)
}
response.ContentType = "application/json"
return response
}