-
Notifications
You must be signed in to change notification settings - Fork 0
/
borga-web-site.js
371 lines (301 loc) · 7.8 KB
/
borga-web-site.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
'use strict';
const express = require('express');
module.exports = function (services) {
/**
* Gets the userId from the request.
* @param {Object} req
* @returns the userId from the request
*/
function getUserId(req) {
return req.user && req.user.userId;
}
/**
* Gets the token from the request.
* @param {Object} req
* @returns the token from the request
*/
function getBearerToken(req) {
return req.user && req.user.token;
}
/**
* Sends as response an object containing the cause of the error.
* @param {Object} res
* @param {Object} error
*/
function onError(res, error) {
switch (error.name) {
case 'MISSING_PARAM':
case 'BAD_REQUEST':
res.status(400);
break;
case 'UNAUTHENTICATED':
res.status(401);
break;
case 'NOT_FOUND':
res.status(404);
break;
case 'ALREADY_EXISTS':
res.status(409);
break;
case 'EXT_SVC_FAIL':
res.status(502);
break;
case 'FAIL':
default:
console.log(error);
res.status(500);
}
res.render('error', { error });
}
/**
* Gets the home page.
* @param {Object} req
* @param {Object} res
*/
function getHomepage(req, res) {
res.render('home', { user: req.user });
}
/**
* Gets the search page.
* @param {Object} req
* @param {Object} res
*/
function getSearchPage(req, res) {
res.render('search', { user: req.user });
}
/**
* Shows game details.
* @param {Object} req
* @param {Object} res
*/
async function showGameDetails(req, res) {
const gameId = req.params.gameId;
try {
const game = await services.getGameDetails(gameId);
const groups = (req.user)
? await services.listUserGroups(getBearerToken(req), getUserId(req))
: {}
res.render('gameDetails', { header: 'Game Details', game, groups, user: req.user });
} catch (error) {
onError(res, error);
}
}
/**
* Shows the twenty most popular games.
* @param {Object} req
* @param {Object} res
*/
async function showPopularGames(req, res) {
try {
const games = await services.getPopularGames();
const groups = (req.user)
? await services.listUserGroups(getBearerToken(req), getUserId(req))
: {}
res.render('games', { header: 'Popular Games', games, groups, user: req.user });
} catch (error) {
onError(res, error);
}
}
/**
* Shows the details of the searched games.
* @param {Object} req
* @param {Object} res
*/
async function showSearchedGames(req, res) {
const gameName = req.query.gameName;
const limit = req.query.limit;
const order_by = req.query.order_by;
try {
const games = await services.searchGamesByName(gameName, limit, order_by);
const groups = (req.user)
? await services.listUserGroups(getBearerToken(req), getUserId(req))
: {}
res.render('games', { header: 'Games', gameName, games, groups, user: req.user });
} catch (error) {
console.log(error)
if (error.name == "NOT_FOUND" && error.info.gameName) {
res.render('error', { error, gameNameNotFound: gameName });
}
else {
onError(res, error);
}
}
}
/**
* Shows the user groups.
* @param {Object} req
* @param {Object} res
*/
async function showUserGroups(req, res) {
const token = getBearerToken(req);
const userId = req.params.userId;
try {
const groups = await services.listUserGroups(token, userId);
res.render('groups', { groups, user: req.user });
} catch (error) {
onError(res, error);
}
}
/**
* Shows group details.
* @param {Object} req
* @param {Object} res
*/
async function showGroupDetails(req, res) {
const token = getBearerToken(req);
const userId = req.params.userId;
const groupId = req.params.groupId;
try {
const group = await services.getGroupDetails(token, userId, groupId);
group.id = groupId;
res.render('groupDetails', { header: 'Group Details', group, user: req.user });
} catch (error) {
onError(res, error);
}
}
/**
* Creates a group.
* @param {Object} req
* @param {Object} res
*/
async function createGroup(req, res) {
const token = getBearerToken(req);
const userId = req.params.userId;
const groupName = req.body.groupName;
const groupDescription = req.body.groupDescription;
try {
const group = await services.createGroup(token, userId, groupName, groupDescription);
res.redirect(`/user/${userId}/groups/${group.id}`);
} catch (error) {
onError(res, error);
}
}
/**
* Edits a group.
* @param {Object} req
* @param {Object} res
*/
async function editGroup(req, res) {
const token = getBearerToken(req);
const userId = req.params.userId;
const groupId = req.params.groupId;
const newGroupName = req.body.newGroupName;
const newGroupDescription = req.body.newGroupDescription;
try {
await services.editGroup(token, userId, groupId, newGroupName, newGroupDescription);
res.redirect(`/user/${userId}/groups/${groupId}`);
} catch (error) {
onError(res, error);
}
}
/**
* Adds a game to a group.
* @param {Object} req
* @param {Object} res
*/
async function addGameToGroup(req, res) {
const token = getBearerToken(req);
const userId = req.params.userId;
const groupId = req.params.groupId;
const gameId = req.params.gameId;
try {
await services.addGameToGroup(token, userId, groupId, gameId);
res.redirect(`/user/${userId}/groups/${groupId}`);
} catch (error) {
onError(res, error);
}
}
/**
* Shows the register/login page.
* @param {Object} req
* @param {Object} res
*/
async function getRegisterLoginPage(req, res) {
res.render('register_login');
}
/**
* Register and logins a new user.
* @param {Object} req
* @param {Object} res
*/
async function registerUser(req, res) {
const userName = req.body.userName;
const userId = req.body.userId;
const password = req.body.password;
try {
await services.createNewUser(userId, userName, password);
doLogin(req, res);
} catch (error) {
if (error.name == 'ALREADY_EXISTS')
res.render('register_login', { already_exists: error })
else {
onError(res, error);
}
}
}
/**
* Logins user
* @param {Object} req
* @param {Object} res
*/
async function doLogin(req, res) {
const userId = req.body.userId;
const password = req.body.password;
try {
const user = await services.checkCredentials(userId, password);
user.userId = userId;
user.token = await services.getToken(userId);
req.login(user, err => {
if (err)
console.log('LOGIN ERROR', err);
res.redirect(`/`);
});
} catch (error) {
if (error.name == 'UNAUTHENTICATED')
res.render('register_login', { unauthenticated: error })
else {
onError(res, error);
}
}
}
/**
* Logouts user
* @param {Object} req
* @param {Object} res
*/
async function doLogout(req, res) {
req.logout();
res.redirect('/');
}
const router = express.Router();
router.use(express.urlencoded({ extended: true }));
// Homepage
router.get('/', getHomepage);
// Search page
router.get('/search', getSearchPage);
// Show popular games
router.get('/games/popular', showPopularGames);
// Show games searched
router.get('/games', showSearchedGames);
// Show game details
router.get('/games/:gameId', showGameDetails);
// Show Register/Login page
router.get('/auth', getRegisterLoginPage);
// Register new user
router.post('/register', registerUser);
// Login
router.post('/login', doLogin);
// Register new user
router.post('/logout', doLogout);
// Show groups
router.get('/user/:userId/groups', showUserGroups);
// Create group
router.post('/user/:userId/groups', createGroup);
// Show group details
router.get('/user/:userId/groups/:groupId', showGroupDetails);
// Edit group
router.post('/user/:userId/groups/:groupId', editGroup);
// Adds a game to a group
router.post('/user/:userId/groups/:groupId/games/:gameId', addGameToGroup);
return router;
};