-
Notifications
You must be signed in to change notification settings - Fork 2
/
post_handlers.cpp
168 lines (157 loc) · 4.5 KB
/
post_handlers.cpp
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "post_handlers.h"
#include "db_instance.h"
#include "helpers.h"
#include "http_parser.h"
#include "multi_index.h"
#include "simple_index.h"
#include "structures.h"
namespace post_handlers {
void UpdateUsers(const http_request& req, http_response& res, uint32_t id) {
User* user = g_users_storage->Get(id);
if (!user) {
NotFound(HTTP_POST, res);
return;
}
User new_user = *user;
int update_flags = 0;
if (!User::Parse(req.body, req.body_length, &new_user, &update_flags)) {
BadRequest(HTTP_POST, res);
return;
}
if (update_flags & UpdateFlags::UPDATE_EMAIL) {
g_db_instance->GetEmailIndex()->Replace(
user->email, new_user.email);
}
std::swap(*user, new_user);
user->Serialize(id);
SendOk(res);
}
void UpdateLocations(const http_request& req, http_response& res, uint32_t id) {
Location* location = g_locations_storage->Get(id);
if (!location) {
NotFound(HTTP_POST, res);
return;
}
Location new_location = *location;
int update_flags = 0;
if (!Location::Parse(req.body, req.body_length,
&new_location, &update_flags)) {
BadRequest(HTTP_POST, res);
return;
}
std::swap(*location, new_location);
location->Serialize(id);
SendOk(res);
}
void UpdateVisits(const http_request& req, http_response& res, uint32_t id) {
Visit* visit = g_visits_storage->Get(id);
if (!visit) {
NotFound(HTTP_POST, res);
return;
}
Visit new_visit = *visit;
int update_flags = 0;
if (!Visit::Parse(req.body, req.body_length, &new_visit, &update_flags)) {
BadRequest(HTTP_POST, res);
return;
}
if (update_flags & UpdateFlags::UPDATE_LOCATION) {
auto* index = g_db_instance->GetLocationsVisitsIndex();
index->Replace(visit->location_id, new_visit.location_id, visit);
}
if (update_flags & UpdateFlags::UPDATE_USER) {
auto* index = g_db_instance->GetUsersVisitsIndex();
index->Replace(visit->user_id, new_visit.user_id, visit);
}
std::swap(*visit, new_visit);
visit->Serialize(id);
SendOk(res);
}
void AddUsers(const http_request& req, http_response& res) {
User* user = new User;
int update_flags = 0;
uint32_t id = 0;
if (!User::Parse(req.body, req.body_length, user, &update_flags, &id)) {
BadRequest(HTTP_POST, res);
return;
}
if (update_flags & UPDATE_ID &&
update_flags & UPDATE_EMAIL &&
update_flags & UPDATE_FIRST_NAME &&
update_flags & UPDATE_LAST_NAME &&
update_flags & UPDATE_GENDER &&
update_flags & UPDATE_BIRTH_DATE) {
if (!g_users_storage->Add(id, user, nullptr)) {
BadRequest(HTTP_POST, res);
return;
}
user->Serialize(id);
} else {
BadRequest(HTTP_POST, res);
return;
}
SendOk(res);
}
void AddLocations(const http_request& req, http_response& res) {
Location* location = new Location;
int update_flags = 0;
uint32_t id = 0;
if (!Location::Parse(req.body, req.body_length,
location, &update_flags, &id)) {
BadRequest(HTTP_POST, res);
return;
}
if (update_flags & UPDATE_ID &&
update_flags & UPDATE_PLACE &&
update_flags & UPDATE_COUNTRY &&
update_flags & UPDATE_CITY &&
update_flags & UPDATE_DISTANCE) {
if (!g_locations_storage->Add(
id, location, nullptr)) {
BadRequest(HTTP_POST, res);
return;
}
location->Serialize(id);
} else {
BadRequest(HTTP_POST, res);
return;
}
SendOk(res);
}
void AddVisits(const http_request& req, http_response& res) {
Visit* visit = new Visit;
int update_flags = 0;
uint32_t id = 0;
if (!Visit::Parse(req.body, req.body_length, visit, &update_flags, &id)) {
BadRequest(HTTP_POST, res);
return;
}
if (update_flags & UPDATE_ID &&
update_flags & UPDATE_LOCATION &&
update_flags & UPDATE_USER &&
update_flags & UPDATE_VISITED_AT &&
update_flags & UPDATE_MARK) {
Visit* instance = nullptr;
uint32_t user_id = visit->user_id;
uint32_t location_id = visit->location_id;
if (!g_visits_storage->Add(id, visit, &instance)) {
BadRequest(HTTP_POST, res);
return;
}
visit->Serialize(id);
g_db_instance->GetUsersVisitsIndex()->Add(
user_id, instance);
g_db_instance->GetLocationsVisitsIndex()->Add(
location_id, instance);
} else {
BadRequest(HTTP_POST, res);
return;
}
SendOk(res);
}
} // namespace post_handlers