-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
183 lines (156 loc) · 4.19 KB
/
users.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
package main
import (
"github.com/valyala/fasthttp"
"net/http"
"strconv"
"strings"
)
type User struct {
Id int `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Gender string `json:"gender"`
Birthdate int `json:"birth_date"`
Visits Array `json:"-"`
}
type UserVisits struct {
VisitedAt int `json:"visited_at"`
Mark int `json:"mark"`
Place string `json:"place"`
}
type UserVisitsArray struct {
Visits []UserVisits `json:"visits"`
}
type UserVisitsSorter struct {
Data []UserVisits
}
func (s UserVisitsSorter) Len() int {
return len(s.Data)
}
func (s UserVisitsSorter) Swap(i, j int) {
s.Data[i], s.Data[j] = s.Data[j], s.Data[i]
}
func (s UserVisitsSorter) Less(i, j int) bool {
return s.Data[i].VisitedAt < s.Data[j].VisitedAt
}
func NewUser(id int) *User {
return &User{
Id: id,
Visits: NewArray(),
}
}
func newUser(ctx *fasthttp.RequestCtx) {
data := ctx.PostBody()
body := NewUser(0)
err := body.UnmarshalJSON(data)
if err != nil {
//Log.Warnf("Cannot parse JSON in request. Reason %s", err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
//Log.Infof("Visits after %p", body.Visits)
err = DB.NewUser(body)
if err != nil {
Log.Errorf("Cannot set id %d. Reason %s", body.Id, err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot set id"))
return
}
writeAnswer(ctx, http.StatusOK, ANSWER_OK)
}
func getUserVisits(ctx *fasthttp.RequestCtx, id int) {
filters := ctx.QueryArgs()
//Log.Infof("Getting user %s visits with filters %#v", id, filters)
vals, err := DB.GetVisitsFilter(id, filters)
switch err {
case NotFound:
//Log.Infof("Not found")
writeAnswer(ctx, http.StatusNotFound, []byte{})
return
case CannotParse:
//Log.Infof("Cannot parse")
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
case nil:
break
default:
Log.Errorf("Error while getting user visits. Reason %s", err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Error while getting user visits"))
return
}
visitsArray := UserVisitsArray{
Visits: vals,
}
result, err := visitsArray.MarshalJSON()
if err != nil {
Log.Errorf("Cannot marshal answer. Reason %s", err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot marshal answer"))
return
}
writeAnswer(ctx, http.StatusOK, result)
}
func getUser(ctx *fasthttp.RequestCtx, id int) {
result, err := DB.GetUser(id)
if err == NotFound {
//Log.Infof("Not found")
writeAnswer(ctx, http.StatusNotFound, []byte{})
return
}
if err != nil {
Log.Errorf("Cannot get id %d. Reason %s", id, err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("cannot get id"))
return
}
answer, err := result.MarshalJSON()
if err != nil {
Log.Errorf("Cannot marshal user %d. Reason %s", id, err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot marshal user"))
return
}
writeAnswer(ctx, http.StatusOK, answer)
}
func updateUser(ctx *fasthttp.RequestCtx, id int) {
data := ctx.PostBody()
user, err := DB.GetUser(id)
if err == NotFound {
//Log.Infof("Not found")
writeAnswer(ctx, http.StatusNotFound, []byte{})
return
}
if err != nil {
Log.Errorf("Cannot get user with id %d. Reason %s", id, err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot get user"))
return
}
err = user.UnmarshalJSON(data)
if err != nil {
//Log.Warnf("Cannot parse JSON in request. Reason %s", err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
err = DB.UpdateUser(user, id)
if err != nil {
Log.Errorf("Cannot update user. Reason %s", err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot update user"))
return
}
writeAnswer(ctx, http.StatusOK, ANSWER_OK)
}
func processUser(ctx *fasthttp.RequestCtx) {
path := strings.Split(string(ctx.Path()), "/")
id, err := strconv.Atoi(path[2])
if err != nil {
//Log.Infof("Cannot parse id %s. Reason %s", path[2], err)
writeAnswer(ctx, http.StatusNotFound, []byte{})
return
}
if len(path) == 4 {
getUserVisits(ctx, id)
return
}
if string(ctx.Method()) == "GET" {
getUser(ctx, id)
} else {
updateUser(ctx, id)
}
}