-
Notifications
You must be signed in to change notification settings - Fork 13
/
list.go
230 lines (198 loc) · 5.31 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
package crud
import (
"fmt"
"strings"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/latolukasz/beeorm"
)
type SearchParams struct {
Page int
PageSize int
Search map[string]string
StringFilters map[string]string
NumberFilters map[string]int64
BooleanFilters map[string]bool
Sort map[string]bool
}
type Column struct {
Key string
Label string
Type string
Searchable bool
Sortable bool
Filterable bool
Visible bool
FilterValidMap []FilterValue
}
type FilterValue struct {
Key string
Label string
}
type ListRequest struct {
Page *int
PageSize *int
Filter map[string]interface{}
Search map[string]interface{}
Sort map[string]interface{}
}
type Crud struct {
}
func (c *Crud) ExtractListParams(cols []Column, request *ListRequest) SearchParams {
finalPage := 1
finalPageSize := 10
if request.Page != nil && *request.Page > 0 {
finalPage = *request.Page
}
if request.PageSize != nil && *request.PageSize > 0 {
finalPageSize = *request.PageSize
}
var searchable = make([]string, 0)
var stringFilters = make([]string, 0)
var booleanFilters = make([]string, 0)
var stringEnumFilters = make(map[string][]FilterValue)
var numberFilters = make([]string, 0)
var sortables = make([]string, 0)
for i := range cols {
if cols[i].Sortable {
sortables = append(sortables, cols[i].Key)
}
if cols[i].Searchable {
searchable = append(searchable, cols[i].Key)
continue
}
if cols[i].Filterable && cols[i].Type == StringType {
stringFilters = append(stringFilters, cols[i].Key)
continue
}
if cols[i].Filterable && cols[i].Type == BooleanType {
booleanFilters = append(booleanFilters, cols[i].Key)
continue
}
if cols[i].Filterable && cols[i].Type == NumberType {
numberFilters = append(numberFilters, cols[i].Key)
continue
}
if cols[i].Filterable && cols[i].Type == EnumType {
stringEnumFilters[cols[i].Key] = cols[i].FilterValidMap
continue
}
}
var selectedStringFilters = make(map[string]string)
var selectedNumberFilters = make(map[string]int64)
var selectedBooleanFilters = make(map[string]bool)
var selectedSort = make(map[string]bool)
var selectedSearches = make(map[string]string)
mainLoop:
for field, value := range request.Filter {
intValue, ok := value.(int64)
if ok {
if helper.StringInArray(field, numberFilters...) {
selectedNumberFilters[field] = intValue
continue mainLoop
}
}
booleanValue, ok := value.(bool)
if ok {
if helper.StringInArray(field, booleanFilters...) {
selectedBooleanFilters[field] = booleanValue
continue mainLoop
}
}
stringValue, ok := value.(string)
if ok {
if helper.StringInArray(field, stringFilters...) {
selectedStringFilters[field] = stringValue
continue mainLoop
}
for enumFiledName := range stringEnumFilters {
if field == enumFiledName {
for _, filterValue := range stringEnumFilters[enumFiledName] {
if filterValue.Key == stringValue {
selectedStringFilters[field] = stringValue
continue mainLoop
}
}
}
}
}
}
for field, value := range request.Search {
stringValue, ok := value.(string)
if ok && len(stringValue) >= 2 {
if helper.StringInArray(field, searchable...) {
selectedSearches[field] = stringValue
}
}
}
if len(request.Sort) == 1 {
for field, mode := range request.Sort {
stringVal := mode.(string)
if helper.StringInArray(field, sortables...) && helper.StringInArray(stringVal, "asc", "desc") {
selectedSort[field] = stringVal == "asc"
}
}
}
return SearchParams{
Page: finalPage,
PageSize: finalPageSize,
Search: selectedSearches,
StringFilters: selectedStringFilters,
NumberFilters: selectedNumberFilters,
BooleanFilters: selectedBooleanFilters,
Sort: selectedSort,
}
}
// 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.StringFilters {
query.FilterTag(field, value)
}
for field, value := range params.BooleanFilters {
query.FilterBool(field, value)
}
// TODO : use full text search
for field, value := range params.Search {
query.QueryRaw(fmt.Sprintf(
"@%s:%v* ",
field, strings.TrimSpace(beeorm.EscapeRedisSearchString(value)),
))
}
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(field+" = ?", value)
}
for field, value := range params.StringFilters {
where.Append(field+" = ?", value)
}
for field, value := range params.BooleanFilters {
where.Append(field+" = ?", value)
}
// TODO : use full text search
for field, value := range params.Search {
where.Append(field+" LIKE ?", value+"%")
}
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
}