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
121 changes: 121 additions & 0 deletions src/controllers/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
let { books } = require('../../data/index')
const {MissingFieldsError, NotFoundError, AlreadyExistsError} = require('../errors/errors')

let nextBookId = 5

const getAllBooks = (req, res) => {
res.status(200).send({ books: books })
}

const addBook = (req, res) => {
if(!req.body.title || !req.body.type || !req.body.author) {
throw new MissingFieldsError('Missing fields in request body')
}

const alreadyExists = books.find((element) => {
return element.title === req.body.title
})

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

const newBook = { id: nextBookId, ...req.body }
nextBookId++
books.push(newBook)

res.status(201).send({ book: newBook })
}

const getBookById = (req, res) => {
const searchedBook = books.find((element) => {
return element.id === Number(req.params['id'])
})

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

res.status(200).send({ book: searchedBook })
}

const deleteBookById = (req, res) => {
const searchedBook = books.find((element) => {
return element.id === Number(req.params['id'])
})

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

books = books.filter((element) => {
return !(element.id === Number(req.params['id']))
})

res.status(200).send({ book: searchedBook })
}

const updateBookById = (req, res) => {
if(!req.body.title || !req.body.type || !req.body.author) {
throw new MissingFieldsError('Missing fields in request body')
}

const searchedBook = books.find((element) => {
return element.id === Number(req.params['id'])
})

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

const alreadyExists = books.find((element) => {
return element.title === req.body.title
})

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

Object.keys(req.body).forEach((element) => {
if (searchedBook[element]) {
searchedBook[element] = req.body[element]
}
})

res.status(200).send({ book: searchedBook })
}

const patchBookById = (req, res) => {
const searchedBook = books.find((element) => {
return element.id === Number(req.params['id'])
})

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

const alreadyExists = books.find((element) => {
return element.title === req.body.title
})

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

Object.keys(req.body).forEach((element) => {
if (searchedBook[element]) {
searchedBook[element] = req.body[element]
}
})

res.status(200).send({ book: searchedBook })
}

module.exports = {
getAllBooks,
addBook,
getBookById,
deleteBookById,
updateBookById,
patchBookById,
}
141 changes: 141 additions & 0 deletions src/controllers/films.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
let { films } = require('../../data/index')
const {
MissingFieldsError,
NotFoundError,
AlreadyExistsError,
} = require('../errors/errors')

let nextFilmId = 5

const getAllFilms = (req, res) => {
let filmsToSend

if(req.query.director) {
filmsToSend = films.filter((element) => {
return element.director === req.query.director
})
} else {
filmsToSend = films
}

res.status(200).send({ films: filmsToSend })
}

const addFilm = (req, res) => {
if (!req.body.title || !req.body.director) {
throw new MissingFieldsError('Missing fields in request body')
}

const alreadyExists = films.find((element) => {
return element.title === req.body.title
})

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

const newFilm = { id: nextFilmId, ...req.body }
nextFilmId++
films.push(newFilm)

res.status(201).send({ film: newFilm })
}

const getFilmById = (req, res) => {
const searchedFilm = films.find((element) => {
return element.id === Number(req.params['id'])
})

if (!searchedFilm) {
throw new NotFoundError('A film with provided ID does not exist')
}

res.status(200).send({ film: searchedFilm })
}

const deleteFilmById = (req, res) => {
const searchedFilm = films.find((element) => {
return element.id === Number(req.params['id'])
})

if (!searchedFilm) {
throw new NotFoundError('A film with provided ID does not exist')
}

films = films.filter((element) => {
return !(element.id === Number(req.params['id']))
})

res.status(200).send({ film: searchedFilm })
}

const updateFilmById = (req, res) => {
if (!req.body.title || !req.body.director) {
throw new MissingFieldsError('Missing fields in request body')
}

const searchedFilm = films.find((element) => {
return element.id === Number(req.params['id'])
})

if (!searchedFilm) {
throw new NotFoundError('A film with provided ID does not exist')
}

const alreadyExists = films.find((element) => {
return element.title === req.body.title
})

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

Object.keys(req.body).forEach((element) => {
if (searchedFilm[element]) {
searchedFilm[element] = req.body[element]
}
})

res.status(200).send({ film: searchedFilm })
}

const patchFilmById = (req, res) => {
const searchedFilm = films.find((element) => {
return element.id === Number(req.params['id'])
})

if (!searchedFilm) {
throw new NotFoundError('A film with provided ID does not exist')
}

const alreadyExists = films.find((element) => {
return element.title === req.body.title
})

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

Object.keys(req.body).forEach((element) => {
if (searchedFilm[element]) {
searchedFilm[element] = req.body[element]
}
})

res.status(200).send({ film: searchedFilm })
}

module.exports = {
getAllFilms,
addFilm,
getFilmById,
deleteFilmById,
updateFilmById,
patchFilmById,
}
102 changes: 102 additions & 0 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
let { users } = require('../../data/index')
const {
MissingFieldsError,
NotFoundError,
AlreadyExistsError,
} = require('../errors/errors')

let nextUserId = 4

const getAllUsers = (req, res) => {
res.status(200).send({ users: users })
}

const addUser = (req, res) => {
if (!req.body.email) {
throw new MissingFieldsError('Missing fields in request body')
}

const alreadyExists = users.find((element) => {
return element.email === req.body.email
})

if (alreadyExists) {
throw new AlreadyExistsError(
'A user with the provided email already exists'
)
}

const newUser = { id: nextUserId, ...req.body }
nextUserId++
users.push(newUser)

res.status(201).send({ user: newUser })
}

const getUserById = (req, res) => {
const searchedUser = users.find((element) => {
return element.id === Number(req.params['id'])
})

if (!searchedUser) {
throw new NotFoundError('A user with the provided ID does not exist')
}

res.status(200).send({ user: searchedUser })
}

const deleteUserById = (req, res) => {
const searchedUser = users.find((element) => {
return element.id === Number(req.params['id'])
})

if (!searchedUser) {
throw new NotFoundError('A user with the provided ID does not exist')
}

users = users.filter((element) => {
return !(element.id === Number(req.params['id']))
})

res.status(200).send({ user: searchedUser })
}

const updateUserById = (req, res) => {
if (!req.body.email) {
throw new MissingFieldsError('Missing fields in request body')
}

const searchedUser = users.find((element) => {
return element.id === Number(req.params['id'])
})

if (!searchedUser) {
throw new NotFoundError('A user with the provided ID does not exist')
}

const alreadyExists = users.find((element) => {
return element.email === req.body.email
})

if (alreadyExists) {
throw new AlreadyExistsError(
'A user with the provided email already exists'
)
}

Object.keys(req.body).forEach((element) => {
if (searchedUser[element]) {
searchedUser[element] = req.body[element]
}
})

res.status(200).send({ user: searchedUser })
}

module.exports = {
getAllUsers,
addUser,
getUserById,
deleteUserById,
updateUserById,
}
7 changes: 7 additions & 0 deletions src/errors/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class MissingFieldsError extends Error{}

class NotFoundError extends Error{}

class AlreadyExistsError extends Error{}

module.exports = {MissingFieldsError, NotFoundError, AlreadyExistsError}
Loading