-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (109 loc) · 2.72 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser')
const cors = require('cors')
const app = express();
app.use(bodyParser.json()) // for parsing application/json
const corsOptions = {
exposedHeaders: [
'Cache-Control',
'Content-Language',
'Content-Length',
'Content-Type',
'Expires',
'Last-Modified',
'Pragma',
'Etag'
],
}
app.use(cors(corsOptions))
var games = new Map();
var nextId = 100;
app.get('/', (_, res) => {
res.send('Welcome to the Turtle Repository!')
});
app.get('/games', (_, res) => {
res.json([...games.values()]);
});
app.get('/games/:id', (req, res) => {
const game = games.get(+req.params.id);
if (!game) {
return res.sendStatus(404);
}
res.json(game);
});
app.post('/games', (req, res) => {
var game = req.body;
const error = validateAddGame(game);
if (error) {
return res.status(400).send(error);
}
if(req.query.prevGame) {
return createNextGame(res, game, +req.query.prevGame);
}
const id = nextId++;
game['id'] = id;
games.set(id, game);
res.json(game);
});
function createNextGame(res, game, prevGameId) {
if (!games.has(prevGameId)) {
return res.status(400).send("Invalid prevGame");
}
prevGame = games.get(prevGameId);
if (games.get(prevGameId).next_game_id) {
return res.json(games.get(prevGame.next_game_id));
}
const id = nextId++;
game['id'] = id;
games.set(id, game);
prevGame.next_game_id = id;
return res.json(game);
}
app.put('/games/:id', (req, res) => {
var game = req.body;
const id = +req.params.id;
const error = validateUpdateGame(game, id);
if (error) {
return res.status(400).send(error);
}
if (!games.has(id)) {
return res.sendStatus(404)
}
games.set(id, game);
res.sendStatus(204);
});
app.delete('/games/:id', (req, res) => {
const id = +req.params.id;
if (!games.has(id)) {
return res.sendStatus(404)
}
delete games.delete(id);
res.sendStatus(204);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log('server started');
});
function validateAddGame(game) {
if (typeof game['id'] !== 'undefined') {
return 'You cannot provide an id when adding a new game.\n'
};
return validateFields(game);
}
function validateUpdateGame(game, id) {
if (typeof game['id'] === 'undefined') {
return 'Field not found: id.\n'
};
if (game['id'] != id) {
return 'Id in the body and in the path don\'t match.\n'
};
return validateFields(game);
}
function validateFields(game) {
for(const field of ['players', 'colors', 'hands', 'deck', 'discarded', 'board', 'active_player']) {
if (typeof game[field] === 'undefined') {
return 'Field not found: ' + field + '.\n'
};
}
return undefined;
}