forked from wso2/choreo-sample-book-list-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mjs
103 lines (93 loc) · 2.89 KB
/
app.mjs
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
import express from "express";
import cache from "./cache.mjs";
import { v4 as uuidv4 } from "uuid";
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// add a book - request body should contain a title, status and an author
app.post("/reading-list/books", (req, res) => {
const { title, author, status } = req.body;
const uuid = uuidv4();
if (!(status === "read" || status === "to_read" || status === "reading")) {
return res.status(400).json({
error: "Status is invalid. Accepted statuses: read | to_read | reading",
});
}
if (!title || !author || !status) {
return res.status(400).json({ error: "Title, Status or Author is empty" });
}
const value = { uuid, title, author, status };
cache.set(uuid, value, 86400);
return res.status(201).json({ uuid, title, author });
});
// update status of a book by uuid
app.put("/reading-list/books/:uuid", (req, res) => {
const uuid = req.params.uuid;
const { status } = req.body;
if (!uuid || typeof uuid !== "string") {
return res.status(400).json({ error: "missing or invalid UUID" });
}
if (!cache.has(uuid)) {
return res.status(404).json({ error: "UUID does not exist" });
}
if (!(status === "read" || status === "to_read" || status === "reading")) {
return res.status(400).json({
error: "Status is invalid. Accepted statuses: read | to_read | reading",
});
}
const value = cache.get(uuid);
value.status = status;
cache.set(uuid, value);
return res.json({ uuid, status });
});
// get the list of books
app.get("/reading-list/books", (_, res) => {
const keys = cache.keys();
const allData = {};
for (const key of keys) {
allData[key] = cache.get(key);
}
return res.json(allData);
});
// get a book by uuid
app.get("/reading-list/books/:uuid", (req, res) => {
const uuid = req.params.uuid;
if (!uuid || typeof uuid !== "string") {
return res.status(400).json({ error: "missing or invalid UUID" });
}
if (!cache.has(uuid)) {
return res.status(404).json({ error: "UUID does not exist" });
}
const value = cache.get(uuid);
return res.json(value);
});
// delete a book by uuid
app.delete("/reading-list/books/:uuid", (req, res) => {
const uuid = req.params.uuid;
if (!uuid || typeof uuid !== "string") {
return res.status(400).json({ error: "missing or invalid UUID" });
}
if (!cache.has(uuid)) {
return res.status(404).json({ error: "UUID does not exist" });
}
cache.del(uuid);
return res.json({ uuid });
});
// health check
app.get("/healthz", (_, res) => {
return res.sendStatus(200);
});
app.use((err, _req, res, next) => {
if (res.headersSent) {
return next(err);
}
console.error(err);
res.status(500);
res.json({ error: err.message });
});
app.use("*", (_, res) => {
return res
.status(404)
.json({ error: "the requested resource does not exist on this server" });
});
export default app;