diff --git a/src/controllers/books.js b/src/controllers/books.js new file mode 100644 index 0000000..e35e3c8 --- /dev/null +++ b/src/controllers/books.js @@ -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, +} diff --git a/src/controllers/films.js b/src/controllers/films.js new file mode 100644 index 0000000..cb0bb1f --- /dev/null +++ b/src/controllers/films.js @@ -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, +} diff --git a/src/controllers/users.js b/src/controllers/users.js new file mode 100644 index 0000000..f662ac4 --- /dev/null +++ b/src/controllers/users.js @@ -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, +} diff --git a/src/errors/errors.js b/src/errors/errors.js new file mode 100644 index 0000000..3441d52 --- /dev/null +++ b/src/errors/errors.js @@ -0,0 +1,7 @@ +class MissingFieldsError extends Error{} + +class NotFoundError extends Error{} + +class AlreadyExistsError extends Error{} + +module.exports = {MissingFieldsError, NotFoundError, AlreadyExistsError} diff --git a/src/routers/books.js b/src/routers/books.js index 18b9a7c..61ac43a 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,4 +1,26 @@ // Import data here... - +const express = require('express') +const router = express.Router() +const { + getAllBooks, + addBook, + getBookById, + deleteBookById, + updateBookById, + patchBookById, +} = require('../controllers/books') // Write routes here... +router.get('/', getAllBooks) + +router.post('/', addBook) + +router.get('/:id', getBookById) + +router.delete('/:id', deleteBookById) + +router.put('/:id', updateBookById) + +router.patch('/:id', patchBookById) + +module.exports = router diff --git a/src/routers/films.js b/src/routers/films.js index e69de29..5b5df5d 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -0,0 +1,19 @@ +// Import data here... +const express = require('express') +const router = express.Router() +const { getAllFilms, addFilm, getFilmById, deleteFilmById, updateFilmById , patchFilmById} = require('../controllers/films') + +// Write routes here... +router.get('/', getAllFilms) + +router.post('/', addFilm) + +router.get('/:id', getFilmById) + +router.delete('/:id', deleteFilmById) + +router.put('/:id', updateFilmById) + +router.patch('/:id', patchFilmById) + +module.exports = router \ No newline at end of file diff --git a/src/routers/users.js b/src/routers/users.js index e69de29..1fa465d 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -0,0 +1,17 @@ +// Import data here... +const express = require('express') +const router = express.Router() +const { getAllUsers, addUser, getUserById, deleteUserById, updateUserById } = require('../controllers/users') + +// Write routes here... +router.get('/', getAllUsers) + +router.post('/', addUser) + +router.get('/:id', getUserById) + +router.delete('/:id', deleteUserById) + +router.put('/:id', updateUserById) + +module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index 715321f..c19af92 100644 --- a/src/server.js +++ b/src/server.js @@ -1,18 +1,40 @@ -const express = require("express"); -const app = express(); +const express = require('express') +const app = express() -const cors = require("cors"); -const morgan = require("morgan"); +const cors = require('cors') +const morgan = require('morgan') +const { + MissingFieldsError, + NotFoundError, + AlreadyExistsError, +} = require('./errors/errors') // SETUP MIDDLEWARE -app.use(cors()); -app.use(express.json()); -app.use(morgan("dev")); +app.use(cors()) +app.use(express.json()) +app.use(morgan('dev')) + + // REQUIRE ROUTERS -const usersRouter = require("./routers/users"); +const usersRouter = require('./routers/users') +const booksRouter = require('./routers/books') +const filmsRouter = require('./routers/films') // ADD ROUTERS TO APP - +app.use('/users', usersRouter) +app.use('/books', booksRouter) +app.use('/films', filmsRouter) +app.use((error, req, res, next) => { + if (error instanceof MissingFieldsError) { + return res.status(400).send({error:error.message}) + } + if (error instanceof NotFoundError) { + return res.status(404).send({error:error.message}) + } + if (error instanceof AlreadyExistsError) { + return res.status(409).send({error:error.message}) + } +}) module.exports = app