-
Notifications
You must be signed in to change notification settings - Fork 46
/
filter.go
71 lines (63 loc) · 1.36 KB
/
filter.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
package candishared
// Filter data
type Filter struct {
Limit int `json:"limit" default:"10"`
Page int `json:"page" default:"1"`
Offset int `json:"-"`
Search string `json:"search,omitempty"`
OrderBy string `json:"orderBy,omitempty"`
Sort string `json:"sort,omitempty" default:"desc" lower:"true"`
ShowAll bool `json:"showAll"`
AllowEmptyFilter bool `json:"-"`
}
// CalculateOffset method
func (f *Filter) CalculateOffset() int {
f.Offset = (f.Page - 1) * f.Limit
return f.Offset
}
// GetPage method
func (f *Filter) GetPage() int {
return f.Page
}
// IncrPage method
func (f *Filter) IncrPage() {
f.Page++
}
// GetLimit method
func (f *Filter) GetLimit() int {
return f.Limit
}
// NullableFilter filter contains nullable value
type NullableFilter struct {
Limit *int
Page *int
Search *string
Sort *string
ShowAll *bool
OrderBy *string
}
func (n *NullableFilter) ToFilter() (filter Filter) {
if n.Search != nil {
filter.Search = *n.Search
}
if n.OrderBy != nil {
filter.OrderBy = *n.OrderBy
}
if n.Sort != nil {
filter.Sort = *n.Sort
}
if n.ShowAll != nil {
filter.ShowAll = *n.ShowAll
}
if n.Limit == nil {
filter.Limit = 10
} else {
filter.Limit = *n.Limit
}
if n.Page == nil {
filter.Page = 1
} else {
filter.Page = *n.Page
}
return filter
}