-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountsgroupquery.go
92 lines (82 loc) · 1.8 KB
/
accountsgroupquery.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
package main
import (
"fmt"
"strconv"
"strings"
)
var validKeys = map[string]int{
"sex": 1,
"status": 1,
"interests": 1,
"country": 1,
"city": 1,
}
// 2 means int should be sent to mongo
var allowedFilters = map[string]int{
"email": 1,
"sex": 1,
"status": 1,
"fname": 1,
"sname": 1,
"phone": 1,
"country": 1,
"city": 1,
"birth": 1, // year
"interests": 1, // one string
"likes": 1, // one id
// "premium": 1,
"joined": 1, // year
}
type AccountsGroupQuery struct {
Keys []string
Filters map[string]string
Limit int
Order int
}
func CreateAccountsGroupQuery(query map[string]string) (accountsGroupQuery AccountsGroupQuery, err error) {
delete(query, "query_id")
limit, ok := query["limit"]
if !ok {
err = &Error{400, "no limit"}
return
}
accountsGroupQuery.Limit, err = strconv.Atoi(limit)
if err != nil || accountsGroupQuery.Limit <= 0 {
err = &Error{400, fmt.Sprint("Bad limit", limit)}
return
}
delete(query, "limit")
order, ok := query["order"]
if !ok {
err = &Error{400, "No order"}
return
}
accountsGroupQuery.Order, err = strconv.Atoi(order)
if err != nil {
err = &Error{400, fmt.Sprint("Bad order", order)}
return
}
delete(query, "order")
keys, ok := query["keys"]
if !ok {
err = &Error{400, "No keys"}
return
}
accountsGroupQuery.Keys = strings.Split(keys, ",")
for _, key := range accountsGroupQuery.Keys {
if _, ok := validKeys[key]; !ok {
err = &Error{400, fmt.Sprint("Bad key", key)}
return
}
}
delete(query, "keys")
accountsGroupQuery.Filters = make(map[string]string)
for field, values := range query {
if _, ok := allowedFilters[field]; !ok {
err = &Error{400, fmt.Sprint("Bad filter", field)}
return
}
accountsGroupQuery.Filters[field] = values
}
return
}