-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (73 loc) · 1.91 KB
/
index.js
File metadata and controls
85 lines (73 loc) · 1.91 KB
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
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
const vacCentres = {
"Radin Mas Community Club": {
nurseCapacity: 10,
currentSlots: [],
},
"Buona Vista Community Club": {
nurseCapacity: 15,
currentSlots: [],
},
"Potong Pasir Community Club": {
nurseCapacity: 20,
currentSlots: [],
},
"Raffles City Convention Centre": {
nurseCapacity: 25,
currentSlots: [],
},
};
app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded());
// parse application/json
app.use(bodyParser.json());
app.get("/", (req, res) => {
const hasId = req.query.hasOwnProperty("id");
if (!hasId) {
return res.json(vacCentres);
}
res.json(vacCentres[req.query.id]);
});
app.post("/", (req, res) => {
const id = req.query.id;
if (vacCentres[id].nurseCapacity >= vacCentres[id].currentSlots.length) {
res.status(500).json();
}
vacCentres[id].currentSlots.push(req.body);
console.log(vacCentres);
res.json();
});
app.put("/", (req, res) => {
const fromId = req.query.fromId;
const toId = req.query.toId;
if (toId === fromId) {
const index = vacCentres[id].currentSlots.findIndex(
(v) => v.registerBy === req.body.registerBy
);
vacCentres[id].currentSlots[index] = req.body;
console.log(vacCentres);
res.json();
} else {
vacCentres[fromId].currentSlots = vacCentres[fromId].currentSlots.filter(
(v) => v.registerBy === req.body.username
);
vacCentres[toId].currentSlots.push(req.body);
console.log(vacCentres);
res.json();
}
});
app.delete("/", (req, res) => {
const id = req.query.id;
vacCentres[id].currentSlots = vacCentres[id].currentSlots.filter(
(v) => v.registerBy !== req.query.registerBy
);
console.log(vacCentres);
res.json();
});
app.listen(5000, () => {
console.log("Server is started.");
});