-
Notifications
You must be signed in to change notification settings - Fork 13
/
list.go
442 lines (365 loc) · 12.2 KB
/
list.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package crud
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/helper"
)
type SearchParams struct {
Page int
PageSize int
StringORFilters map[string]string
StringFilters map[string]string
TagFilters map[string]string
ArrayStringFilters map[string][]string
NumberFilters map[string]int64
ArrayNumberFilters map[string][]int64
RangeNumberFilters map[string][]int64
DateTimeFilters map[string]time.Time
DateFilters map[string]time.Time
RangeDateTimeFilters map[string][]time.Time
RangeDateFilters map[string][]time.Time
BooleanFilters map[string]bool
Sort map[string]bool
}
type Column struct {
Key string
Label string
FilterType string
Searchable bool
Sortable bool
Visible bool
DataStringKeyStringValue []StringKeyStringValue
DataIntKeyStringValue []IntKeyStringValue
}
type IntKeyStringValue struct {
Key uint64
Label string
}
type StringKeyStringValue struct {
Key string
Label string
}
type ListRequest struct {
Page *int
PageSize *int
Search map[string]interface{}
SearchOR map[string]interface{}
Sort map[string]interface{}
}
type Crud struct {
}
func (c *Crud) ExtractListParams(cols []Column, request *ListRequest) SearchParams {
finalPage := 1
finalPageSize := 20
if request.Page != nil && *request.Page > 0 {
finalPage = *request.Page
}
if request.PageSize != nil && *request.PageSize > 0 {
finalPageSize = *request.PageSize
}
var stringStartsWithSearch = make([]string, 0)
var arrayStringFilters = make([]string, 0)
var booleanFilters = make([]string, 0)
var mapStringStringFilters = make(map[string][]StringKeyStringValue)
var mapIntStringFilters = make(map[string][]IntKeyStringValue)
var numberFilters = make([]string, 0)
var rangeNumberFilters = make([]string, 0)
var arrayNumberFilters = make([]string, 0)
var dateTimeFilters = make([]string, 0)
var dateFilters = make([]string, 0)
var rangeDateTimeFilters = make([]string, 0)
var rangeDateFilters = make([]string, 0)
var sortables = make([]string, 0)
for _, column := range cols {
if column.Sortable {
sortables = append(sortables, column.Key)
}
if column.Searchable {
switch column.FilterType {
case InputTypeString:
stringStartsWithSearch = append(stringStartsWithSearch, column.Key)
case ArrayStringType:
arrayStringFilters = append(arrayStringFilters, column.Key)
case CheckboxTypeBoolean:
booleanFilters = append(booleanFilters, column.Key)
case RangeSliderTypeArrayNumber:
rangeNumberFilters = append(rangeNumberFilters, column.Key)
case MultiSelectTypeArrayNumber:
arrayNumberFilters = append(arrayNumberFilters, column.Key)
case InputTypeNumber:
numberFilters = append(numberFilters, column.Key)
case SelectTypeStringString:
mapStringStringFilters[column.Key] = column.DataStringKeyStringValue
case SelectTypeIntString:
mapIntStringFilters[column.Key] = column.DataIntKeyStringValue
case DateTimePickerTypeDateTime:
dateTimeFilters = append(dateTimeFilters, column.Key)
case DatePickerTypeDate:
dateFilters = append(dateFilters, column.Key)
case RangeDateTimePickerTypeArrayDateTime:
rangeDateTimeFilters = append(rangeDateTimeFilters, column.Key)
case RangeDatePickerTypeArrayDate:
rangeDateFilters = append(rangeDateFilters, column.Key)
}
}
}
var selectedMapStringStringFilters = make(map[string]string)
var selectedStringStartsWithFilters = make(map[string]string)
var selectedArrayStringFilters = make(map[string][]string)
var selectedNumberFilters = make(map[string]int64)
var selectedRangeNumberFilters = make(map[string][]int64, 2)
var selectedArrayNumberFilters = make(map[string][]int64)
var selectedDateTimeFilters = make(map[string]time.Time)
var selectedDateFilters = make(map[string]time.Time)
var selectedRangeDateTimeFilters = make(map[string][]time.Time)
var selectedRangeDateFilters = make(map[string][]time.Time)
var selectedBooleanFilters = make(map[string]bool)
var selectedSort = make(map[string]bool)
var selectedORFilters = make(map[string]string)
mainLoop:
for field, value := range request.Search {
switch reflect.TypeOf(value).Kind() {
case reflect.Int64:
if helper.StringInArray(field, numberFilters...) {
selectedNumberFilters[field] = value.(int64)
continue mainLoop
}
case reflect.Float64:
if helper.StringInArray(field, numberFilters...) {
selectedNumberFilters[field] = int64(value.(float64))
continue mainLoop
}
for selectFiledName := range mapIntStringFilters {
if field == selectFiledName {
for _, filterValue := range mapIntStringFilters[selectFiledName] {
if int64(filterValue.Key) == int64(value.(float64)) {
selectedNumberFilters[field] = int64(value.(float64))
continue mainLoop
}
}
}
}
case reflect.Bool:
if helper.StringInArray(field, booleanFilters...) {
selectedBooleanFilters[field] = value.(bool)
continue mainLoop
}
case reflect.Slice:
s := reflect.ValueOf(value)
if helper.StringInArray(field, rangeNumberFilters...) {
if s.Len() != 2 {
continue mainLoop
}
minRange, _ := strconv.ParseInt(fmt.Sprintf("%v", s.Index(0)), 10, 64)
maxRange, _ := strconv.ParseInt(fmt.Sprintf("%v", s.Index(1)), 10, 64)
selectedRangeNumberFilters[field] = []int64{minRange, maxRange}
} else if helper.StringInArray(field, rangeDateTimeFilters...) {
if s.Len() != 2 {
continue mainLoop
}
minRange, _ := time.Parse(helper.TimeLayoutRFC3339Milli, fmt.Sprintf("%v", s.Index(0)))
maxRange, _ := time.Parse(helper.TimeLayoutRFC3339Milli, fmt.Sprintf("%v", s.Index(1)))
selectedRangeDateTimeFilters[field] = []time.Time{minRange, maxRange}
} else if helper.StringInArray(field, rangeDateFilters...) {
if s.Len() != 2 {
continue mainLoop
}
minRange, _ := time.Parse(helper.TimeLayoutYMD, fmt.Sprintf("%v", s.Index(0)))
maxRange, _ := time.Parse(helper.TimeLayoutYMD, fmt.Sprintf("%v", s.Index(1)))
selectedRangeDateFilters[field] = []time.Time{minRange, maxRange}
} else if helper.StringInArray(field, arrayNumberFilters...) {
if s.Len() == 0 {
continue mainLoop
}
for i := 0; i < s.Len(); i++ {
int64Value, _ := strconv.ParseInt(fmt.Sprintf("%v", s.Index(i)), 10, 64)
selectedArrayNumberFilters[field] = append(selectedArrayNumberFilters[field], int64Value)
}
} else if helper.StringInArray(field, arrayStringFilters...) {
if s.Len() == 0 {
continue mainLoop
}
for i := 0; i < s.Len(); i++ {
selectedArrayStringFilters[field] = append(selectedArrayStringFilters[field], fmt.Sprintf("%v", s.Index(i)))
}
}
continue mainLoop
case reflect.String:
jsonIntValue, ok := value.(json.Number)
if ok {
jsonInt, err := jsonIntValue.Int64()
if err == nil {
if helper.StringInArray(field, numberFilters...) {
selectedNumberFilters[field] = jsonInt
continue mainLoop
}
}
}
stringValue := value.(string)
if stringValue == "" {
continue mainLoop
}
if helper.StringInArray(field, dateTimeFilters...) {
dateTimeValue, _ := time.Parse(helper.TimeLayoutRFC3339Milli, stringValue)
selectedDateTimeFilters[field] = dateTimeValue
continue mainLoop
}
if helper.StringInArray(field, dateFilters...) {
dateValue, _ := time.Parse(helper.TimeLayoutYMD, stringValue)
selectedDateFilters[field] = dateValue
continue mainLoop
}
for selectFiledName := range mapStringStringFilters {
if field == selectFiledName {
for _, filterValue := range mapStringStringFilters[selectFiledName] {
if filterValue.Key == stringValue {
selectedMapStringStringFilters[field] = stringValue
continue mainLoop
}
}
}
}
if helper.StringInArray(field, stringStartsWithSearch...) {
selectedStringStartsWithFilters[field] = stringValue
continue
}
}
}
for field, value := range request.SearchOR {
stringValue, ok := value.(string)
if ok && len(stringValue) >= 2 {
if helper.StringInArray(field, stringStartsWithSearch...) {
selectedORFilters[field] = stringValue
}
}
}
if len(request.Sort) == 1 {
for field, mode := range request.Sort {
stringVal, ok := mode.(string)
if ok && helper.StringInArray(field, sortables...) && helper.StringInArray(stringVal, "asc", "desc") {
selectedSort[field] = stringVal == "asc"
}
}
}
return SearchParams{
Page: finalPage,
PageSize: finalPageSize,
TagFilters: selectedMapStringStringFilters,
StringORFilters: selectedORFilters,
StringFilters: selectedStringStartsWithFilters,
ArrayStringFilters: selectedArrayStringFilters,
NumberFilters: selectedNumberFilters,
ArrayNumberFilters: selectedArrayNumberFilters,
RangeNumberFilters: selectedRangeNumberFilters,
DateTimeFilters: selectedDateTimeFilters,
DateFilters: selectedDateFilters,
RangeDateTimeFilters: selectedRangeDateTimeFilters,
RangeDateFilters: selectedRangeDateFilters,
BooleanFilters: selectedBooleanFilters,
Sort: selectedSort,
}
}
// GenerateListRedisSearchQuery TODO : add full text queries when supported by hitrix
func (c *Crud) GenerateListRedisSearchQuery(params SearchParams) *beeorm.RedisSearchQuery {
query := &beeorm.RedisSearchQuery{}
for field, value := range params.NumberFilters {
query.FilterInt(field, value)
}
for field, value := range params.ArrayNumberFilters {
query.FilterInt(field, value...)
}
for field, value := range params.RangeNumberFilters {
query.FilterIntMinMax(field, value[0], value[1])
}
for field, value := range params.DateTimeFilters {
query.FilterDateTime(field, value)
}
for field, value := range params.DateFilters {
query.FilterDate(field, value)
}
for field, value := range params.RangeDateTimeFilters {
query.FilterDateTimeMinMax(field, value[0], value[1])
}
for field, value := range params.RangeDateFilters {
query.FilterDateMinMax(field, value[0], value[1])
}
for field, value := range params.TagFilters {
query.FilterTag(field, value)
}
for field, value := range params.StringFilters {
query.QueryFieldPrefixMatch(field, value)
}
for field, value := range params.ArrayStringFilters {
query.FilterTag(field, value...)
}
for field, value := range params.BooleanFilters {
query.FilterBool(field, value)
}
orStatements := make([]string, 0)
for field, value := range params.StringORFilters {
if strings.TrimSpace(value) == "" {
continue
}
orStatements = append(orStatements, fmt.Sprintf(
"(@%s:%v*)",
field, strings.TrimSpace(beeorm.EscapeRedisSearchString(value)),
))
}
if len(orStatements) > 0 {
query.AppendQueryRaw("(" + strings.Join(orStatements, "|") + ")")
}
if len(params.Sort) == 1 {
for field, mode := range params.Sort {
query.Sort(field, !mode)
}
}
return query
}
func (c *Crud) GenerateListMysqlQuery(params SearchParams) *beeorm.Where {
where := beeorm.NewWhere("1")
for field, value := range params.NumberFilters {
where.Append("AND "+field+" = ?", value)
}
for field, value := range params.TagFilters {
where.Append("AND "+field+" = ?", value)
}
for field, value := range params.BooleanFilters {
where.Append("AND "+field+" = ?", value)
}
// TODO : use full text search
for field, value := range params.StringFilters {
where.Append("AND "+field+" LIKE ?", value+"%")
}
orStatements := make([]string, 0)
orStatementsVariables := make([]interface{}, 0)
for field, value := range params.StringORFilters {
orStatements = append(orStatements, fmt.Sprintf(
"%s LIKE ?",
field,
))
orStatementsVariables = append(orStatementsVariables, value+"%")
}
if len(orStatements) > 0 {
where.Append(
"AND ("+strings.Join(orStatements, " OR ")+")",
orStatementsVariables...,
)
}
if len(params.Sort) == 1 {
sortQuery := "ORDER BY "
for field, mode := range params.Sort {
sort := "ASC"
if !mode {
sort = "DESC"
}
sortQuery += field + " " + sort
}
where.Append(sortQuery)
}
return where
}