-
Notifications
You must be signed in to change notification settings - Fork 0
/
pager.go
88 lines (74 loc) · 1.65 KB
/
pager.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
package gormer
import "github.com/jinzhu/gorm"
// PageParam uniform paging parameters
type PageParam struct {
CurrPage int `json:"curr_page" binding:"number"` // (optional) current page number, default 1
PageSize int `json:"page_size" binding:"number"` // (optional) number of per page, default 10
IgnorePage int `json:"ignore_page,omitempty"` // (optional) no paging and no total count, while not 0
}
// Init init param
func (p *PageParam) Init() {
if p == nil {
p = &PageParam{
PageSize: 10,
CurrPage: 1,
}
}
if p.CurrPage <= 0 {
p.CurrPage = 1
}
if p.PageSize <= 0 {
p.PageSize = 10
}
}
// Page pager scope
func (p *PageParam) Page() func(db *gorm.DB) *gorm.DB {
// ignore page and count
if p.IgnorePage != 0 {
return func(db *gorm.DB) *gorm.DB {
return db
}
}
p.Init()
offset := (p.CurrPage - 1) * p.PageSize
return func(db *gorm.DB) *gorm.DB {
if offset > 0 {
db = db.Offset(offset)
}
if p.PageSize > 0 {
db = db.Limit(p.PageSize)
}
return db
}
}
// PageResult unified paging response structure
type PageResult struct {
PageParam
TotalCount int `json:"total_count"`
CurrCount int `json:"curr_count"`
}
// Count count result
func (p *PageResult) Count(db *gorm.DB) error {
if p.IgnorePage != 0 {
return nil
}
return db.Count(&p.TotalCount).Error
}
// Scan scan result and count
func (p *PageResult) Scan(db *gorm.DB, dest interface{}) (err error) {
db = db.Scan(dest)
err = db.Error
if err == gorm.ErrRecordNotFound {
err = nil
}
if err != nil {
return
}
p.CurrCount = int(db.RowsAffected)
if p.IgnorePage != 0 {
p.TotalCount = p.CurrCount
} else {
return p.Count(db)
}
return
}