-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathpagination.go
83 lines (76 loc) · 2.04 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
package mongopagination
import (
"math"
)
// Paginator struct for holding pagination info
type Paginator struct {
TotalRecord int64 `json:"total_record"`
TotalPage int64 `json:"total_page"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
Page int64 `json:"page"`
PrevPage int64 `json:"prev_page"`
NextPage int64 `json:"next_page"`
}
// PaginationData struct for returning pagination stat
type PaginationData struct {
Total int64 `json:"total"`
Page int64 `json:"page"`
PerPage int64 `json:"perPage"`
Prev int64 `json:"prev"`
Next int64 `json:"next"`
TotalPage int64 `json:"totalPage"`
}
// PaginationData returns PaginationData struct which
// holds information of all stats needed for pagination
func (p *Paginator) PaginationData() *PaginationData {
data := PaginationData{
Total: p.TotalRecord,
Page: p.Page,
PerPage: p.Limit,
Prev: 0,
Next: 0,
TotalPage: p.TotalPage,
}
if p.Page != p.PrevPage && p.TotalRecord > 0 {
data.Prev = p.PrevPage
}
if p.Page != p.NextPage && p.TotalRecord > 0 && p.Page <= p.TotalPage {
data.Next = p.NextPage
}
return &data
}
// Paging returns Paginator struct which hold pagination
// stats
func Paging(p *pagingQuery, paginationInfo chan<- *Paginator, aggregate bool, aggCount int64) {
var paginator Paginator
var offset int64
var count int64
ctx := p.getContext()
if !aggregate {
count, _ = p.Collection.CountDocuments(ctx, p.FilterQuery)
} else {
count = aggCount
}
if p.PageCount > 0 {
offset = (p.PageCount - 1) * p.LimitCount
} else {
offset = 0
}
paginator.TotalRecord = count
paginator.Page = p.PageCount
paginator.Offset = offset
paginator.Limit = p.LimitCount
paginator.TotalPage = int64(math.Ceil(float64(count) / float64(p.LimitCount)))
if p.PageCount > 1 {
paginator.PrevPage = p.PageCount - 1
} else {
paginator.PrevPage = p.PageCount
}
if p.PageCount == paginator.TotalPage {
paginator.NextPage = p.PageCount
} else {
paginator.NextPage = p.PageCount + 1
}
paginationInfo <- &paginator
}