Skip to content
67 changes: 67 additions & 0 deletions src/controllers/books/booksController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { getAllBooks, newBook, getBookById, deleteBookById, updateBookById, patchBookById } = require("../../domain/books/booksRepository")

let idCounter = 5

const getAll = (req, res) => {
const books = getAllBooks()

res.json({books})
}

const createBook = (req, res) => {
const book = req.body

book.id = idCounter
newBook(book)

idCounter++

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

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

res.json({book})
}

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

deleteBookById(bookID)

res.json({book})
}

const updateBook = (req, res) => {
const newBookInfo = req.body
const bookID = Number(req.params.id)

newBookInfo.id = bookID

updateBookById(bookID, newBookInfo)

res.json({book: newBookInfo})
}

const update = (req, res) => {
const newBookInfo = req.body
const bookID = Number(req.params.id)

newBookInfo.id = bookID

const patchedBook = patchBookById(bookID, newBookInfo)

res.json({book: patchedBook})
}

module.exports = {
getAll,
createBook,
findBook,
deleteBook,
updateBook,
update
}
75 changes: 75 additions & 0 deletions src/controllers/films/filmsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { getAllFilms, newFilm, getFilmById, deleteFilmById, updateFilmById, patchFilmById } = require("../../domain/films/filmsRepository")

let idCounter = 5

const getAll = (req, res) => {
const director = req.query.director

if (director) {
const filteredFilms = getAllFilms(director)

return res.json({films: filteredFilms})
}

const films = getAllFilms()

res.json({films})
}

const createFilm = (req, res) => {
const film = req.body

film.id = idCounter
newFilm(film)

idCounter++

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

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

res.json({film})
}

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

deleteFilmById(filmID)

res.json({film})
}

const updateFilm = (req, res) => {
const newFilmInfo = req.body
const filmID = Number(req.params.id)

newFilmInfo.id = filmID

updateFilmById(filmID, newFilmInfo)

res.json({film: newFilmInfo})
}

const update = (req, res) => {
const newFilmInfo = req.body
const filmID = Number(req.params.id)

newFilmInfo.id = filmID

const patchedFilm = patchFilmById(filmID, newFilmInfo)

res.json({film: patchedFilm})
}

module.exports = {
getAll,
createFilm,
findFilm,
deleteFilm,
updateFilm,
update
}
55 changes: 55 additions & 0 deletions src/controllers/users/usersController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { getAllUsers, newUser, getUserById, deleteUserById, updateUserById } = require("../../domain/users/usersRepository")

let idCounter = 4

const getAll = (req, res) => {
const users = getAllUsers()

res.json({users})
}

const createUser = (req, res) => {
const user = req.body

user.id = idCounter
newUser(user)

idCounter++

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

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

res.json({user})
}

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

deleteUserById(userID)

res.json({user})
}

const updateUser = (req, res) => {
const newUserInfo = req.body
const userID = Number(req.params.id)

newUserInfo.id = userID

updateUserById(userID, newUserInfo)

res.json({user: newUserInfo})
}

module.exports = {
getAll,
createUser,
findUser,
deleteUser,
updateUser
}
106 changes: 106 additions & 0 deletions src/domain/books/booksRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
let { books } = require("../../../data/index.js")
const AlreadyExistsError = require("../../errors/alreadyExistsError.js")
const MissingFieldsError = require("../../errors/missingFieldsError.js")
const NotFoundError = require("../../errors/notFoundError.js")

function getAllBooks() {
return books
}

function newBook(book) {
if (!verifyBookProperties(book)) {
throw new MissingFieldsError('Missing fields in request body')
}

if (verifyBook(book)) {
throw new AlreadyExistsError('A book with the provided title already exists')
}

books.push(book)
}

function getBookById(id) {
const found = books.find((book) => book.id === id)

if (!found) {
throw new NotFoundError('A book the provided ID does not exist')
}

return found
}

function deleteBookById(id) {
const found = getBookById(id)

if (!found) {
throw new NotFoundError('A book the provided ID does not exist')
}

books = books.filter((book) => book.id !== id)
}

function updateBookById(id, updatedBook) {
if (!verifyBookProperties(updatedBook)) {
throw new MissingFieldsError('Missing fields in request body')
}

const found = getBookById(id)

if (!found) {
throw new NotFoundError('A book the provided ID does not exist')
}

if (verifyBook(updatedBook)) {
throw new AlreadyExistsError('A book with the provided title already exists')
}

Object.assign(books, updatedBook)
}

function patchBookById(id, updatedBook) {
const found = books.find((book) => book.id === id)
const foundIndex = books.indexOf(found)

if (!found) {
throw new NotFoundError('A book the provided ID does not exist')
}

if (verifyBook(updatedBook)) {
throw new AlreadyExistsError('A book with the provided title already exists')
}

Object.assign(books[foundIndex], updatedBook)

return books[foundIndex]
}

function verifyBookProperties(object) {
const neededProperties = ['title', 'type', 'author']

for (const item of neededProperties) {
if (object[item] === undefined) {
return false
}
}

return true
}

function verifyBook(object) {
const foundBook = books.find((book) => book.title === object.title)

if (foundBook) {
return true
}

return false
}

module.exports = {
getAllBooks,
newBook,
getBookById,
deleteBookById,
updateBookById,
patchBookById
}
Loading