From 666f1e99ef04e3b69f21d9c456396d2827d9b24e Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 12:23:52 +0100 Subject: [PATCH 01/21] get/users --- src/controllers/books/books.js | 0 src/controllers/films/films.js | 0 src/controllers/users/users.js | 10 ++++++++++ src/domain/books/books.js | 2 ++ src/domain/films/films.js | 2 ++ src/domain/users/users.js | 8 ++++++++ src/routers/books.js | 4 ---- src/routers/users.js | 8 ++++++++ src/server.js | 3 ++- 9 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/controllers/books/books.js create mode 100644 src/controllers/films/films.js create mode 100644 src/controllers/users/users.js create mode 100644 src/domain/books/books.js create mode 100644 src/domain/films/films.js create mode 100644 src/domain/users/users.js diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controllers/films/films.js b/src/controllers/films/films.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js new file mode 100644 index 0000000..2d661f1 --- /dev/null +++ b/src/controllers/users/users.js @@ -0,0 +1,10 @@ +const getAllUsers = require('../../domain/users/users.js') + +const getAll = (req, res) => { + + res.status(200).json({ + getAllUsers + }) +} + +module.exports = getAll \ No newline at end of file diff --git a/src/domain/books/books.js b/src/domain/books/books.js new file mode 100644 index 0000000..907d1d3 --- /dev/null +++ b/src/domain/books/books.js @@ -0,0 +1,2 @@ +const data = require('../../data/index.js') +const books = data.books diff --git a/src/domain/films/films.js b/src/domain/films/films.js new file mode 100644 index 0000000..913e96c --- /dev/null +++ b/src/domain/films/films.js @@ -0,0 +1,2 @@ +const data = require('../../data/index.js') +const films = data.films \ No newline at end of file diff --git a/src/domain/users/users.js b/src/domain/users/users.js new file mode 100644 index 0000000..c6b8ac4 --- /dev/null +++ b/src/domain/users/users.js @@ -0,0 +1,8 @@ +const data = require('../../../data/index.js') +const users = data.users + +const getAllUsers = () => { + return users +} + +module.exports = getAllUsers() \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 18b9a7c..e69de29 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,4 +0,0 @@ -// Import data here... - - -// Write routes here... diff --git a/src/routers/users.js b/src/routers/users.js index e69de29..c5c4452 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -0,0 +1,8 @@ +const { Router } = require("express"); +const getAll = require("../controllers/users/users"); + +const router = Router() + +router.get('/', getAll) + +module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index 715321f..b9c976b 100644 --- a/src/server.js +++ b/src/server.js @@ -10,9 +10,10 @@ app.use(express.json()); app.use(morgan("dev")); // REQUIRE ROUTERS -const usersRouter = require("./routers/users"); +const usersRouter = require("./routers/users.js"); // ADD ROUTERS TO APP +app.use('/users', usersRouter) module.exports = app From 11dd00dcc913d7126ae5442a28c22e5029ca8d6a Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 12:44:57 +0100 Subject: [PATCH 02/21] post/users: create user --- src/controllers/users/users.js | 25 +++++++++++++++++++++++-- src/functions/createID.js | 6 ++++++ src/routers/users.js | 4 +++- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 src/functions/createID.js diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index 2d661f1..9d57a0f 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -1,10 +1,31 @@ const getAllUsers = require('../../domain/users/users.js') +const newID = require('../../functions/createID.js') +const data = require('../../../data/index.js') + +let newUser = { + id: 0, + email: 'string' +} + const getAll = (req, res) => { res.status(200).json({ - getAllUsers + users: getAllUsers + }) +} + +const createUser = (req, res) => { + newUser.id = newID(data.users) + newUser.email = req.body.email + + data.users.push(newUser) + res.status(201).json({ + user: newUser }) } -module.exports = getAll \ No newline at end of file +module.exports = { + getAll, + createUser +} diff --git a/src/functions/createID.js b/src/functions/createID.js new file mode 100644 index 0000000..720c9ea --- /dev/null +++ b/src/functions/createID.js @@ -0,0 +1,6 @@ +function newID(data) { + const newID = data.reverse().find((d) => d.id) + return newID.id +1 +} + +module.exports = newID \ No newline at end of file diff --git a/src/routers/users.js b/src/routers/users.js index c5c4452..469466d 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -1,8 +1,10 @@ const { Router } = require("express"); -const getAll = require("../controllers/users/users"); +const {getAll, createUser} = require("../controllers/users/users"); const router = Router() router.get('/', getAll) +router.post('/', createUser) + module.exports = router \ No newline at end of file From 5079779a34c1241f1833987ac96d8ebf0400419e Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 13:02:16 +0100 Subject: [PATCH 03/21] /get/users/id --- src/controllers/users/users.js | 14 ++++++++++++-- src/domain/users/users.js | 9 ++++++++- src/functions/findId.js | 6 ++++++ src/routers/users.js | 4 +++- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/functions/findId.js diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index 9d57a0f..9142a60 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -1,4 +1,4 @@ -const getAllUsers = require('../../domain/users/users.js') +const {getAllUsers, getUserById} = require('../../domain/users/users.js') const newID = require('../../functions/createID.js') const data = require('../../../data/index.js') @@ -25,7 +25,17 @@ const createUser = (req, res) => { }) } +const getByID = (req, res) => { + const id = Number(req.params.id) + const found = getUserById(id) + + res.status(200).json({ + user: found + }) +} + module.exports = { getAll, - createUser + createUser, + getByID } diff --git a/src/domain/users/users.js b/src/domain/users/users.js index c6b8ac4..08baa48 100644 --- a/src/domain/users/users.js +++ b/src/domain/users/users.js @@ -5,4 +5,11 @@ const getAllUsers = () => { return users } -module.exports = getAllUsers() \ No newline at end of file +const getUserById = (id) => { + return getAllUsers().find((u) => u.id === id) +} + +module.exports = { + getAllUsers, + getUserById +} \ No newline at end of file diff --git a/src/functions/findId.js b/src/functions/findId.js new file mode 100644 index 0000000..834b1d4 --- /dev/null +++ b/src/functions/findId.js @@ -0,0 +1,6 @@ +function findById(data, id) { + const found = data.find((d) => d.id === id) + return found +} + +module.exports = {findById} \ No newline at end of file diff --git a/src/routers/users.js b/src/routers/users.js index 469466d..48e4340 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -1,5 +1,5 @@ const { Router } = require("express"); -const {getAll, createUser} = require("../controllers/users/users"); +const {getAll, createUser, getByID} = require("../controllers/users/users"); const router = Router() @@ -7,4 +7,6 @@ router.get('/', getAll) router.post('/', createUser) +router.get('/:id', getByID) + module.exports = router \ No newline at end of file From 301a8a97262f05e14b5e88eab709bbd6b360948d Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 13:15:42 +0100 Subject: [PATCH 04/21] delete/users/id --- data/deletedUsers.js | 5 +++++ src/controllers/users/users.js | 24 ++++++++++++++++++------ src/routers/users.js | 4 +++- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 data/deletedUsers.js diff --git a/data/deletedUsers.js b/data/deletedUsers.js new file mode 100644 index 0000000..11f70f4 --- /dev/null +++ b/data/deletedUsers.js @@ -0,0 +1,5 @@ +const deletedUsers = [] + +module.exports = { + deletedUsers +} \ No newline at end of file diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index 9142a60..a8bcc44 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -1,6 +1,6 @@ const {getAllUsers, getUserById} = require('../../domain/users/users.js') const newID = require('../../functions/createID.js') -const data = require('../../../data/index.js') +const {deletedUsers} = require('../../../data/deletedUsers.js') let newUser = { id: 0, @@ -9,17 +9,16 @@ let newUser = { const getAll = (req, res) => { - res.status(200).json({ - users: getAllUsers + users: getAllUsers() }) } const createUser = (req, res) => { - newUser.id = newID(data.users) + newUser.id = newID(getAllUsers()) newUser.email = req.body.email - data.users.push(newUser) + getAllUsers().push(newUser) res.status(201).json({ user: newUser }) @@ -34,8 +33,21 @@ const getByID = (req, res) => { }) } +const removeUser = (req, res) => { + const id = Number(req.params.id) + const found = getUserById(id) + + deletedUsers.push(found) + const index = getAllUsers().indexOf(found) + getAllUsers().splice(index, 1) + res.status(200).json({ + user: found + }) +} + module.exports = { getAll, createUser, - getByID + getByID, + removeUser } diff --git a/src/routers/users.js b/src/routers/users.js index 48e4340..307ce10 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -1,5 +1,5 @@ const { Router } = require("express"); -const {getAll, createUser, getByID} = require("../controllers/users/users"); +const {getAll, createUser, getByID, removeUser} = require("../controllers/users/users.js"); const router = Router() @@ -9,4 +9,6 @@ router.post('/', createUser) router.get('/:id', getByID) +router.delete('/:id', removeUser) + module.exports = router \ No newline at end of file From 9d5ffe7c5681be023078376b39a2e85134115443 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 13:34:28 +0100 Subject: [PATCH 05/21] post/films --- src/controllers/films/films.js | 30 ++++++++++++++++++++++++++++++ src/controllers/users/users.js | 13 ++++++++++++- src/domain/films/films.js | 13 +++++++++++-- src/routers/films.js | 10 ++++++++++ src/routers/users.js | 4 +++- src/server.js | 2 ++ 6 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/controllers/films/films.js b/src/controllers/films/films.js index e69de29..a065cb5 100644 --- a/src/controllers/films/films.js +++ b/src/controllers/films/films.js @@ -0,0 +1,30 @@ +const {getAllFilms} = require('../../domain/films/films.js') +const newID = require('../../functions/createID.js') + +let newFilm = { + id: 0, + title: 'string', + director: 'string' +} + +const getAll = (req, res) => { + res.status(200).json({ + films: getAllFilms() + }) +} + +const addFilm = (req, res) => { + newFilm.id = newID(getAllFilms()) + newFilm.title = req.body.title + newFilm.director = req.body.director + + getAllFilms().push(newFilm) + res.status(201).json({ + film: newFilm + }) +} + +module.exports = { + getAll, + addFilm +} \ No newline at end of file diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index a8bcc44..92120d9 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -45,9 +45,20 @@ const removeUser = (req, res) => { }) } +const updateUser = (req, res) => { + const id = Number(req.params.id) + const found = getUserById(id) + + found.email = req.body.email + res.status(200).json({ + user: found + }) +} + module.exports = { getAll, createUser, getByID, - removeUser + removeUser, + updateUser } diff --git a/src/domain/films/films.js b/src/domain/films/films.js index 913e96c..416fcaf 100644 --- a/src/domain/films/films.js +++ b/src/domain/films/films.js @@ -1,2 +1,11 @@ -const data = require('../../data/index.js') -const films = data.films \ No newline at end of file +const data = require('../../../data/index.js') +const films = data.films + + +const getAllFilms = () => { + return films +} + +module.exports = { + getAllFilms +} \ No newline at end of file diff --git a/src/routers/films.js b/src/routers/films.js index e69de29..e066aac 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -0,0 +1,10 @@ +const { Router } = require("express"); +const { getAll, addFilm } = require("../controllers/films/films"); + +const router = Router() + +router.get('/', getAll) + +router.post('/', addFilm) + +module.exports = router \ No newline at end of file diff --git a/src/routers/users.js b/src/routers/users.js index 307ce10..69fb06c 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -1,5 +1,5 @@ const { Router } = require("express"); -const {getAll, createUser, getByID, removeUser} = require("../controllers/users/users.js"); +const {getAll, createUser, getByID, removeUser, updateUser} = require("../controllers/users/users.js"); const router = Router() @@ -11,4 +11,6 @@ router.get('/:id', getByID) router.delete('/:id', removeUser) +router.put('/:id', updateUser) + module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index b9c976b..928272b 100644 --- a/src/server.js +++ b/src/server.js @@ -11,9 +11,11 @@ app.use(morgan("dev")); // REQUIRE ROUTERS const usersRouter = require("./routers/users.js"); +const filmsRouter = require('./routers/films.js') // ADD ROUTERS TO APP app.use('/users', usersRouter) +app.use('/films', filmsRouter) module.exports = app From f35c90c56a26fe0d2baf9be1b9159fba5b154b88 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 14:01:15 +0100 Subject: [PATCH 06/21] delete/films, put/films --- data/deletedData.js | 7 ++++++ data/deletedUsers.js | 5 ----- src/controllers/films/films.js | 39 ++++++++++++++++++++++++++++++++-- src/controllers/users/users.js | 2 +- src/domain/films/films.js | 7 +++++- src/routers/films.js | 8 ++++++- 6 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 data/deletedData.js delete mode 100644 data/deletedUsers.js diff --git a/data/deletedData.js b/data/deletedData.js new file mode 100644 index 0000000..643b861 --- /dev/null +++ b/data/deletedData.js @@ -0,0 +1,7 @@ +const deletedUsers = [] +const deletedFilms = [] + +module.exports = { + deletedUsers, + deletedFilms +} \ No newline at end of file diff --git a/data/deletedUsers.js b/data/deletedUsers.js deleted file mode 100644 index 11f70f4..0000000 --- a/data/deletedUsers.js +++ /dev/null @@ -1,5 +0,0 @@ -const deletedUsers = [] - -module.exports = { - deletedUsers -} \ No newline at end of file diff --git a/src/controllers/films/films.js b/src/controllers/films/films.js index a065cb5..7c08b43 100644 --- a/src/controllers/films/films.js +++ b/src/controllers/films/films.js @@ -1,4 +1,5 @@ -const {getAllFilms} = require('../../domain/films/films.js') +const { deletedFilms } = require('../../../data/deletedData.js') +const {getAllFilms, getFilmByID} = require('../../domain/films/films.js') const newID = require('../../functions/createID.js') let newFilm = { @@ -24,7 +25,41 @@ const addFilm = (req, res) => { }) } +const getByID = (req, res) => { + const id = Number(req.params.id) + const found = getFilmByID(id) + res.status(200).json({ + film: found + }) +} + +const removeFIlm = (req, res) => { + const id = Number(req.params.id) + const found = getFilmByID(id) + + deletedFilms.push(found) + const index = getAllFilms().indexOf(found) + getAllFilms().splice(index, 1) + res.status(200).json({ + film: found + }) +} + +const updateFilm = (req, res) => { + const id = Number(req.params.id) + const found = getFilmByID(id) + + found.title = req.body.title + found.director = req.body.director + res.status(200).json({ + film: found + }) +} + module.exports = { getAll, - addFilm + addFilm, + getByID, + removeFIlm, + updateFilm } \ No newline at end of file diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index 92120d9..950c44c 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -1,6 +1,6 @@ const {getAllUsers, getUserById} = require('../../domain/users/users.js') const newID = require('../../functions/createID.js') -const {deletedUsers} = require('../../../data/deletedUsers.js') +const {deletedUsers} = require('../../../data/deletedData.js') let newUser = { id: 0, diff --git a/src/domain/films/films.js b/src/domain/films/films.js index 416fcaf..eda37d2 100644 --- a/src/domain/films/films.js +++ b/src/domain/films/films.js @@ -6,6 +6,11 @@ const getAllFilms = () => { return films } +const getFilmByID = (id) => { + return getAllFilms().find((f) => f.id === id) +} + module.exports = { - getAllFilms + getAllFilms, + getFilmByID } \ No newline at end of file diff --git a/src/routers/films.js b/src/routers/films.js index e066aac..746f58e 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -1,5 +1,5 @@ const { Router } = require("express"); -const { getAll, addFilm } = require("../controllers/films/films"); +const { getAll, addFilm, getByID, removeFIlm, updateFilm } = require("../controllers/films/films"); const router = Router() @@ -7,4 +7,10 @@ router.get('/', getAll) router.post('/', addFilm) +router.get('/:id', getByID) + +router.delete('/:id', removeFIlm) + +router.put('/:id', updateFilm) + module.exports = router \ No newline at end of file From 1961b1a2d33db8239c3efd4cbabe7730bdb447bb Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 14:49:42 +0100 Subject: [PATCH 07/21] post/books --- src/controllers/books/books.js | 32 ++++++++++++++++++++++++++++++++ src/domain/books/books.js | 11 ++++++++++- src/routers/books.js | 9 +++++++++ src/server.js | 2 ++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index e69de29..0395d72 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -0,0 +1,32 @@ +const { getAllBooks } = require("../../domain/books/books") +const newID = require("../../functions/createID") + +let newBook = { + id: 0, + title: 'string', + type: 'string', + author: 'string' +} + +const getBooks = (req, res) => { + res.status(200).json({ + books: getAllBooks() + }) +} + +const addBook = (req, res) => { + newBook.id = newID(getAllBooks()) + newBook.title = req.body.title + newBook.type = req.body.type + newBook.author = req.body.author + + getAllBooks().push(newBook) + res.status(201).json({ + books: newBook + }) +} + +module.exports = { + getBooks, + addBook +} \ No newline at end of file diff --git a/src/domain/books/books.js b/src/domain/books/books.js index 907d1d3..947d32f 100644 --- a/src/domain/books/books.js +++ b/src/domain/books/books.js @@ -1,2 +1,11 @@ -const data = require('../../data/index.js') +const data = require('../../../data/index.js') const books = data.books + + +const getAllBooks = () => { + return books +} + +module.exports = { + getAllBooks +} \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index e69de29..bda4493 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -0,0 +1,9 @@ +const { Router } = require("express"); +const { getBooks, addBook } = require("../controllers/books/books"); + +const router = Router() + +router.get('/', getBooks) +router.post('/', addBook) + +module.exports = router diff --git a/src/server.js b/src/server.js index 928272b..3d628d3 100644 --- a/src/server.js +++ b/src/server.js @@ -12,10 +12,12 @@ app.use(morgan("dev")); // REQUIRE ROUTERS const usersRouter = require("./routers/users.js"); const filmsRouter = require('./routers/films.js') +const booksRouter = require('./routers/books.js') // ADD ROUTERS TO APP app.use('/users', usersRouter) app.use('/films', filmsRouter) +app.use('/books', booksRouter) module.exports = app From 5c3f334eb7b258eb2ad59163fe26892ca5668e0f Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 15:00:21 +0100 Subject: [PATCH 08/21] get/books/id --- src/controllers/books/books.js | 14 ++++++++++++-- src/domain/books/books.js | 7 ++++++- src/routers/books.js | 3 ++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 0395d72..1bf90b4 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -1,4 +1,4 @@ -const { getAllBooks } = require("../../domain/books/books") +const { getAllBooks, getBookByID } = require("../../domain/books/books") const newID = require("../../functions/createID") let newBook = { @@ -26,7 +26,17 @@ const addBook = (req, res) => { }) } +const getByID = (req, res) => { + const id = Number(req.params.id) + const found = getBookByID(id) + + res.status(200).json({ + book: found + }) +} + module.exports = { getBooks, - addBook + addBook, + getByID } \ No newline at end of file diff --git a/src/domain/books/books.js b/src/domain/books/books.js index 947d32f..ff346ed 100644 --- a/src/domain/books/books.js +++ b/src/domain/books/books.js @@ -6,6 +6,11 @@ const getAllBooks = () => { return books } +const getBookByID = (id) => { + return getAllBooks().find((b) => b.id === id) +} + module.exports = { - getAllBooks + getAllBooks, + getBookByID } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index bda4493..9b76a53 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,9 +1,10 @@ const { Router } = require("express"); -const { getBooks, addBook } = require("../controllers/books/books"); +const { getBooks, addBook, getByID } = require("../controllers/books/books"); const router = Router() router.get('/', getBooks) router.post('/', addBook) +router.get('/:id', getByID) module.exports = router From eb578efedf4c2b85c6cae433847ef42474ce19b1 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 15:07:06 +0100 Subject: [PATCH 09/21] delete/books/id --- data/deletedData.js | 4 +++- src/controllers/books/books.js | 16 +++++++++++++++- src/routers/books.js | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/data/deletedData.js b/data/deletedData.js index 643b861..31706b5 100644 --- a/data/deletedData.js +++ b/data/deletedData.js @@ -1,7 +1,9 @@ const deletedUsers = [] const deletedFilms = [] +const deletedBooks = [] module.exports = { deletedUsers, - deletedFilms + deletedFilms, + deletedBooks } \ No newline at end of file diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 1bf90b4..3c59e49 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -1,5 +1,6 @@ const { getAllBooks, getBookByID } = require("../../domain/books/books") const newID = require("../../functions/createID") +const { deletedBooks } = require('../../../data/deletedData.js') let newBook = { id: 0, @@ -35,8 +36,21 @@ const getByID = (req, res) => { }) } +const removeBook = (req, res) => { + const id = Number(req.params.id) + const found = getBookByID(id) + + deletedBooks.push(found) + const index = getAllBooks().indexOf(found) + getAllBooks().splice(index, 1) + res.status(200).json({ + book: found + }) +} + module.exports = { getBooks, addBook, - getByID + getByID, + removeBook } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 9b76a53..1c853bf 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,10 +1,11 @@ const { Router } = require("express"); -const { getBooks, addBook, getByID } = require("../controllers/books/books"); +const { getBooks, addBook, getByID, removeBook } = require("../controllers/books/books"); const router = Router() router.get('/', getBooks) router.post('/', addBook) router.get('/:id', getByID) +router.delete('/:id', removeBook) module.exports = router From 38033b7330dd55a6d8dbebc049cecc5bbd32d233 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 15:11:52 +0100 Subject: [PATCH 10/21] put/books/id --- src/controllers/books/books.js | 16 +++++++++++++++- src/routers/books.js | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 3c59e49..2d08eb0 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -48,9 +48,23 @@ const removeBook = (req, res) => { }) } +const updateBook = (req, res) => { + const id = Number(req.params.id) + const found = getBookByID(id) + + found.title = req.body.title + found.type = req.body.type + found.author = req.body.author + + res.status(200).json({ + book: found + }) +} + module.exports = { getBooks, addBook, getByID, - removeBook + removeBook, + updateBook } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 1c853bf..1d104a2 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,5 +1,5 @@ const { Router } = require("express"); -const { getBooks, addBook, getByID, removeBook } = require("../controllers/books/books"); +const { getBooks, addBook, getByID, removeBook, updateBook } = require("../controllers/books/books"); const router = Router() @@ -7,5 +7,6 @@ router.get('/', getBooks) router.post('/', addBook) router.get('/:id', getByID) router.delete('/:id', removeBook) +router.put('/:id', updateBook) module.exports = router From 69457939e56726c3da412a0b6d0a06dca6452518 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 15:13:43 +0100 Subject: [PATCH 11/21] core tests run --- src/controllers/books/books.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 2d08eb0..a35086c 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -23,7 +23,7 @@ const addBook = (req, res) => { getAllBooks().push(newBook) res.status(201).json({ - books: newBook + book: newBook }) } From a9b05e562e160457e20f518401b27a1b32991ff8 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 15:29:40 +0100 Subject: [PATCH 12/21] put/books/ id error boundary created --- src/controllers/books/books.js | 15 ++++++++++++++- src/domain/books/books.js | 7 ++++++- src/errorClasses/index.js | 11 +++++++++++ src/server.js | 24 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/errorClasses/index.js diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index a35086c..6b096e9 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -1,4 +1,4 @@ -const { getAllBooks, getBookByID } = require("../../domain/books/books") +const { getAllBooks, getBookByID, filterByTitle } = require("../../domain/books/books") const newID = require("../../functions/createID") const { deletedBooks } = require('../../../data/deletedData.js') @@ -50,12 +50,25 @@ const removeBook = (req, res) => { const updateBook = (req, res) => { const id = Number(req.params.id) + if(typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + const found = getBookByID(id) + if(!found) { + throw new NotFoundError("Book not found") + } found.title = req.body.title found.type = req.body.type found.author = req.body.author + const checkTitle = filterByTitle(found.title) + + if(checkTitle) { + throw new BookAlreadyExistsError("Book already exists") + } + res.status(200).json({ book: found }) diff --git a/src/domain/books/books.js b/src/domain/books/books.js index ff346ed..a0ffd85 100644 --- a/src/domain/books/books.js +++ b/src/domain/books/books.js @@ -10,7 +10,12 @@ const getBookByID = (id) => { return getAllBooks().find((b) => b.id === id) } +const filterByTitle = (title) => { + return getAllBooks().find((b) => b.title === title) +} + module.exports = { getAllBooks, - getBookByID + getBookByID, + filterByTitle } \ No newline at end of file diff --git a/src/errorClasses/index.js b/src/errorClasses/index.js new file mode 100644 index 0000000..82355d7 --- /dev/null +++ b/src/errorClasses/index.js @@ -0,0 +1,11 @@ +class NotFoundError extends Error { + +} + +class InvalidDataError extends Error { + +} + +class BookAlreadyExistsError extends Error { + +} \ No newline at end of file diff --git a/src/server.js b/src/server.js index 3d628d3..b5921d3 100644 --- a/src/server.js +++ b/src/server.js @@ -20,4 +20,28 @@ app.use('/films', filmsRouter) app.use('/books', booksRouter) +app.use((error, req, res, next) => { + if(error instanceof NotFoundError) { + return res.status(404).json({ + message: error.message + }) + } + + if(error instanceof InvalidDataError) { + return res.status(400).json({ + message: error.message + }) + } + + if(error instanceof BookAlreadyExistsError) { + return res.status(409).json({ + message: error.message + }) + } + + res.status(500).json({ + message: "Something went wrong" + }) +}) + module.exports = app From 4e135822ee4ccee3876c1c3dfca24bd3e943fb8f Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 15:38:04 +0100 Subject: [PATCH 13/21] delets/books, get/books/id error boundary created --- src/controllers/books/books.js | 28 ++++++++++++++++++++++++++-- src/errorClasses/index.js | 6 +++++- src/server.js | 6 ++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 6b096e9..868a163 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -31,6 +31,14 @@ const getByID = (req, res) => { const id = Number(req.params.id) const found = getBookByID(id) + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Book not found") + } + res.status(200).json({ book: found }) @@ -40,6 +48,14 @@ const removeBook = (req, res) => { const id = Number(req.params.id) const found = getBookByID(id) + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Book not found") + } + deletedBooks.push(found) const index = getAllBooks().indexOf(found) getAllBooks().splice(index, 1) @@ -50,12 +66,12 @@ const removeBook = (req, res) => { const updateBook = (req, res) => { const id = Number(req.params.id) - if(typeof id !== "number") { + if (typeof id !== "number") { throw new InvalidDataError("ID must be a number") } const found = getBookByID(id) - if(!found) { + if (!found) { throw new NotFoundError("Book not found") } @@ -63,6 +79,14 @@ const updateBook = (req, res) => { found.type = req.body.type found.author = req.body.author + if ( + found.title === "" || + found.type === "" || + found.author === "" + ) { + throw new BookFieldMissing("Missing fields") + } + const checkTitle = filterByTitle(found.title) if(checkTitle) { diff --git a/src/errorClasses/index.js b/src/errorClasses/index.js index 82355d7..e4b92e0 100644 --- a/src/errorClasses/index.js +++ b/src/errorClasses/index.js @@ -7,5 +7,9 @@ class InvalidDataError extends Error { } class BookAlreadyExistsError extends Error { - + +} + +class BookFieldMissing extends Error { + } \ No newline at end of file diff --git a/src/server.js b/src/server.js index b5921d3..e12d5b4 100644 --- a/src/server.js +++ b/src/server.js @@ -39,6 +39,12 @@ app.use((error, req, res, next) => { }) } + if(error instanceof BookFieldMissing) { + return res.status(400).json({ + message: error.message + }) + } + res.status(500).json({ message: "Something went wrong" }) From 000bed275f9c9095e4c11828bad0e6d7c1aab9b4 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 15:39:36 +0100 Subject: [PATCH 14/21] post/books error boundary created --- src/controllers/books/books.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 868a163..c74c6ce 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -21,6 +21,20 @@ const addBook = (req, res) => { newBook.type = req.body.type newBook.author = req.body.author + if ( + found.title === "" || + found.type === "" || + found.author === "" + ) { + throw new BookFieldMissing("Missing fields") + } + + const checkTitle = filterByTitle(found.title) + + if(checkTitle) { + throw new BookAlreadyExistsError("Book already exists") + } + getAllBooks().push(newBook) res.status(201).json({ book: newBook From 3c0d684a2fe903ac790f8bf25349154ef790c9e6 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 16:03:43 +0100 Subject: [PATCH 15/21] put/films/id error boundary created --- src/controllers/books/books.js | 4 ++-- src/controllers/films/films.js | 31 +++++++++++++++++++++++++++++-- src/domain/films/films.js | 7 ++++++- src/errorClasses/index.js | 4 ++-- src/routers/films.js | 4 +++- src/server.js | 4 ++-- 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index c74c6ce..2bb01f6 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -26,13 +26,13 @@ const addBook = (req, res) => { found.type === "" || found.author === "" ) { - throw new BookFieldMissing("Missing fields") + throw new FieldMissing("Missing fields") } const checkTitle = filterByTitle(found.title) if(checkTitle) { - throw new BookAlreadyExistsError("Book already exists") + throw new AlreadyExistsError("Book already exists") } getAllBooks().push(newBook) diff --git a/src/controllers/films/films.js b/src/controllers/films/films.js index 7c08b43..96d3462 100644 --- a/src/controllers/films/films.js +++ b/src/controllers/films/films.js @@ -1,5 +1,5 @@ const { deletedFilms } = require('../../../data/deletedData.js') -const {getAllFilms, getFilmByID} = require('../../domain/films/films.js') +const {getAllFilms, getFilmByID, getFilmByDirector} = require('../../domain/films/films.js') const newID = require('../../functions/createID.js') let newFilm = { @@ -49,17 +49,44 @@ const updateFilm = (req, res) => { const id = Number(req.params.id) const found = getFilmByID(id) + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Film not found") + } + found.title = req.body.title found.director = req.body.director + + if ( + found.title === "" || + found.director === "" + ) { + throw new FieldMissing("Missing fields") + } + res.status(200).json({ film: found }) } +const filterByDirector = (req, res) => { + const director = req.query.director + + const found = getFilmByDirector(director) + + res.status(200).json({ + films: found + }) +} + module.exports = { getAll, addFilm, getByID, removeFIlm, - updateFilm + updateFilm, + filterByDirector } \ No newline at end of file diff --git a/src/domain/films/films.js b/src/domain/films/films.js index eda37d2..f1f52be 100644 --- a/src/domain/films/films.js +++ b/src/domain/films/films.js @@ -10,7 +10,12 @@ const getFilmByID = (id) => { return getAllFilms().find((f) => f.id === id) } +const getFilmByDirector = (d) => { + return getAllFilms().filter((f) => f.director === d) +} + module.exports = { getAllFilms, - getFilmByID + getFilmByID, + getFilmByDirector } \ No newline at end of file diff --git a/src/errorClasses/index.js b/src/errorClasses/index.js index e4b92e0..f776321 100644 --- a/src/errorClasses/index.js +++ b/src/errorClasses/index.js @@ -6,10 +6,10 @@ class InvalidDataError extends Error { } -class BookAlreadyExistsError extends Error { +class AlreadyExistsError extends Error { } -class BookFieldMissing extends Error { +class FieldMissing extends Error { } \ No newline at end of file diff --git a/src/routers/films.js b/src/routers/films.js index 746f58e..7bddf3e 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -1,5 +1,5 @@ const { Router } = require("express"); -const { getAll, addFilm, getByID, removeFIlm, updateFilm } = require("../controllers/films/films"); +const { getAll, addFilm, getByID, removeFIlm, updateFilm, filterByDirector } = require("../controllers/films/films"); const router = Router() @@ -13,4 +13,6 @@ router.delete('/:id', removeFIlm) router.put('/:id', updateFilm) +router.get('/?director=:name', filterByDirector) + module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index e12d5b4..1246525 100644 --- a/src/server.js +++ b/src/server.js @@ -33,13 +33,13 @@ app.use((error, req, res, next) => { }) } - if(error instanceof BookAlreadyExistsError) { + if(error instanceof AlreadyExistsError) { return res.status(409).json({ message: error.message }) } - if(error instanceof BookFieldMissing) { + if(error instanceof FieldMissing) { return res.status(400).json({ message: error.message }) From fbb6094e9b6b1bc78db2f36c442348400c7f30de Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 16:08:42 +0100 Subject: [PATCH 16/21] delete/films/id error boundary created --- src/controllers/books/books.js | 7 ------- src/controllers/films/films.js | 17 +++++++++++------ src/domain/films/films.js | 7 ++++++- src/errorClasses/index.js | 4 ---- src/server.js | 6 ------ 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 2bb01f6..b3257f2 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -21,13 +21,6 @@ const addBook = (req, res) => { newBook.type = req.body.type newBook.author = req.body.author - if ( - found.title === "" || - found.type === "" || - found.author === "" - ) { - throw new FieldMissing("Missing fields") - } const checkTitle = filterByTitle(found.title) diff --git a/src/controllers/films/films.js b/src/controllers/films/films.js index 96d3462..dfdf3cb 100644 --- a/src/controllers/films/films.js +++ b/src/controllers/films/films.js @@ -1,5 +1,5 @@ const { deletedFilms } = require('../../../data/deletedData.js') -const {getAllFilms, getFilmByID, getFilmByDirector} = require('../../domain/films/films.js') +const {getAllFilms, getFilmByID, getFilmByDirector, getFilmByTitle} = require('../../domain/films/films.js') const newID = require('../../functions/createID.js') let newFilm = { @@ -36,6 +36,14 @@ const getByID = (req, res) => { const removeFIlm = (req, res) => { const id = Number(req.params.id) const found = getFilmByID(id) + + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Film not found") + } deletedFilms.push(found) const index = getAllFilms().indexOf(found) @@ -60,11 +68,8 @@ const updateFilm = (req, res) => { found.title = req.body.title found.director = req.body.director - if ( - found.title === "" || - found.director === "" - ) { - throw new FieldMissing("Missing fields") + if (getFilmByTitle(req.body.title)) { + throw new AlreadyExistsError("Film already exists") } res.status(200).json({ diff --git a/src/domain/films/films.js b/src/domain/films/films.js index f1f52be..b5f1510 100644 --- a/src/domain/films/films.js +++ b/src/domain/films/films.js @@ -14,8 +14,13 @@ const getFilmByDirector = (d) => { return getAllFilms().filter((f) => f.director === d) } +const getFilmByTitle = (t) => { + return getAllFilms().find((f) => f.title === t) +} + module.exports = { getAllFilms, getFilmByID, - getFilmByDirector + getFilmByDirector, + getFilmByTitle } \ No newline at end of file diff --git a/src/errorClasses/index.js b/src/errorClasses/index.js index f776321..26acb86 100644 --- a/src/errorClasses/index.js +++ b/src/errorClasses/index.js @@ -8,8 +8,4 @@ class InvalidDataError extends Error { class AlreadyExistsError extends Error { -} - -class FieldMissing extends Error { - } \ No newline at end of file diff --git a/src/server.js b/src/server.js index 1246525..c26810a 100644 --- a/src/server.js +++ b/src/server.js @@ -39,12 +39,6 @@ app.use((error, req, res, next) => { }) } - if(error instanceof FieldMissing) { - return res.status(400).json({ - message: error.message - }) - } - res.status(500).json({ message: "Something went wrong" }) From 036aad7e54780616436c05ff7361a91f70084f02 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 16:13:39 +0100 Subject: [PATCH 17/21] get/films/id, post/films error boundaries created --- src/controllers/books/books.js | 7 +++++++ src/controllers/films/films.js | 18 +++++++++++++++++- src/errorClasses/index.js | 4 ++++ src/server.js | 6 ++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index b3257f2..4b52a0a 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -21,6 +21,13 @@ const addBook = (req, res) => { newBook.type = req.body.type newBook.author = req.body.author + if ( + newBook.title === "" || + newBook.type === "" || + newBook.author === "" + ) { + throw new FieldsMissing("Missing fields") + } const checkTitle = filterByTitle(found.title) diff --git a/src/controllers/films/films.js b/src/controllers/films/films.js index dfdf3cb..c6a8e72 100644 --- a/src/controllers/films/films.js +++ b/src/controllers/films/films.js @@ -19,6 +19,13 @@ const addFilm = (req, res) => { newFilm.title = req.body.title newFilm.director = req.body.director + if( + newFilm.title === "" || + newFilm.director === "" + ) { + throw new FieldsMissing('Missing Fields') + } + getAllFilms().push(newFilm) res.status(201).json({ film: newFilm @@ -28,6 +35,15 @@ const addFilm = (req, res) => { const getByID = (req, res) => { const id = Number(req.params.id) const found = getFilmByID(id) + + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Film not found") + } + res.status(200).json({ film: found }) @@ -36,7 +52,7 @@ const getByID = (req, res) => { const removeFIlm = (req, res) => { const id = Number(req.params.id) const found = getFilmByID(id) - + if (typeof id !== "number") { throw new InvalidDataError("ID must be a number") } diff --git a/src/errorClasses/index.js b/src/errorClasses/index.js index 26acb86..c915182 100644 --- a/src/errorClasses/index.js +++ b/src/errorClasses/index.js @@ -8,4 +8,8 @@ class InvalidDataError extends Error { class AlreadyExistsError extends Error { +} + +class FieldsMissing extends Error { + } \ No newline at end of file diff --git a/src/server.js b/src/server.js index c26810a..179b4b9 100644 --- a/src/server.js +++ b/src/server.js @@ -39,6 +39,12 @@ app.use((error, req, res, next) => { }) } + if(error instanceof FieldsMissing) { + return res.status(409).json({ + message: error.message + }) + } + res.status(500).json({ message: "Something went wrong" }) From c97a25fe9edfb1a6cf1af9950b200714506f62c0 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 16:19:11 +0100 Subject: [PATCH 18/21] put/users/id error boundary created --- src/controllers/books/books.js | 4 ++-- src/controllers/users/users.js | 9 +++++++++ src/domain/users/users.js | 7 ++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 4b52a0a..60f1187 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -98,13 +98,13 @@ const updateBook = (req, res) => { found.type === "" || found.author === "" ) { - throw new BookFieldMissing("Missing fields") + throw new FieldsMissing("Missing fields") } const checkTitle = filterByTitle(found.title) if(checkTitle) { - throw new BookAlreadyExistsError("Book already exists") + throw new AlreadyExistsError("Book already exists") } res.status(200).json({ diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index 950c44c..b992243 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -50,6 +50,15 @@ const updateUser = (req, res) => { const found = getUserById(id) found.email = req.body.email + + if (filterUserEmails(req.body.email)) { + throw new AlreadyExistsError("A user already exists with this email") + } + + if (req.body.email === "") { + throw new FieldsMissing("Email field missing") + } + res.status(200).json({ user: found }) diff --git a/src/domain/users/users.js b/src/domain/users/users.js index 08baa48..1be883a 100644 --- a/src/domain/users/users.js +++ b/src/domain/users/users.js @@ -9,7 +9,12 @@ const getUserById = (id) => { return getAllUsers().find((u) => u.id === id) } +const filterUserEmails = (email) => { + return getAllUsers().find((u) => u.email === email) +} + module.exports = { getAllUsers, - getUserById + getUserById, + filterUserEmails } \ No newline at end of file From 30803e8d4221bb934861e4435d81b88b9c4194a2 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 16:20:20 +0100 Subject: [PATCH 19/21] delete/users/id, get/users/id error boundary created --- src/controllers/users/users.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index b992243..94d35f0 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -28,6 +28,14 @@ const getByID = (req, res) => { const id = Number(req.params.id) const found = getUserById(id) + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Book not found") + } + res.status(200).json({ user: found }) @@ -37,6 +45,14 @@ const removeUser = (req, res) => { const id = Number(req.params.id) const found = getUserById(id) + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Book not found") + } + deletedUsers.push(found) const index = getAllUsers().indexOf(found) getAllUsers().splice(index, 1) From f50a9f3dd13713a7a38b4ec3eafa0731d764233d Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Tue, 18 Jun 2024 16:22:07 +0100 Subject: [PATCH 20/21] post/users error boundary created --- src/controllers/users/users.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index 94d35f0..fea865c 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -18,6 +18,15 @@ const createUser = (req, res) => { newUser.id = newID(getAllUsers()) newUser.email = req.body.email + if (filterUserEmails(req.body.email)) { + throw new AlreadyExistsError("A user already exists with this email") + } + + if (req.body.email === "") { + throw new FieldsMissing("Email field missing") + } + + getAllUsers().push(newUser) res.status(201).json({ user: newUser @@ -65,6 +74,14 @@ const updateUser = (req, res) => { const id = Number(req.params.id) const found = getUserById(id) + if (typeof id !== "number") { + throw new InvalidDataError("ID must be a number") + } + + if (!found) { + throw new NotFoundError("Book not found") + } + found.email = req.body.email if (filterUserEmails(req.body.email)) { From be0d63d7c8e2274c0558b0a35c42fce6f7b37c68 Mon Sep 17 00:00:00 2001 From: Tim Zoltie Date: Fri, 21 Jun 2024 16:18:28 +0100 Subject: [PATCH 21/21] updated for core tests to pass --- .gitignore | 1 + package-lock.json | 159 +++++++++++++++++++++++++++++++++ package.json | 2 + src/controllers/books/books.js | 22 ++--- src/controllers/films/films.js | 9 +- src/controllers/users/users.js | 10 +-- src/routers/films.js | 5 -- src/server.js | 1 + 8 files changed, 179 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 5540c35..97ca039 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules node_modules/* build build/* +.env diff --git a/package-lock.json b/package-lock.json index fc9d54a..ab44819 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,14 @@ "version": "1.0.0", "dependencies": { "cors": "^2.8.5", + "dotenv": "^16.4.5", "express": "^4.18.2", "morgan": "^1.10.0" }, "devDependencies": { "jest": "^28.1.3", "nodemon": "^2.0.22", + "pg": "^8.12.0", "supertest": "^6.3.3" } }, @@ -1853,6 +1855,17 @@ "node": "^12.13.0 || ^14.15.0 || ^16.10.0 || >=17.0.0" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -3794,6 +3807,95 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" }, + "node_modules/pg": { + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.12.0.tgz", + "integrity": "sha512-A+LHUSnwnxrnL/tZ+OLfqR1SxLN3c/pgDztZ47Rpbsd4jUytsTtwQo/TLPRzPJMp/1pbhYVhH9cuSZLAajNfjQ==", + "dev": true, + "dependencies": { + "pg-connection-string": "^2.6.4", + "pg-pool": "^3.6.2", + "pg-protocol": "^1.6.1", + "pg-types": "^2.1.0", + "pgpass": "1.x" + }, + "engines": { + "node": ">= 8.0.0" + }, + "optionalDependencies": { + "pg-cloudflare": "^1.1.1" + }, + "peerDependencies": { + "pg-native": ">=3.0.1" + }, + "peerDependenciesMeta": { + "pg-native": { + "optional": true + } + } + }, + "node_modules/pg-cloudflare": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.1.1.tgz", + "integrity": "sha512-xWPagP/4B6BgFO+EKz3JONXv3YDgvkbVrGw2mTo3D6tVDQRh1e7cqVGvyR3BE+eQgAvx1XhW/iEASj4/jCWl3Q==", + "dev": true, + "optional": true + }, + "node_modules/pg-connection-string": { + "version": "2.6.4", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.6.4.tgz", + "integrity": "sha512-v+Z7W/0EO707aNMaAEfiGnGL9sxxumwLl2fJvCQtMn9Fxsg+lPpPkdcyBSv/KFgpGdYkMfn+EI1Or2EHjpgLCA==", + "dev": true + }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "dev": true, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/pg-pool": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.6.2.tgz", + "integrity": "sha512-Htjbg8BlwXqSBQ9V8Vjtc+vzf/6fVUuak/3/XXKA9oxZprwW3IMDQTGHP+KDmVL7rtd+R1QjbnCFPuTHm3G4hg==", + "dev": true, + "peerDependencies": { + "pg": ">=8.0" + } + }, + "node_modules/pg-protocol": { + "version": "1.6.1", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.6.1.tgz", + "integrity": "sha512-jPIlvgoD63hrEuihvIg+tJhoGjUsLPn6poJY9N5CnlPd91c2T18T/9zBtLxZSb1EhYxBRoZJtzScCaWlYLtktg==", + "dev": true + }, + "node_modules/pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "dev": true, + "dependencies": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/pgpass": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz", + "integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==", + "dev": true, + "dependencies": { + "split2": "^4.1.0" + } + }, "node_modules/picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", @@ -3833,6 +3935,45 @@ "node": ">=8" } }, + "node_modules/postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", + "dev": true, + "engines": { + "node": ">=4" + } + }, + "node_modules/postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "dev": true, + "dependencies": { + "xtend": "^4.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/pretty-format": { "version": "28.1.3", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-28.1.3.tgz", @@ -4205,6 +4346,15 @@ "source-map": "^0.6.0" } }, + "node_modules/split2": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", + "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==", + "dev": true, + "engines": { + "node": ">= 10.x" + } + }, "node_modules/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -4695,6 +4845,15 @@ "node": "^12.13.0 || ^14.15.0 || >=16.0.0" } }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "dev": true, + "engines": { + "node": ">=0.4" + } + }, "node_modules/y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", diff --git a/package.json b/package.json index c63bc01..be4f7c3 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,14 @@ }, "dependencies": { "cors": "^2.8.5", + "dotenv": "^16.4.5", "express": "^4.18.2", "morgan": "^1.10.0" }, "devDependencies": { "jest": "^28.1.3", "nodemon": "^2.0.22", + "pg": "^8.12.0", "supertest": "^6.3.3" }, "keywords": [] diff --git a/src/controllers/books/books.js b/src/controllers/books/books.js index 60f1187..9abcc60 100644 --- a/src/controllers/books/books.js +++ b/src/controllers/books/books.js @@ -22,14 +22,14 @@ const addBook = (req, res) => { newBook.author = req.body.author if ( - newBook.title === "" || - newBook.type === "" || - newBook.author === "" + req.body.title === "" || + req.body.type === "" || + req.body.author === "" ) { throw new FieldsMissing("Missing fields") } - const checkTitle = filterByTitle(found.title) + const checkTitle = filterByTitle(newBook.title) if(checkTitle) { throw new AlreadyExistsError("Book already exists") @@ -80,11 +80,13 @@ const removeBook = (req, res) => { const updateBook = (req, res) => { const id = Number(req.params.id) + const found = getBookByID(id) + if (typeof id !== "number") { throw new InvalidDataError("ID must be a number") } - const found = getBookByID(id) + if (!found) { throw new NotFoundError("Book not found") } @@ -93,17 +95,9 @@ const updateBook = (req, res) => { found.type = req.body.type found.author = req.body.author - if ( - found.title === "" || - found.type === "" || - found.author === "" - ) { - throw new FieldsMissing("Missing fields") - } - const checkTitle = filterByTitle(found.title) - if(checkTitle) { + if(!checkTitle) { throw new AlreadyExistsError("Book already exists") } diff --git a/src/controllers/films/films.js b/src/controllers/films/films.js index c6a8e72..57bb998 100644 --- a/src/controllers/films/films.js +++ b/src/controllers/films/films.js @@ -8,7 +8,7 @@ let newFilm = { director: 'string' } -const getAll = (req, res) => { +const getAll = async (req, res) => { res.status(200).json({ films: getAllFilms() }) @@ -20,8 +20,8 @@ const addFilm = (req, res) => { newFilm.director = req.body.director if( - newFilm.title === "" || - newFilm.director === "" + req.body.title === "" || + req.body.director === "" ) { throw new FieldsMissing('Missing Fields') } @@ -64,6 +64,7 @@ const removeFIlm = (req, res) => { deletedFilms.push(found) const index = getAllFilms().indexOf(found) getAllFilms().splice(index, 1) + res.status(200).json({ film: found }) @@ -84,7 +85,7 @@ const updateFilm = (req, res) => { found.title = req.body.title found.director = req.body.director - if (getFilmByTitle(req.body.title)) { + if (!getFilmByTitle(found.title)) { throw new AlreadyExistsError("Film already exists") } diff --git a/src/controllers/users/users.js b/src/controllers/users/users.js index fea865c..429bc02 100644 --- a/src/controllers/users/users.js +++ b/src/controllers/users/users.js @@ -1,4 +1,4 @@ -const {getAllUsers, getUserById} = require('../../domain/users/users.js') +const {getAllUsers, getUserById, filterUserEmails} = require('../../domain/users/users.js') const newID = require('../../functions/createID.js') const {deletedUsers} = require('../../../data/deletedData.js') @@ -18,7 +18,7 @@ const createUser = (req, res) => { newUser.id = newID(getAllUsers()) newUser.email = req.body.email - if (filterUserEmails(req.body.email)) { + if (filterUserEmails(newUser.email)) { throw new AlreadyExistsError("A user already exists with this email") } @@ -84,14 +84,10 @@ const updateUser = (req, res) => { found.email = req.body.email - if (filterUserEmails(req.body.email)) { + if (!filterUserEmails(found.email)) { throw new AlreadyExistsError("A user already exists with this email") } - if (req.body.email === "") { - throw new FieldsMissing("Email field missing") - } - res.status(200).json({ user: found }) diff --git a/src/routers/films.js b/src/routers/films.js index 7bddf3e..55876b4 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -4,15 +4,10 @@ const { getAll, addFilm, getByID, removeFIlm, updateFilm, filterByDirector } = r const router = Router() router.get('/', getAll) - router.post('/', addFilm) - router.get('/:id', getByID) - router.delete('/:id', removeFIlm) - router.put('/:id', updateFilm) - router.get('/?director=:name', filterByDirector) module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index 179b4b9..0ba1edc 100644 --- a/src/server.js +++ b/src/server.js @@ -1,5 +1,6 @@ const express = require("express"); const app = express(); +require('dotenv').config() const cors = require("cors"); const morgan = require("morgan");