Repository navigation
Expand file tree
/
Copy pathcontroller.js
More file actions
99 lines (77 loc) · 3.06 KB
/
Copy pathcontroller.js
File metadata and controls
99 lines (77 loc) · 3.06 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const Book = require("./model.js");
const { getBodyData } = require('./utils.js');
const getBooks = async (req, res) => {
try {
const books = await Book.findAll();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(books));
} catch (err) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: err.message }));
}
};
const getBook = async (req, res, id) => {
try {
const foundBook = await Book.findOne(id);
if (!foundBook) return res
.writeHead(404, { "Content-Type": "application/json" })
.end(JSON.stringify({ message: "Book not found" }));
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(foundBook));
} catch (err) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: err.message }));
}
};
const addBook = async (req, res) => {
try {
const body = await getBodyData(req);
const { title, desc } = JSON.parse(body);
if (!title || !desc) return res
.writeHead(400, { "Content-Type": "application/json" })
.end(JSON.stringify({ message: "Book needs to have a title and description" }));
const newBook = await Book.add({ title, desc });
await Book.save();
res.writeHead(201, { "Content-Type": "application/json" });
res.end(JSON.stringify(newBook));
} catch (err) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: err.message }));
}
};
const updateBook = async (req, res, id) => {
try {
let body = await getBodyData(req);
const foundBook = await Book.findOne(id);
if (!foundBook) return res
.writeHead(404, { "Content-Type": "application/json" })
.end(JSON.stringify({ message: "Book not found" }));
const fields = Object.fromEntries(Object.entries(JSON.parse(body)).filter(([, v]) => v !== ''));//Exclude empty fields
if (Object.keys(fields).length <= 0) return res
.writeHead(400, { "Content-Type": "application/json" })
.end(JSON.stringify({ message: "Book cannot be updated with: ''" }));
const updatedBook = await Book.update(id, fields);
await Book.save();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(updatedBook));
} catch (err) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: err.message }));
}
};
const deleteBook = async (req, res, id) => {
try {
const foundBook = await Book.findOne(id);
if (!foundBook) return res
.writeHead(404, { "Content-Type": "application/json" })
.end(JSON.stringify({ message: "Book not found" }));
const books = await Book.delete(id);
await Book.save();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(books));
} catch (err) {
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: err.message }));
}
};
module.exports = { getBooks, getBook, addBook, updateBook, deleteBook };