-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.go
200 lines (162 loc) · 4.1 KB
/
params.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package gormx
import (
"fmt"
"strings"
"github.com/go-zoox/zoox"
)
// Params is the interface that wraps the basic methods.
type Params struct {
ctx *zoox.Context
//
page *Page
}
// NewParams returns the params.
func NewParams(ctx *zoox.Context) *Params {
return &Params{
ctx: ctx,
}
}
func (c *Params) parsePage() error {
if c.page != nil {
return nil
}
c.page = &Page{}
if err := c.ctx.BindQuery(c.page); err != nil {
return err
}
if c.page.PageSize == 0 {
c.page.PageSize = 10
}
if c.page.Page == 0 {
c.page.Page = 1
}
if c.page.Page > 1000 {
c.page.Page = 1000
}
if c.page.PageSize > 100 {
c.page.PageSize = 100
}
return nil
}
// Page is the struct that wraps the basic fields.
func (c *Params) Page() (uint, error) {
if err := c.parsePage(); err != nil {
return 0, err
}
return uint(c.page.Page), nil
}
// PageSize is the struct that wraps the basic fields.
func (c *Params) PageSize() (uint, error) {
if err := c.parsePage(); err != nil {
return 0, err
}
return uint(c.page.PageSize), nil
}
// ID is the struct that wraps the basic fields.
func (c *Params) ID() (uint, error) {
id := c.ctx.Param().Get("id").Int64()
if id == 0 {
return 0, fmt.Errorf("invalid id: %s", c.ctx.Param().Get("id").String())
}
return uint(id), nil
}
// Where is the struct that wraps the basic fields.
func (c *Params) Where() *Where {
var where Where
whereObject := c.ctx.Queries()
whereObject.Del("page")
whereObject.Del("pageSize")
whereObject.Del("orderBy")
whereObject.Del("page-size")
whereObject.Del("order-by")
for key, value := range whereObject.Iterator() {
if vs, ok := value.(string); ok {
if strings.Contains(vs, ":") {
vs := strings.Split(vs, ":")
pattern := vs[len(vs)-1]
value := strings.Join(vs[0:len(vs)-1], ":")
if pattern == "*" {
where.Set(key, value, &SetWhereOptions{IsFuzzy: true})
} else if pattern == "!" {
where.Set(key, value, &SetWhereOptions{IsNotEqual: true})
} else if pattern == "in" {
where.Set(key, strings.Split(value, ","), &SetWhereOptions{IsIn: true})
} else if pattern == "!in" {
where.Set(key, strings.Split(value, ","), &SetWhereOptions{IsNotIn: true})
}
} else {
where.Set(key, vs)
}
} else {
where.Set(key, vs)
}
}
return &where
}
// OrderBy is the struct that wraps the basic fields.
func (c *Params) OrderBy() *OrderBy {
var orderBy OrderBy
orderByRaw := c.ctx.Query().Get("orderBy").String()
if orderByRaw == "" {
orderByRaw = c.ctx.Query().Get("order-by").String()
}
if orderByRaw != "" {
orderByRaws := strings.Split(orderByRaw, ",")
for _, one := range orderByRaws {
one = strings.TrimSpace(one)
if one == "" {
continue
}
orderByRaws := strings.Split(one, ":")
if len(orderByRaws) != 2 {
continue
}
key := orderByRaws[0]
order := strings.ToLower(orderByRaws[1])
isDESC := false
if order == "desc" {
isDESC = true
} else if order == "DESC" {
isDESC = true
}
orderBy.Set(key, isDESC)
}
}
return &orderBy
}
// ListParams is the struct that wraps the basic fields.
type ListParams struct {
Page uint
PageSize uint
Where *Where
OrderBy *OrderBy
}
// ListParamsDefault is the struct that wraps the basic fields.
type ListParamsDefault struct {
Page uint
PageSize uint
}
// GetList is the struct that wraps the basic fields.
func (c *Params) GetList(defaults ...*ListParamsDefault) (*ListParams, error) {
var defaultsX *ListParamsDefault
if len(defaults) > 0 && defaults[0] != nil {
defaultsX = defaults[0]
}
var listParams ListParams
var err error
listParams.Page, err = c.Page()
if err != nil {
return nil, fmt.Errorf("parse page error: %s", err)
} else if defaultsX != nil && listParams.Page == 0 && defaultsX.Page != 0 {
listParams.Page = defaultsX.Page
}
listParams.PageSize, err = c.PageSize()
if err != nil {
return nil, fmt.Errorf("parse page size error: %s", err)
} else if defaultsX != nil && listParams.PageSize == 0 && defaultsX.PageSize != 0 {
listParams.PageSize = defaultsX.PageSize
}
listParams.Where = c.Where()
listParams.OrderBy = c.OrderBy()
return &listParams, nil
}