-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborga-webapi.js
257 lines (220 loc) · 5.89 KB
/
borga-webapi.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"use strict";
const express = require("express");
const openApiUi = require("swagger-ui-express");
const openApiSpec = require("./docs/borga-spec.json");
const errors = require("./borga-errors");
function getHttpErrorCode(err) {
switch (err.name) {
case "FAILURE":
return 500;
case "NOT_FOUND":
return 404;
case "EXT_SVC_FAILURE":
return 503;
case "ALREADY_EXISTS":
return 409;
case "MISSING_PARAM":
return 400;
case "INVALID_PARAM":
return 400;
case "UNAUTHENTICATED":
return 401;
default:
return 500;
}
}
function handleError(err, res) {
const code = getHttpErrorCode(err);
res.status(code).json({ cause: err });
}
module.exports = function (services) {
function getUserToken(req) {
return req.user && req.user.token;
}
function getBearerToken(req) {
const auth = req.headers.authorization;
if (auth) {
const authData = auth.trim();
if (authData.substr(0, 6).toLowerCase() === "bearer") {
return authData.replace(/^bearer\s+/i, "");
}
}
return null;
}
async function getUsername(req) {
const token = getUserToken(req);
if (!token) {
throw errors.UNAUTHENTICATED(`token is missing`);
}
return services.getUsername(token);
}
async function searchPopularGames(_, res) {
try {
const games = await services.getMostPopularGames();
res.json(games);
} catch (err) {
handleError(err, res);
}
}
async function searchGame(req, res) {
try {
const games = await services.searchGame(req.query.name);
res.json({ games });
} catch (err) {
handleError(err, res);
}
}
async function listAllGroups(req, res) {
try {
const username = await getUsername(req);
const groups = await services.listAllGroups(username);
res.json({ groups });
} catch (err) {
handleError(err, res);
}
}
async function getGroupDetails(req, res) {
try {
const username = await getUsername(req);
const details = await services.getGroupDetails(
username,
req.params.groupId
);
res.json(details);
} catch (err) {
handleError(err, res);
}
}
async function editGroup(req, res) {
try {
const username = await getUsername(req);
if (!req.body.name) {
throw errors.MISSING_PARAM("parameter 'name' is missing");
}
if (!req.body.description) {
throw errors.MISSING_PARAM("parameter 'description' is missing");
}
const groupId = await services.editGroup(
username,
req.params.groupId,
req.body.name,
req.body.description
);
res.json(groupId);
} catch (err) {
handleError(err, res);
}
}
async function createGroup(req, res) {
try {
const username = await getUsername(req);
if (!req.body.name) {
throw errors.MISSING_PARAM("parameter 'name' is missing");
}
if (!req.body.description) {
throw errors.MISSING_PARAM("parameter 'description' is missing");
}
const groupId = await services.createGroup(
username,
req.body.name,
req.body.description
);
res.status(201).json({ groupId });
} catch (err) {
handleError(err, res);
}
}
async function deleteGroup(req, res) {
try {
const username = await getUsername(req);
const groupId = await services.deleteGroup(username, req.params.groupId);
res.json({ groupId });
} catch (err) {
handleError(err, res);
}
}
async function addGame(req, res) {
try {
const username = await getUsername(req);
if (!req.body.gameId) {
throw errors.MISSING_PARAM("'gameId' is missing");
}
const gameId = await services.addGame(
username,
req.params.groupId,
req.body.gameId
);
res.status(201).json({ gameId });
} catch (err) {
handleError(err, res);
}
}
async function removeGame(req, res) {
try {
const username = await getUsername(req);
const gameId = await services.removeGame(
username,
req.params.groupId,
req.params.gameId
);
res.json({ gameId });
} catch (err) {
handleError(err, res);
}
}
async function createUser(req, res) {
try {
if (!req.body.username) {
throw errors.MISSING_PARAM("'username' is missing");
}
if (!req.body.name) {
throw errors.MISSING_PARAM("'name' is missing");
}
if (!req.body.password) {
throw errors.MISSING_PARAM("'password' is missing");
}
const token = await services.createUser(
req.body.name,
req.body.username,
req.body.password
);
res.status(201).json({ token });
} catch (err) {
handleError(err, res);
}
}
function extractToken(req, res, next) {
const bearerToken = getBearerToken(req);
if (bearerToken) {
req.user = { token: bearerToken };
}
next();
}
const router = express.Router();
router.use("/docs", openApiUi.serve);
router.use(express.json());
router.use(extractToken);
// Open API docs
router.get("/docs", openApiUi.setup(openApiSpec));
// Get most popular games
router.get("/games/mostPopular", searchPopularGames);
// Search for games by name
router.get("/games", searchGame);
// List user groups
router.get("/groups", listAllGroups);
// Get group details
router.get("/groups/:groupId", getGroupDetails);
// Edit group
router.put("/groups/:groupId", editGroup);
// Create new group
router.post("/groups", createGroup);
// Delete group
router.delete("/groups/:groupId", deleteGroup);
// Add game to group
router.post("/groups/:groupId/games", addGame);
// Remove game from group
router.delete("/groups/:groupId/games/:gameId", removeGame);
// Create user
router.post("/users", createUser);
return router;
};