Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/controllers/books/booksControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const { books } = require("../../../data/index.js")
const DataNotFoundError = require("../../errors/DataNotFoundError.js")
const {
verifyBookBody,
verifyBookTitle,
addBookToDb,
findBookId,
removeBookFromDb,
replaceBookInDb,
verifyPatchOfBookBody,
verifyBookBodyRequest,
} = require("../../domain/books/booksRepository.js")

const getBooks = (req, res) => {
res.json({
books: books,
})
}

const postBook = (req, res) => {
verifyBookBody(req)

verifyBookTitle(req)

const book = { id: books.length + 1, ...req.body }

addBookToDb(book)

res.status(201).json({
book: book,
})
}

const getBookById = (req, res) => {
const id = Number(req.params.id)
const book = findBookId(id)

if (!book) {
throw new DataNotFoundError("A book the provided ID does not exist")
}

res.json({
book: book,
})
}

const deleteBook = (req, res) => {
const id = Number(req.params.id)
const book = findBookId(id)

if (!book) {
throw new DataNotFoundError("A book the provided ID does not exist")
}

removeBookFromDb(book)

res.json({
book: book,
})
}

const updateBook = (req, res) => {
verifyBookBody(req)

const id = Number(req.params.id)
const book = findBookId(id)

if (!book) {
throw new DataNotFoundError("A book the provided ID does not exist")
}

const updatedBook = { id: book.id, ...req.body }

verifyBookTitle(req)

replaceBookInDb(book, updatedBook)

res.json({
book: updatedBook,
})
}

const patchBook = (req, res) => {
verifyPatchOfBookBody(req)

const id = Number(req.params.id)
const book = findBookId(id)

if (!book) {
throw new DataNotFoundError("A book the provided ID does not exist")
}

verifyBookTitle(req)

let updatedBook = {}

verifyBookBodyRequest(req, updatedBook, book)

res.json({
book: updatedBook,
})
}

module.exports = {
getBooks,
postBook,
getBookById,
deleteBook,
updateBook,
patchBook,
}
116 changes: 116 additions & 0 deletions src/controllers/films/filmsControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const { films } = require("../../../data/index.js")
const MissingDataError = require("../../errors/MissingDataError.js")
const ExistingDataError = require("../../errors/ExistingDataError.js")
const DataNotFoundError = require("../../errors/DataNotFoundError.js")
const {
verifyFilmReqQuery,
verifyFilmBody,
verifyFilmTitle,
addFilmToDb,
findFilmById,
removeFilmFromDb,
replaceFilmInDb,
verifyFilmReqBody,
verifyMssingFields,
} = require("../../domain/films/filmsRepository.js")

const getFilms = (req, res) => {
verifyFilmReqQuery(req, res)

res.json({
films: films,
})
}

const postFilm = (req, res) => {
verifyFilmBody(req)

verifyFilmTitle(req)

const film = { id: films.length + 1, ...req.body }

addFilmToDb(film)

res.status(201).json({
film: film,
})
}

const getFilmById = (req, res) => {
const id = Number(req.params.id)
const film = findFilmById(id)

if (!film) {
throw new DataNotFoundError("A film with provided ID does not exist")
}

res.json({
film: film,
})
}

const deleteFilm = (req, res) => {
const id = Number(req.params.id)
const film = findFilmById(id)

if (!film) {
throw new DataNotFoundError("A film with provided ID does not exist")
}

removeFilmFromDb(film)

res.json({
film: film,
})
}

const updateFilm = (req, res) => {
const id = Number(req.params.id)
const film = findFilmById(id)

if (!film) {
throw new DataNotFoundError("A film with provided ID does not exist")
}

verifyFilmTitle(req)

const updatedFilm = { id: film.id, ...req.body }

replaceFilmInDb(film, updatedFilm)

res.json({
film: updatedFilm,
})
}

const patchFilm = (req, res) => {
verifyMssingFields(req)

const id = Number(req.params.id)
const film = findFilmById(id)

if (!film) {
throw new DataNotFoundError("A film with provided ID does not exist")
}

verifyFilmTitle(req)

let updatedFilm = {}

verifyFilmReqBody(req, updatedFilm, film)

replaceFilmInDb(film, updatedFilm)

res.json({
film: updatedFilm,
})
}

module.exports = {
getFilms,
postFilm,
getFilmById,
deleteFilm,
updateFilm,
patchFilm,
}
87 changes: 87 additions & 0 deletions src/controllers/users/usersControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { users } = require("../../../data/index.js")
const {
verifyUserBody,
verifyUserEmail,
addUserToDb,
findUserId,
deleteUserFromDb,
replaceUserInDb,
} = require("../../domain/users/usersRepository.js")
const DataNotFoundError = require("../../errors/DataNotFoundError.js")

const getUsers = (req, res) => {
res.json({
users: users,
})
}

const postUser = (req, res) => {
verifyUserBody(req)

verifyUserEmail(req)

const user = { id: users.length + 1, ...req.body }

addUserToDb(user)

res.status(201).json({
user: user,
})
}

const getUserById = (req, res) => {
const id = Number(req.params.id)
const user = findUserId(id)

if (!user) {
throw new DataNotFoundError("A user with the provided ID does not exist")
}

res.json({
user: user,
})
}

const deleteUser = (req, res) => {
const id = Number(req.params.id)
const user = findUserId(id)

if (!user) {
throw new DataNotFoundError("A user with the provided ID does not exist")
}

deleteUserFromDb(user)

res.json({
user: user,
})
}

const updateUser = (req, res) => {
verifyUserBody(req)

const id = Number(req.params.id)
const user = findUserId(id)

if (!user) {
throw new DataNotFoundError("A user with the provided ID does not exist")
}

verifyUserEmail(req)

const updatedUser = { id: user.id, ...req.body }

replaceUserInDb(user, updatedUser)

res.json({
user: updatedUser,
})
}

module.exports = {
getUsers,
postUser,
getUserById,
deleteUser,
updateUser,
}
Loading