-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountsnewhandler.go
74 lines (60 loc) · 1.39 KB
/
accountsnewhandler.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
package main
import (
"encoding/json"
routing "github.com/qiangxue/fasthttp-routing"
)
type AccountsNewHandler struct {
storage Storage
}
var allowedFields = map[string]int{
"id": 1,
"fname": 1,
"sname": 1,
"phone": 1,
"email": 1,
"sex": 1,
"birth": 1,
"country": 1,
"city": 1,
"joined": 1,
"interests": 1,
"status": 1,
"premium": 1,
"likes": 1,
}
func (handler *AccountsNewHandler) ServeHTTP(c *routing.Context) error {
body := c.PostBody()
var rawAccount map[string]interface{}
err := json.Unmarshal(body, &rawAccount)
if err != nil {
return &Error{400, "unmarshall error"}
}
for key, _ := range rawAccount {
if _, ok := allowedFields[key]; !ok {
return &Error{400, "bad field"}
}
}
if v, ok := rawAccount["sex"]; !ok || (v != "f" && v != "m") {
return &Error{400, "bad sex"}
}
if v, ok := rawAccount["status"]; !ok || ParseStatus(v.(string)) == 0 {
return &Error{400, "bad status"}
}
if _, ok := rawAccount["birth"]; !ok {
return &Error{400, "bad birth"}
}
if _, ok := rawAccount["email"]; !ok {
return &Error{400, "bad email"}
}
var account Account
err = json.Unmarshal(body, &account)
if err != nil {
return &Error{400, "unmarshall error"}
}
EnrichAccount(&account, handler.storage.GetNow())
err = handler.storage.LoadAccounts([]Account{account})
if err != nil {
return err
}
return nil
}