-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (45 loc) · 1.33 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
const express = require("express");
const port = 3001;
const app = express();
app.use(express.json());
let users = [
{ name: "john", kidneys: [{ healthy: false }, { healthy: true }] },
];
app.get("/", (req, res) => {
const johnKidneys = users[0].kidneys;
const numberOfKidneys = johnKidneys.length;
let healthyKidneys = johnKidneys.filter((kidney) => {
return kidney.healthy;
});
const countOfHealthy = healthyKidneys.length;
const countOfUnhealthy = numberOfKidneys - countOfHealthy;
res.json({ numberOfKidneys, countOfHealthy, countOfUnhealthy });
});
app.post("/", (req, res) => {
const isHealthy = req.body.isHealthy;
users[0].kidneys.push({ healthy: isHealthy });
res.json({ msg: "Done" });
});
app.put("/", (req, res) => {
users[0].kidneys.forEach((kidney) => {
kidney.healthy = true;
});
res.json({ msg: "Done" });
});
app.delete("/", (req, res) => {
const johnKidneys = users[0].kidneys;
let unhealthyKidneys = johnKidneys.filter((kidney) => {
return !kidney.healthy;
});
if (unhealthyKidneys.length === 0) {
res.status(411).json({ msg: "no unhealthy kidneys" });
}
const newKidneys = users[0].kidneys.filter((kidney) => {
return kidney.healthy;
});
users[0].kidneys = newKidneys;
res.json({ msg: "Done" });
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});