diff --git a/package-lock.json b/package-lock.json index fc9d54a..d3ee373 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "dependencies": { "cors": "^2.8.5", - "express": "^4.18.2", + "express": "^4.19.2", "morgan": "^1.10.0" }, "devDependencies": { @@ -1395,12 +1395,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1408,7 +1408,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -1719,9 +1719,9 @@ "dev": true }, "node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } @@ -1992,16 +1992,16 @@ } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.19.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.19.2.tgz", + "integrity": "sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -3914,9 +3914,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index c63bc01..69949f3 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ }, "dependencies": { "cors": "^2.8.5", - "express": "^4.18.2", + "express": "^4.19.2", "morgan": "^1.10.0" }, "devDependencies": { diff --git a/src/routers/books.js b/src/routers/books.js index 18b9a7c..fcc6a0d 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,4 +1,141 @@ +const router = require("express").Router(); // Import data here... - +let books = require("../../data/index.js").books; // Write routes here... +let newBookId = 4; + +router.get("/", (req, res) => { + res.status(200).json({ books }); +}); + +router.post("/", (req, res) => { + const newBook = req.body; + // const requiredProperties = ["title", "type", "author", "pages"]; + + // for (const item of requiredProperties) { + // if (newBook[item] === undefined) { + // return res + // .status(400) + // .json({ error: "Missing fields in request body" }); + // } + // } + + if (!newBook.title || !newBook.author || !newBook.type) { + res.status(400).json({ error: "Missing fields in request body" }); + } + + const matchedBook = books.find((book) => book.title === newBook.title); + + if (matchedBook) { + res.status(409).json({ error: "A book with the provided title already exists" }); + } else { + newBookId += 1; + newBook.id = newBookId; + books.push(newBook); + res.status(201).json({ book: newBook }); + } +}); + +router.get("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundBook = books.find((book) => book.id === id); + + if (!foundBook) { + res + .status(404) + .json({ error: "A book the provided ID does not exist" }); + } + + res.status(200).json({ book: foundBook }); +}); + +router.delete("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundBook = books.find((book) => book.id === id); + + if (!foundBook) { + res + .status(404) + .json({ error: "A book with the provided ID does not exist" }); + } + + books = books.filter((book) => book.id !== foundBook.id); + + res.status(200).json({ book: foundBook }); +}); + +router.put("/:id", (req, res) => { + const updatedBook = req.body; + const id = Number(req.params.id); + if ( + !updatedBook.title || + !updatedBook.author || + !updatedBook.type + ) { + res.status(400).json({ error: "Missing fields in request body" }); + } + + const foundBook = books.find((book) => book.id === id); + + if (!foundBook) { + res + .status(404) + .json({ error: "A book with the provided ID does not exist" }); + } + + const matchedBookTitle = books.find( + (book) => book.title === updatedBook.title + ); + + if (matchedBookTitle) { + res + .status(409) + .json({ error: "A book with the provided title already exists" }); + } + + const existingBookIndex = books.findIndex((book) => book.id === id); + + updatedBook.id = id; + + books.splice(existingBookIndex, 1, updatedBook); + + res.status(200).json({ book: updatedBook }); +}); + +router.patch("/:id", (req, res) => { + const updatedBook = req.body; + const id = Number(req.params.id); + + const foundBook = books.find((book) => book.id === id); + + if (!foundBook) { + res + .status(404) + .json({ error: "A book with the provided ID does not exist" }); + } + + const matchedBookTitle = books.find( + (book) => book.title === updatedBook.title + ); + + if (matchedBookTitle) { + return res + .status(409) + .json({ error: "A book with the provided title already exists" }); + } + + const existingBookIndex = books.findIndex((book) => book.id === id); + + books[existingBookIndex] = { + ...books[existingBookIndex], + ...updatedBook, + }; + + return res.status(400).json({ book: updatedBook }); + +}); + +module.exports = router; diff --git a/src/routers/films.js b/src/routers/films.js index e69de29..94d8642 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -0,0 +1,127 @@ +const router = require("express").Router(); + +let films = require("../../data/index.js").films; + +let newFilmId = 4; + +router.get("/", (req, res) => { + if (req.query.director) { + const director = req.query.director; + const filteredFilms = films.filter((film) => film.director === director); + + res.status(200).json({ films: filteredFilms }); + } + res.status(200).json({ films }); +}); + +router.post("/", (req, res) => { + const newFilm = req.body; + + if (!newFilm.title || !newFilm.director) { + return res.status(400).json({ error: "Missing fields in request body" }); + } + + const matchedFilm = films.find((film) => film.title === newFilm.title); + + if (matchedFilm) { + res + .status(409) + .json({ error: "A film with the provided title already exists" }); + } + + newFilmId += 1; + newFilm.id = newFilmId; + + films.push(newFilm); + res.status(201).json({ film: newFilm }); + +}); + +router.get("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundFilm = films.find((film) => film.id === id); + + if (!foundFilm) { + res.status(404).json({ error: "A film with provided ID does not exist" }); + } + + res.status(200).json({ film: foundFilm }); +}); + +router.delete("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundFilm = films.find((film) => film.id === id); + + if (!foundFilm) { + res.status(404).json({ error: "A film with provided ID does not exist" }); + } + + films = films.filter((film) => film.id !== foundFilm.id); + + res.status(200).json({ film: foundFilm }); +}); + +router.put("/:id", (req, res) => { + const updatedFilm = req.body; + const id = Number(req.params.id); + + const foundFilm = films.find((film) => film.id === id); + + if (!foundFilm) { + res.status(404).json({ error: "A film with provided ID does not exist" }); + } + + const matchedFilm = films.find((film) => film.title === updatedFilm.title); + + if (matchedFilm) { + res + .status(409) + .json({ error: "A film with the provided title already exists" }); + } + + const existingFilmIndex = films.findIndex((film) => film.id === id); + + updatedFilm.id = id; + + films.splice(existingFilmIndex, 1, updatedFilm); + + res.status(200).json({ film: updatedFilm }); +}); + +router.patch("/:id", (req, res) => { + const updatedFilm = req.body; + const id = Number(req.params.id); + + if (!updatedFilm.title) { + return res.status(400).json({ error: "Missing fields in request body" }); + } + + const foundFilm = films.find((film) => film.id === id); + + if (!foundFilm) { + res.status(404).json({ error: "A film with provided ID does not exist" }); + } + + const matchedFilmTitle = films.find( + (film) => film.title === updatedFilm.title + ); + + if (matchedFilmTitle) { + res + .status(409) + .json({ error: "A film with the provided title already exists" }); + } + + const existingFilmIndex = films.findIndex((film) => film.id === id); + + films[existingFilmIndex] = { + ...films[existingFilmIndex], + ...updatedFilm, + }; + + res.status(200).json({ film: updatedFilm }); +}); + +module.exports = router; diff --git a/src/routers/users.js b/src/routers/users.js index e69de29..c0f4105 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -0,0 +1,82 @@ +const router = require("express").Router(); + +let users = require("../../data/index.js").users; +let newUserId = 3; + +router.get("/", (req, res) => { + res.status(200).json({ users }); +}); + +router.post("/", (req, res) => { + const newUser = req.body; + if(!newUser.email) { + return res.status(400).json({ error: "Missing fields in request body" }); + } + + const matchedUser = users.find((user) => user.email === newUser.email); + + if(matchedUser) { + res.status(409).json({ error: "A user with the provided email already exists" }); + } else { + newUserId += 1; + newUser.id = newUserId; + users.push(newUser); + res.status(201).json({ user : newUser }); + } +}); + +router.get("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundUser = users.find((user) => user.id === id); + + if (!foundUser) { + res.status(404).json({ error: "A user with the provided ID does not exist" }); + }; + + res.status(200).json({ user: foundUser }); +}); + +router.delete("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundUser = users.find((user) => user.id === id); + + if(!foundUser) { + res.status(404).json({ error: "A user with the provided ID does not exist" }); + } + + users = users.filter((user) => user.id !== foundUser.id); + res.status(200).json({ user: foundUser }); + +}); + +router.put("/:id", (req, res) => { + const updatedUser = req.body; + const id = Number(req.params.id); + updatedUser.id = id; + + if (!updatedUser.email) { + return res.status(400).json({ error: "Missing fields in request body" }); + } + + const matchedUser = users.find((user) => user.email === updatedUser.email); + + if(matchedUser) { + res.status(409).json({ error: "A user with the provided email already exists" }); + } + + const matchedUserID = users.find((user) => user.id === updatedUser.id); + + if(!matchedUserID) { + res.status(404).json({ error: "A user with the provided ID does not exist" }); + } + + const existingUserIndex = users.findIndex((user) => user.id === id); + + users.splice(existingUserIndex, 1, updatedUser); + + res.status(200).json({ user: updatedUser }); +}); + +module.exports = router; diff --git a/src/server.js b/src/server.js index 715321f..03be555 100644 --- a/src/server.js +++ b/src/server.js @@ -11,8 +11,12 @@ app.use(morgan("dev")); // REQUIRE ROUTERS 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); - -module.exports = app +module.exports = app; diff --git a/test/api/extensions/books.spec.js b/test/api/extensions/books.spec.js index 4889b8f..a13e135 100644 --- a/test/api/extensions/books.spec.js +++ b/test/api/extensions/books.spec.js @@ -54,7 +54,7 @@ describe("Books Extension Endpoint", () => { }) expect(response.status).toEqual(404) - expect(response.body.error).toEqual('A book the provided ID does not exist') + expect(response.body.error).toEqual('A book with the provided ID does not exist') }) it("will return 409 when updating a book with title that already exists", async () => { const response = await supertest(app).put("/books/1").send({ @@ -70,17 +70,17 @@ describe("Books Extension Endpoint", () => { describe("PATCH /books", () => { it("will return 404 when updating a book that does not exist", async () => { - const response = await supertest(app).put("/books/999").send({ + const response = await supertest(app).patch("/books/999").send({ title: "test1", type: "test1", author: "test1", }) expect(response.status).toEqual(404) - expect(response.body.error).toEqual('A book the provided ID does not exist') + expect(response.body.error).toEqual('A book with the provided ID does not exist') }) it("will return 409 when updating a book with title that already exists", async () => { - const response = await supertest(app).put("/books/1").send({ + const response = await supertest(app).patch("/books/1").send({ title: "1984", type: "test1", author: "test1", @@ -96,7 +96,7 @@ describe("Books Extension Endpoint", () => { const response = await supertest(app).delete("/books/999") expect(response.status).toEqual(404) - expect(response.body.error).toEqual('A book the provided ID does not exist') + expect(response.body.error).toEqual('A book with the provided ID does not exist') }) }) })