-
Notifications
You must be signed in to change notification settings - Fork 1
/
visits.go
156 lines (134 loc) · 3.65 KB
/
visits.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
package main
import (
"github.com/valyala/fasthttp"
"net/http"
"strconv"
"strings"
)
type Visit struct {
Id int `json:"id"`
Location int `json:"location"`
User int `json:"user"`
VisitedAt int `json:"visited_at"`
Mark int `json:"mark"`
}
func NewVisit(id int) *Visit {
return &Visit{
Id: id,
}
}
var ANSWER_OK = []byte("{}")
func newVisit(ctx *fasthttp.RequestCtx) {
data := ctx.PostBody()
body := NewVisit(0)
err := body.UnmarshalJSON(data)
if err != nil {
//Log.Warnf("Cannot parse JSON in request. Reason %s", err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
err = DB.NewVisit(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 getVisit(ctx *fasthttp.RequestCtx, id int) {
visit, err := DB.GetVisit(id)
if err == NotFound {
//Log.Warnf("Not found")
writeAnswer(ctx, http.StatusNotFound, []byte{})
return
}
if err != nil {
Log.Errorf("Cannot get id %s. Reason %s", id, err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("cannot get id"))
return
}
result, err := visit.MarshalJSON()
if err != nil {
Log.Errorf("Cannot marshal visit %#v. Reason %s", visit, err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot marshal visit"))
return
}
writeAnswer(ctx, http.StatusOK, result)
}
func updateVisit(ctx *fasthttp.RequestCtx, id int) {
data := ctx.PostBody()
visit, err := DB.GetVisit(id)
if err == NotFound {
//Log.Infof("Not found")
writeAnswer(ctx, http.StatusNotFound, []byte{})
return
}
if err != nil {
Log.Errorf("Cannot get visit with id %d. Reason %s", id, err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot get visit"))
return
}
oldUser := visit.User
oldLocation := visit.Location
err = visit.UnmarshalJSON(data)
if err != nil {
//Log.Warnf("Cannot parse JSON in request. Reason %s", err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
//UPDATE USER
if visit.User != oldUser {
usrOld, err := DB.GetUser(oldUser)
if err != nil {
//Log.Errorf("Cannot get user %d. Reason %s", oldUser, err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
usrOld.Visits.Remove(visit.Id)
usr, err := DB.GetUser(visit.User)
if err != nil {
//Log.Errorf("Cannot get user %d. Reason %s", visit.User, err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
usr.Visits.Add(visit.Id)
}
//UPDATE LOCATION
if visit.Location != oldLocation {
locOld, err := DB.GetLocation(oldLocation)
if err != nil {
//Log.Errorf("Cannot get location %d. Reason %s", oldLocation, err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
locOld.Visits.Remove(visit.Id)
loc, err := DB.GetLocation(visit.Location)
if err != nil {
//Log.Errorf("Cannot get location %d. Reason %s", visit.Location, err)
writeAnswer(ctx, http.StatusBadRequest, []byte{})
return
}
loc.Visits.Add(visit.Id)
}
err = DB.UpdateVisit(visit, id)
if err != nil {
Log.Errorf("Cannot update visit. Reason %s", err)
writeAnswer(ctx, http.StatusInternalServerError, generateError("Cannot update visit"))
return
}
writeAnswer(ctx, http.StatusOK, ANSWER_OK)
}
func processVisit(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 string(ctx.Method()) == "GET" {
getVisit(ctx, id)
} else {
updateVisit(ctx, id)
}
}