Skip to content

Commit 54997b8

Browse files
author
Leonardo Lodi
committed
do mrv: add domain directory
1 parent 0c9b3c2 commit 54997b8

File tree

15 files changed

+553
-425
lines changed

15 files changed

+553
-425
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const { books } = require("../../../data/index.js")
2+
const DataNotFoundError = require("../../errors/DataNotFoundError.js")
3+
const {
4+
verifyBookBody,
5+
verifyBookTitle,
6+
addBookToDb,
7+
findBookId,
8+
removeBookFromDb,
9+
replaceBookInDb,
10+
verifyPatchOfBookBody,
11+
verifyBookBodyRequest,
12+
} = require("../../domain/books/booksRepository.js")
13+
14+
const getBooks = (req, res) => {
15+
res.json({
16+
books: books,
17+
})
18+
}
19+
20+
const postBook = (req, res) => {
21+
verifyBookBody(req)
22+
23+
verifyBookTitle(req)
24+
25+
const book = { id: books.length + 1, ...req.body }
26+
27+
addBookToDb(book)
28+
29+
res.status(201).json({
30+
book: book,
31+
})
32+
}
33+
34+
const getBookById = (req, res) => {
35+
const id = Number(req.params.id)
36+
const book = findBookId(id)
37+
38+
if (!book) {
39+
throw new DataNotFoundError("A book the provided ID does not exist")
40+
}
41+
42+
res.json({
43+
book: book,
44+
})
45+
}
46+
47+
const deleteBook = (req, res) => {
48+
const id = Number(req.params.id)
49+
const book = findBookId(id)
50+
51+
if (!book) {
52+
throw new DataNotFoundError("A book the provided ID does not exist")
53+
}
54+
55+
removeBookFromDb(book)
56+
57+
res.json({
58+
book: book,
59+
})
60+
}
61+
62+
const updateBook = (req, res) => {
63+
verifyBookBody(req)
64+
65+
const id = Number(req.params.id)
66+
const book = findBookId(id)
67+
68+
if (!book) {
69+
throw new DataNotFoundError("A book the provided ID does not exist")
70+
}
71+
72+
const updatedBook = { id: book.id, ...req.body }
73+
74+
verifyBookTitle(req)
75+
76+
replaceBookInDb(book, updatedBook)
77+
78+
res.json({
79+
book: updatedBook,
80+
})
81+
}
82+
83+
const patchBook = (req, res) => {
84+
verifyPatchOfBookBody(req)
85+
86+
const id = Number(req.params.id)
87+
const book = findBookId(id)
88+
89+
if (!book) {
90+
throw new DataNotFoundError("A book the provided ID does not exist")
91+
}
92+
93+
verifyBookTitle(req)
94+
95+
let updatedBook = {}
96+
97+
verifyBookBodyRequest(req, updatedBook, book)
98+
99+
res.json({
100+
book: updatedBook,
101+
})
102+
}
103+
104+
module.exports = {
105+
getBooks,
106+
postBook,
107+
getBookById,
108+
deleteBook,
109+
updateBook,
110+
patchBook,
111+
}

src/controllers/booksControllers.js

Lines changed: 0 additions & 173 deletions
This file was deleted.

0 commit comments

Comments
 (0)