From 790be5d99096a95f344808efcdf43df9de546958 Mon Sep 17 00:00:00 2001 From: Zainab Choudhry Date: Wed, 31 Jul 2024 13:57:28 +0100 Subject: [PATCH 1/5] Added get/post/update/delete functionality for /users --- package-lock.json | 34 +++++++++++++++--------------- package.json | 2 +- src/routers/users.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/server.js | 2 +- 4 files changed, 69 insertions(+), 19 deletions(-) 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/users.js b/src/routers/users.js index e69de29..e659f69 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -0,0 +1,50 @@ +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 user = req.body; + newUserId += 1; + user.id = newUserId; + + users.push(user); + res.status(201).json({ user }); +}); + +router.get("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundUser = users.find((user) => user.id === id); + + 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); + + 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); + + const existingUserIndex = users.findIndex((user) => user.id === id); + + updatedUser.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..4c97071 100644 --- a/src/server.js +++ b/src/server.js @@ -13,6 +13,6 @@ app.use(morgan("dev")); const usersRouter = require("./routers/users"); // ADD ROUTERS TO APP - +app.use('/users', usersRouter) module.exports = app From 299ae622eb5f3161663b1c9fd942346b199841a2 Mon Sep 17 00:00:00 2001 From: Zainab Choudhry Date: Wed, 31 Jul 2024 14:28:13 +0100 Subject: [PATCH 2/5] Added get/post/update/delete/patch functionality for /books --- src/routers/books.js | 65 +++++++++++++++++++++++++++++++++++++++++++- src/server.js | 4 ++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/routers/books.js b/src/routers/books.js index 18b9a7c..08ad37c 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,4 +1,67 @@ +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 book = req.body; + newBookId += 1; + book.id = newBookId; + + books.push(book); + res.status(201).json({ book }); +}); + +router.get("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundBook = books.find((book) => book.id === id); + + 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); + + 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); + + 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 existingBookIndex = books.findIndex((book) => book.id === id); + + books[existingBookIndex] = { + ...books[existingBookIndex], + ...updatedBook, + }; + + res.status(200).json({ book: updatedBook }); + +}); + +module.exports = router; \ No newline at end of file diff --git a/src/server.js b/src/server.js index 4c97071..928cb99 100644 --- a/src/server.js +++ b/src/server.js @@ -11,8 +11,10 @@ app.use(morgan("dev")); // REQUIRE ROUTERS const usersRouter = require("./routers/users"); +const booksRouter = require("./routers/books"); // ADD ROUTERS TO APP -app.use('/users', usersRouter) +app.use('/users', usersRouter); +app.use('/books', booksRouter); module.exports = app From bed1ae8f6a9082e6a24d0f18b6c626eeeb4aa3c5 Mon Sep 17 00:00:00 2001 From: Zainab Choudhry Date: Wed, 31 Jul 2024 14:45:22 +0100 Subject: [PATCH 3/5] Added get/post/update/delete/patch functionality for /films --- src/routers/films.js | 66 ++++++++++++++++++++++++++++++++++++++++++++ src/server.js | 8 ++++-- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/routers/films.js b/src/routers/films.js index e69de29..556c2f0 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -0,0 +1,66 @@ +const router = require("express").Router(); + +let films = require("../../data/index.js").films; + +let newFilmId = 4; + +router.get("/", (req, res) => { + res.status(200).json({ films }); +}); + +router.post("/", (req, res) => { + const film = req.body; + newFilmId += 1; + film.id = newFilmId; + + films.push(film); + res.status(201).json({ film }); +}); + +router.get("/:id", (req, res) => { + const id = Number(req.params.id); + + const foundFilm = films.find((film) => film.id === id); + + 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); + + 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 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); + + const existingFilmIndex = films.findIndex((film) => film.id === id); + + films[existingFilmIndex] = { + ...films[existingFilmIndex], + ...updatedFilm, + }; + + res.status(200).json({ film: updatedFilm }); +}); + + +module.exports = router; \ No newline at end of file diff --git a/src/server.js b/src/server.js index 928cb99..03be555 100644 --- a/src/server.js +++ b/src/server.js @@ -12,9 +12,11 @@ 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("/users", usersRouter); +app.use("/books", booksRouter); +app.use("/films", filmsRouter); -module.exports = app +module.exports = app; From 4b11d820982cf289b4200cff17d8d5fd78ce6c35 Mon Sep 17 00:00:00 2001 From: Zainab Choudhry Date: Wed, 31 Jul 2024 18:18:52 +0100 Subject: [PATCH 4/5] Extensions complete --- src/routers/books.js | 104 +++++++++++++++++++++++++----- src/routers/films.js | 74 +++++++++++++++++++-- src/routers/users.js | 47 ++++++++++++-- test/api/extensions/books.spec.js | 10 +-- 4 files changed, 201 insertions(+), 34 deletions(-) diff --git a/src/routers/books.js b/src/routers/books.js index 08ad37c..575db5d 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -10,12 +10,31 @@ router.get("/", (req, res) => { }); router.post("/", (req, res) => { - const book = req.body; - newBookId += 1; - book.id = newBookId; - - books.push(book); - res.status(201).json({ book }); + 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({ newBook }); + } }); router.get("/:id", (req, res) => { @@ -23,6 +42,12 @@ router.get("/:id", (req, res) => { 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 }); }); @@ -31,6 +56,12 @@ router.delete("/:id", (req, res) => { 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 }); @@ -39,6 +70,31 @@ router.delete("/:id", (req, res) => { 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); @@ -50,18 +106,36 @@ router.put("/:id", (req, res) => { }); router.patch("/:id", (req, res) => { - const updatedBook = req.body; - const id = Number(req.params.id); + const updatedBook = req.body; + const id = Number(req.params.id); + + const foundBook = books.find((book) => book.id === id); - const existingBookIndex = books.findIndex((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, - }; + books[existingBookIndex] = { + ...books[existingBookIndex], + ...updatedBook, + }; - res.status(200).json({ book: updatedBook }); + return res.status(400).json({ book: updatedBook }); }); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/src/routers/films.js b/src/routers/films.js index 556c2f0..7a291b4 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -5,16 +5,35 @@ 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 film = req.body; - newFilmId += 1; - film.id = newFilmId; + 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); - films.push(film); - res.status(201).json({ film }); + if (matchedFilm) { + res + .status(409) + .json({ error: "A film with the provided title already exists" }); + } else { + newFilmId += 1; + newFilm.id = newFilmId; + + films.push(newFilm); + res.status(201).json({ newFilm }); + } }); router.get("/:id", (req, res) => { @@ -22,6 +41,10 @@ router.get("/:id", (req, res) => { 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 }); }); @@ -30,6 +53,10 @@ router.delete("/:id", (req, res) => { 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 }); @@ -39,6 +66,20 @@ 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; @@ -52,6 +93,26 @@ 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] = { @@ -62,5 +123,4 @@ router.patch("/:id", (req, res) => { res.status(200).json({ film: updatedFilm }); }); - -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/src/routers/users.js b/src/routers/users.js index e659f69..ea62040 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -8,12 +8,21 @@ router.get("/", (req, res) => { }); router.post("/", (req, res) => { - const user = req.body; - newUserId += 1; - user.id = newUserId; - - users.push(user); - res.status(201).json({ user }); + 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({ newUser }); + } }); router.get("/:id", (req, res) => { @@ -21,6 +30,10 @@ router.get("/:id", (req, res) => { 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 }); }); @@ -29,15 +42,35 @@ router.delete("/:id", (req, res) => { const foundUser = users.find((user) => user.id === id); - users = users.filter((user) => user.id !== foundUser.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); + 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); updatedUser.id = id; 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') }) }) }) From f9f257e0d9bc8ef92cb41394dc7f39264d0e5e39 Mon Sep 17 00:00:00 2001 From: Zainab Choudhry Date: Wed, 31 Jul 2024 18:42:46 +0100 Subject: [PATCH 5/5] Fixed bugs --- src/routers/books.js | 2 +- src/routers/films.js | 7 ++++--- src/routers/users.js | 5 ++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/routers/books.js b/src/routers/books.js index 575db5d..fcc6a0d 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -33,7 +33,7 @@ router.post("/", (req, res) => { newBookId += 1; newBook.id = newBookId; books.push(newBook); - res.status(201).json({ newBook }); + res.status(201).json({ book: newBook }); } }); diff --git a/src/routers/films.js b/src/routers/films.js index 7a291b4..94d8642 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -27,13 +27,14 @@ router.post("/", (req, res) => { res .status(409) .json({ error: "A film with the provided title already exists" }); - } else { + } + newFilmId += 1; newFilm.id = newFilmId; films.push(newFilm); - res.status(201).json({ newFilm }); - } + res.status(201).json({ film: newFilm }); + }); router.get("/:id", (req, res) => { diff --git a/src/routers/users.js b/src/routers/users.js index ea62040..c0f4105 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -21,7 +21,7 @@ router.post("/", (req, res) => { newUserId += 1; newUser.id = newUserId; users.push(newUser); - res.status(201).json({ newUser }); + res.status(201).json({ user : newUser }); } }); @@ -54,6 +54,7 @@ router.delete("/:id", (req, res) => { 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" }); @@ -73,8 +74,6 @@ router.put("/:id", (req, res) => { const existingUserIndex = users.findIndex((user) => user.id === id); - updatedUser.id = id; - users.splice(existingUserIndex, 1, updatedUser); res.status(200).json({ user: updatedUser });