-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (66 loc) · 2.28 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
"use strict";
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const fixtures = require("./data/fixtures");
let fixturesDB = JSON.parse(JSON.stringify(fixtures));
const HTTP_STATUS_CODE_NOT_FOUND = 404;
const HTTP_STATUS_CODE_FORBIDDEN = 403;
const HTTP_STATUS_CODE_ACCEPTED = 202;
const HTTP_STATUS_CODE_NO_CONTENT = 204;
const getFixtureById = id =>
fixturesDB.find(fixture => fixture.fixtureId === id);
const getFixtureIndex = id =>
fixturesDB.findIndex(fixture => fixture.fixtureId === id);
const putFixture = fixture => {
fixturesDB = fixturesDB.filter(fixtures => fixtures.fixtureId !== fixture.fixtureId);
fixturesDB.push(fixture);
}
const randomIntBetweenMinMax = (min, max) =>
Math.floor(Math.random() * max) + min;
const secondsToMillis = seconds => seconds * 1000;
const updateDelay = () =>
secondsToMillis(randomIntBetweenMinMax(2, 5));
const postFixture = fixture =>
setTimeout(() => {
fixturesDB.push(fixture);
}, updateDelay());
const checkIfFixtureExists = fixtureId => getFixtureIndex(fixtureId) !== -1;
const fixtureNotFound = res =>
res.status(HTTP_STATUS_CODE_NOT_FOUND).send();
app.get("/fixtures", (req, res) => res.json(fixturesDB));
app.get("/fixture/:id", (req, res) => {
const fixtureId = req.params.id;
checkIfFixtureExists(fixtureId)
? res.json(getFixtureById(fixtureId))
: fixtureNotFound(res);
});
app.put("/fixture", (req, res) => {
const fixtureId = req.body.fixtureId;
if (checkIfFixtureExists(fixtureId)) {
putFixture(req.body);
res.status(HTTP_STATUS_CODE_NO_CONTENT).send();
} else {
fixtureNotFound(res);
}
});
app.post("/fixture", (req, res) => {
const fixtureId = req.body.fixtureId;
if (checkIfFixtureExists(fixtureId)) {
res.status(HTTP_STATUS_CODE_FORBIDDEN).send();
} else {
postFixture(req.body);
res.status(HTTP_STATUS_CODE_ACCEPTED).send();
}
});
app.delete("/fixture/:id", (req, res) => {
const fixtureId = req.params.id;
if (checkIfFixtureExists(fixtureId)) {
fixturesDB = fixturesDB.filter(fixtures => fixtures.fixtureId !== fixtureId);
res.status(HTTP_STATUS_CODE_NO_CONTENT).send();
} else {
fixtureNotFound(res);
}
});
app.listen(3000, () => console.log("Server is listening on port 3000"));