From 0cc8b3bd78264e055cf9b0c23b9da41bf647348a Mon Sep 17 00:00:00 2001 From: Periklis Date: Tue, 18 Jun 2024 14:09:08 +0300 Subject: [PATCH 1/7] initial commit -npm install etc --- package-lock.json | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index fc9d54a..2f9ad09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" }, @@ -1428,12 +1428,12 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -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", @@ -2054,9 +2054,9 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, "dependencies": { "to-regex-range": "^5.0.1" @@ -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", From cd52027624fb1ce30bd7bb234a52495dd39b6415 Mon Sep 17 00:00:00 2001 From: Periklis Date: Tue, 18 Jun 2024 22:24:39 +0300 Subject: [PATCH 2/7] refactot to include functionality in the controllers --- data/index.js | 2 +- package-lock.json | 69 ++++++++++++++++++------------ package.json | 12 ++++-- src/controllers/booksController.js | 44 +++++++++++++++++++ src/controllers/filmsController.js | 0 src/controllers/usersController.js | 38 ++++++++++++++++ src/domain/booksRep.js | 42 ++++++++++++++++++ src/domain/filmsRep.js | 0 src/domain/usersRep.js | 42 ++++++++++++++++++ src/routers/books.js | 17 +++++++- src/routers/films.js | 14 ++++++ src/routers/users.js | 16 +++++++ src/server.js | 7 ++- 13 files changed, 270 insertions(+), 33 deletions(-) create mode 100644 src/controllers/booksController.js create mode 100644 src/controllers/filmsController.js create mode 100644 src/controllers/usersController.js create mode 100644 src/domain/booksRep.js create mode 100644 src/domain/filmsRep.js create mode 100644 src/domain/usersRep.js diff --git a/data/index.js b/data/index.js index 7a216ce..a7002cd 100644 --- a/data/index.js +++ b/data/index.js @@ -1,4 +1,4 @@ -const books = [ + const books = [ { id: 1, title: "1984", diff --git a/package-lock.json b/package-lock.json index 2f9ad09..e6aa062 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "express-router-with-in-memory-data-store", "version": "1.0.0", + "license": "ISC", "dependencies": { "cors": "^2.8.5", "express": "^4.18.2", @@ -14,7 +15,7 @@ }, "devDependencies": { "jest": "^28.1.3", - "nodemon": "^2.0.22", + "nodemon": "^3.1.3", "supertest": "^6.3.3" } }, @@ -3512,18 +3513,18 @@ "dev": true }, "node_modules/nodemon": { - "version": "2.0.22", - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.22.tgz", - "integrity": "sha512-B8YqaKMmyuCO7BowF1Z1/mkPqLk6cs/l63Ojtd6otKjMx47Dq1utxfRxcavH1I7VSaL8n5BUaoutadnsX3AAVQ==", + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.3.tgz", + "integrity": "sha512-m4Vqs+APdKzDFpuaL9F9EVOF85+h070FnkHVEoU4+rmT6Vw0bmNl7s61VEkY/cJkL7RCv1p4urnUDUMrS5rk2w==", "dev": true, "dependencies": { "chokidar": "^3.5.2", - "debug": "^3.2.7", + "debug": "^4", "ignore-by-default": "^1.0.1", "minimatch": "^3.1.2", "pstree.remy": "^1.1.8", - "semver": "^5.7.1", - "simple-update-notifier": "^1.0.7", + "semver": "^7.5.3", + "simple-update-notifier": "^2.0.0", "supports-color": "^5.5.0", "touch": "^3.1.0", "undefsafe": "^2.0.5" @@ -3532,7 +3533,7 @@ "nodemon": "bin/nodemon.js" }, "engines": { - "node": ">=8.10.0" + "node": ">=10" }, "funding": { "type": "opencollective", @@ -3540,12 +3541,20 @@ } }, "node_modules/nodemon/node_modules/debug": { - "version": "3.2.7", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz", - "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==", + "version": "4.3.5", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz", + "integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==", "dev": true, "dependencies": { - "ms": "^2.1.1" + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } } }, "node_modules/nodemon/node_modules/has-flag": { @@ -3558,18 +3567,21 @@ } }, "node_modules/nodemon/node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, "node_modules/nodemon/node_modules/semver": { - "version": "5.7.2", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz", - "integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==", + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz", + "integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==", "dev": true, "bin": { - "semver": "bin/semver" + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" } }, "node_modules/nodemon/node_modules/supports-color": { @@ -4151,24 +4163,27 @@ "dev": true }, "node_modules/simple-update-notifier": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-1.1.0.tgz", - "integrity": "sha512-VpsrsJSUcJEseSbMHkrsrAVSdvVS5I96Qo1QAQ4FxQ9wXFcB+pjj7FB7/us9+GcgfW4ziHtYMc1J0PLczb55mg==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz", + "integrity": "sha512-a2B9Y0KlNXl9u/vsW6sTIu9vGEpfKu2wRV6l1H3XEas/0gUIzGzBoP/IouTcUQbm9JWZLH3COxyn03TYlFax6w==", "dev": true, "dependencies": { - "semver": "~7.0.0" + "semver": "^7.5.3" }, "engines": { - "node": ">=8.10.0" + "node": ">=10" } }, "node_modules/simple-update-notifier/node_modules/semver": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.0.0.tgz", - "integrity": "sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A==", + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz", + "integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==", "dev": true, "bin": { "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" } }, "node_modules/sisteransi": { diff --git a/package.json b/package.json index c63bc01..d70e537 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "express-router-with-in-memory-data-store", "version": "1.0.0", - "description": "", + "description": "In this exercise, you are going to extend the REST APIs you have been building to handle DELETE and PUT requests. You will also learn how to make your route handling more modular in express using express.Router.", "main": "index.js", "scripts": { "start": "npx nodemon src/index.js", @@ -15,8 +15,14 @@ }, "devDependencies": { "jest": "^28.1.3", - "nodemon": "^2.0.22", + "nodemon": "^3.1.3", "supertest": "^6.3.3" }, - "keywords": [] + "keywords": [], + "directories": { + "doc": "docs", + "test": "test" + }, + "author": "", + "license": "ISC" } diff --git a/src/controllers/booksController.js b/src/controllers/booksController.js new file mode 100644 index 0000000..086f739 --- /dev/null +++ b/src/controllers/booksController.js @@ -0,0 +1,44 @@ +const { + getAllBks, + createNewBk, + getBkById, + deleteBk, + updateBkById, +} = require("../domain/booksRep.js") + +const getAllBooks = (req, res) => { + const users = getAllBks() + + res.status(200).json({ books }) +} + +const createNewBook = (req, res) => { + const newBook = createNewBk(req.body) + res.status(201).json({ newBook }) +} + +const getBookById = (req, res) => { + const foundBook = getUsrById(Number(req.params.id)) + res.status(200).json({ foundBook }) +} + +const deleteBook = (req, res) => { + const book = deleteBk(Number(req.params.id)) + res.status(200).json({ book }) +} + +const updateBookById = (req, res) => { + const bookId = Number(req.params.id) + const data = req.body + + const updateBook = updateBkById + res.status(200).json({ updateUser }) +} + +module.exports = { + getAllBooks, + createNewBook, + getBookById, + deleteBook, + updateBookById, +} diff --git a/src/controllers/filmsController.js b/src/controllers/filmsController.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controllers/usersController.js b/src/controllers/usersController.js new file mode 100644 index 0000000..5bcd897 --- /dev/null +++ b/src/controllers/usersController.js @@ -0,0 +1,38 @@ +const { + getAllUsr, + createNewUsr, + getUsrById, + deleteUsrById, + updateUsrById, +} = require("../domain/usersRep.js") + +const getAllUsers = (req, res) => { + const users = getAllUsr() + + res.status(200).json({ users }) +} + +const createNewUser = (req, res) => { + const newUser = createNewUsr(req.body) + res.status(201).json({ newUser }) +} + +const getUserById = (req, res) => { + const foundUser = getUsrById(Number(req.params.id)) + res.status(200).json({ foundUser }) +} + +const deleteUserById = (req, res) => { + const user = deleteUsrById(Number(req.params.id)) + res.status(200).json({user}) +} + +const updateUserById = (req, res) => { + const usrId = Number(req.params.id) + const data = req.body + + const updateUser = updateUsrById(usrId,data) + res.status(200).json({updateUser}) +} + +module.exports = { getAllUsers, createNewUser, getUserById, deleteUserById, updateUserById } diff --git a/src/domain/booksRep.js b/src/domain/booksRep.js new file mode 100644 index 0000000..8f0e863 --- /dev/null +++ b/src/domain/booksRep.js @@ -0,0 +1,42 @@ +const { books } = require("../../data/index.js") +// const usersDb = require(users) + +let newBkId = books.length + 1 + +const getAllBks = () => { + return books +} + +const createNewBk = (data) => { + const newBk = data + newBk.id = newBkId + newUsrId += 1 + books.push(newBk) +} + +const getBkById = (id) => { + return books.find((b) => b.id === id) +} + +const deleteBk = (id) => { + const bookToDelete = books.find((b) => b.id === id) + books = books.filter((b) => b.id !== id) + + return bookToDelete +} + +const updateBkById = (id, data) => { + const bookId = id + const updatedBook = data + updatedBook.id = bookId + books.splice(bookId - 1, 1, updatedBook) + return updatedUser +} + +module.exports = { + getAllBks, + createNewBk, + getBkById, + deleteBk, + updateBkById, +} diff --git a/src/domain/filmsRep.js b/src/domain/filmsRep.js new file mode 100644 index 0000000..e69de29 diff --git a/src/domain/usersRep.js b/src/domain/usersRep.js new file mode 100644 index 0000000..fd8857e --- /dev/null +++ b/src/domain/usersRep.js @@ -0,0 +1,42 @@ +const { users } = require("../../data/index.js") +// const usersDb = require(users) + +let newUsrId = users.length + 1 + +const getAllUsr = () => { + return users +} + +const createNewUsr = (data) => { + const newUser = data + newUser.id = newUsrId + newUsrId += 1 + users.push(newUser) +} + +const getUsrById = (id) => { + return users.find((u) => u.id === id) +} + +const deleteUsrById = (id) => { + const userToDelete = users.find((u) => u.id === id) + users = users.filter((u) => u.id !== id) + + return userToDelete +} + +const updateUsrById = (id, data) => { + const userId = id + const updatedUser = data + updatedUser.id = userId + users.splice(userId - 1, 1, updatedUser) + return updatedUser +} + +module.exports = { + getAllUsr, + createNewUsr, + getUsrById, + deleteUsrById, + updateUsrById, +} diff --git a/src/routers/books.js b/src/routers/books.js index 18b9a7c..14b7c25 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,4 +1,19 @@ // Import data here... - +const express = require("express") +const booksRouter = express.Router() +const {getAllBooks,createNewBook,getBookById,deleteBookById,updateBookById} = require('../controllers/booksController.js') // Write routes here... +booksRouter.get("/", getAllBooks) + +booksRouter.post("/", createNewBook) + +booksRouter.get("/:id", getBookById) + +booksRouter.delete("/:id", deleteBookById) + +booksRouter.put("/:id", updateBookById) + +module.exports = booksRouter + + diff --git a/src/routers/films.js b/src/routers/films.js index e69de29..7ac8492 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -0,0 +1,14 @@ +const express = require("express") +const filmsRouter = express.Router() + +filmsRouter.get("/", getAllFilms) + +filmsRouter.post("/", createNewFilm) + +filmsRouter.get("/:id", getFilmById) + +filmsRouter.delete("/:id", deleteFilmById) + +filmsRouter.put("/:id", updateFilmById) + +module.exports = filmsRouter diff --git a/src/routers/users.js b/src/routers/users.js index e69de29..3ad73d5 100644 --- a/src/routers/users.js +++ b/src/routers/users.js @@ -0,0 +1,16 @@ +const express = require('express') +const usersRouter = express.Router() +const {getAllUsers,createNewUser,getUserById,deleteUserById,updateUserById} = require('../controllers/usersController.js') + +usersRouter.get('/', getAllUsers) + +usersRouter.post('/', createNewUser) + +usersRouter.get('/:id', getUserById) + +usersRouter.delete('/:id', deleteUserById) + +usersRouter.put('/:id', updateUserById) + + +module.exports = usersRouter diff --git a/src/server.js b/src/server.js index 715321f..733af5c 100644 --- a/src/server.js +++ b/src/server.js @@ -10,9 +10,14 @@ app.use(express.json()); app.use(morgan("dev")); // REQUIRE ROUTERS -const usersRouter = require("./routers/users"); +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("/books", booksRouter) + module.exports = app From 86d11f6d8aab02213ea86abe6b1ca49d9ba3ec56 Mon Sep 17 00:00:00 2001 From: Periklis Date: Tue, 18 Jun 2024 22:53:42 +0300 Subject: [PATCH 3/7] users refactored -pass --- src/controllers/usersController.js | 50 +++++++++++++++++------------- src/domain/usersRep.js | 42 ------------------------- 2 files changed, 29 insertions(+), 63 deletions(-) delete mode 100644 src/domain/usersRep.js diff --git a/src/controllers/usersController.js b/src/controllers/usersController.js index 5bcd897..739dd57 100644 --- a/src/controllers/usersController.js +++ b/src/controllers/usersController.js @@ -1,38 +1,46 @@ -const { - getAllUsr, - createNewUsr, - getUsrById, - deleteUsrById, - updateUsrById, -} = require("../domain/usersRep.js") +const usersData = require("../../data/index.js") +const users = usersData.users -const getAllUsers = (req, res) => { - const users = getAllUsr() +let newUserId = users.length + 1 +const getAllUsers = (req, res) => { res.status(200).json({ users }) } const createNewUser = (req, res) => { - const newUser = createNewUsr(req.body) - res.status(201).json({ newUser }) + const newUser = req.body + newUser.id = newUserId + newUserId += 1 + users.push(newUser) + res.status(201).json({ user: newUser }) } const getUserById = (req, res) => { - const foundUser = getUsrById(Number(req.params.id)) - res.status(200).json({ foundUser }) + const userId = Number(req.params.id) + const foundUser = users.find(u=>u.id === userId) + res.status(200).json({user: foundUser }) } const deleteUserById = (req, res) => { - const user = deleteUsrById(Number(req.params.id)) - res.status(200).json({user}) + const userId = Number(req.params.id) + const userToDelete = users.find((u) => u.id === userId) + const indexToDelete = users.indexOf(userToDelete) + users.splice(indexToDelete,1) + res.status(200).json({ user: userToDelete }) } const updateUserById = (req, res) => { - const usrId = Number(req.params.id) - const data = req.body - - const updateUser = updateUsrById(usrId,data) - res.status(200).json({updateUser}) + const userId = Number(req.params.id) + const updateUser = req.body + updateUser.id = userId + users.splice(userId - 1, 1, updateUser) + res.status(200).json({ user: updateUser }) } -module.exports = { getAllUsers, createNewUser, getUserById, deleteUserById, updateUserById } +module.exports = { + getAllUsers, + createNewUser, + getUserById, + deleteUserById, + updateUserById, +} diff --git a/src/domain/usersRep.js b/src/domain/usersRep.js deleted file mode 100644 index fd8857e..0000000 --- a/src/domain/usersRep.js +++ /dev/null @@ -1,42 +0,0 @@ -const { users } = require("../../data/index.js") -// const usersDb = require(users) - -let newUsrId = users.length + 1 - -const getAllUsr = () => { - return users -} - -const createNewUsr = (data) => { - const newUser = data - newUser.id = newUsrId - newUsrId += 1 - users.push(newUser) -} - -const getUsrById = (id) => { - return users.find((u) => u.id === id) -} - -const deleteUsrById = (id) => { - const userToDelete = users.find((u) => u.id === id) - users = users.filter((u) => u.id !== id) - - return userToDelete -} - -const updateUsrById = (id, data) => { - const userId = id - const updatedUser = data - updatedUser.id = userId - users.splice(userId - 1, 1, updatedUser) - return updatedUser -} - -module.exports = { - getAllUsr, - createNewUsr, - getUsrById, - deleteUsrById, - updateUsrById, -} From 0f9695ab80416b05a305f0ba10cfaf07ac9fa3ac Mon Sep 17 00:00:00 2001 From: Periklis Date: Tue, 18 Jun 2024 23:22:16 +0300 Subject: [PATCH 4/7] books and films refactored -core tests pass --- src/controllers/booksController.js | 46 ++++++++++++++++-------------- src/controllers/filmsController.js | 46 ++++++++++++++++++++++++++++++ src/controllers/usersController.js | 2 +- src/routers/books.js | 10 +++++-- src/routers/films.js | 9 ++++++ src/server.js | 7 +++-- 6 files changed, 91 insertions(+), 29 deletions(-) diff --git a/src/controllers/booksController.js b/src/controllers/booksController.js index 086f739..64eb3c0 100644 --- a/src/controllers/booksController.js +++ b/src/controllers/booksController.js @@ -1,44 +1,46 @@ -const { - getAllBks, - createNewBk, - getBkById, - deleteBk, - updateBkById, -} = require("../domain/booksRep.js") +const booksData = require("../../data/index.js") +const books = booksData.books -const getAllBooks = (req, res) => { - const users = getAllBks() +let newBookId = books.length + 1 - res.status(200).json({ books }) +const getAllBooks = (req, res) => { + res.status(200).json({ books: books }) } const createNewBook = (req, res) => { - const newBook = createNewBk(req.body) - res.status(201).json({ newBook }) + const newBook = req.body + newBook.id = newBookId + newBookId += 1 + books.push(newBook) + res.status(201).json({ book: newBook }) } const getBookById = (req, res) => { - const foundBook = getUsrById(Number(req.params.id)) - res.status(200).json({ foundBook }) + const bookId = Number(req.params.id) + const foundBook = books.find((b) => b.id === bookId) + res.status(200).json({ book: foundBook }) } -const deleteBook = (req, res) => { - const book = deleteBk(Number(req.params.id)) - res.status(200).json({ book }) +const deleteBookById = (req, res) => { + const bookId = Number(req.params.id) + const bookToDelete = books.find((b) => b.id === bookId) + const indexToDelete = books.indexOf(bookToDelete) + books.splice(indexToDelete, 1) + res.status(200).json({ book: bookToDelete }) } const updateBookById = (req, res) => { const bookId = Number(req.params.id) - const data = req.body - - const updateBook = updateBkById - res.status(200).json({ updateUser }) + const updateBook = req.body + updateBook.id = bookId + books.splice(bookId - 1, 1, updateBook) + res.status(200).json({ book: updateBook }) } module.exports = { getAllBooks, createNewBook, getBookById, - deleteBook, + deleteBookById, updateBookById, } diff --git a/src/controllers/filmsController.js b/src/controllers/filmsController.js index e69de29..022e04c 100644 --- a/src/controllers/filmsController.js +++ b/src/controllers/filmsController.js @@ -0,0 +1,46 @@ +const filmsData = require("../../data/index.js") +const films = filmsData.films + +let newFilmId = films.length + 1 + +const getAllFilms = (req, res) => { + res.status(200).json({ films: films }) +} + +const createNewFilm = (req, res) => { + const newFilm = req.body + newFilm.id = newFilmId + newFilmId += 1 + films.push(newFilm) + res.status(201).json({ film: newFilm }) +} + +const getFilmById = (req, res) => { + const filmId = Number(req.params.id) + const foundFilm = films.find((b) => b.id === filmId) + res.status(200).json({ film: foundFilm }) +} + +const deleteFilmById = (req, res) => { + const filmId = Number(req.params.id) + const filmToDelete = films.find((b) => b.id === filmId) + const indexToDelete = films.indexOf(filmToDelete) + films.splice(indexToDelete, 1) + res.status(200).json({ film: filmToDelete }) +} + +const updateFilmById = (req, res) => { + const filmId = Number(req.params.id) + const updateFilm = req.body + updateFilm.id = filmId + films.splice(filmId - 1, 1, updateFilm) + res.status(200).json({ film: updateFilm }) +} + +module.exports = { + getAllFilms, + createNewFilm, + getFilmById, + deleteFilmById, + updateFilmById, +} diff --git a/src/controllers/usersController.js b/src/controllers/usersController.js index 739dd57..15b57e9 100644 --- a/src/controllers/usersController.js +++ b/src/controllers/usersController.js @@ -4,7 +4,7 @@ const users = usersData.users let newUserId = users.length + 1 const getAllUsers = (req, res) => { - res.status(200).json({ users }) + res.status(200).json({ users: users }) } const createNewUser = (req, res) => { diff --git a/src/routers/books.js b/src/routers/books.js index 14b7c25..0f413f5 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,7 +1,13 @@ // Import data here... const express = require("express") const booksRouter = express.Router() -const {getAllBooks,createNewBook,getBookById,deleteBookById,updateBookById} = require('../controllers/booksController.js') +const { + getAllBooks, + createNewBook, + getBookById, + deleteBookById, + updateBookById, +} = require("../controllers/booksController.js") // Write routes here... booksRouter.get("/", getAllBooks) @@ -15,5 +21,3 @@ booksRouter.delete("/:id", deleteBookById) booksRouter.put("/:id", updateBookById) module.exports = booksRouter - - diff --git a/src/routers/films.js b/src/routers/films.js index 7ac8492..e1dbb64 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -1,5 +1,12 @@ const express = require("express") const filmsRouter = express.Router() +const { + getAllFilms, + createNewFilm, + getFilmById, + deleteFilmById, + updateFilmById, +} = require("../controllers/filmsController.js") filmsRouter.get("/", getAllFilms) @@ -12,3 +19,5 @@ filmsRouter.delete("/:id", deleteFilmById) filmsRouter.put("/:id", updateFilmById) module.exports = filmsRouter + + diff --git a/src/server.js b/src/server.js index 733af5c..fe7d048 100644 --- a/src/server.js +++ b/src/server.js @@ -11,13 +11,14 @@ app.use(morgan("dev")); // REQUIRE ROUTERS const usersRouter = require("./routers/users.js") -// const filmsRouter = require("./routers/films.js") -// const booksRouter = require("./routers/books.js") +const filmsRouter = require("./routers/films.js") +const booksRouter = require("./routers/books.js") // ADD ROUTERS TO APP app.use("/users", usersRouter) -// app.use("/books", booksRouter) +app.use('/films', filmsRouter) +app.use("/books", booksRouter) module.exports = app From 1d333ff0880832789dfea4aa7b734692b1c5d9b3 Mon Sep 17 00:00:00 2001 From: Periklis Date: Wed, 19 Jun 2024 04:16:14 +0300 Subject: [PATCH 5/7] most ext done --- src/controllers/booksController.js | 48 ++++++++++++++++++++++- src/controllers/filmsController.js | 61 +++++++++++++++++++++++++++++- src/controllers/usersController.js | 60 ++++++++++++++++++++++++----- src/domain/booksRep.js | 42 -------------------- src/domain/filmsRep.js | 0 src/errors/errors.js | 11 ++++++ src/routers/films.js | 4 +- src/server.js | 38 ++++++++++++++----- 8 files changed, 201 insertions(+), 63 deletions(-) delete mode 100644 src/domain/booksRep.js delete mode 100644 src/domain/filmsRep.js create mode 100644 src/errors/errors.js diff --git a/src/controllers/booksController.js b/src/controllers/booksController.js index 64eb3c0..74f1253 100644 --- a/src/controllers/booksController.js +++ b/src/controllers/booksController.js @@ -1,6 +1,11 @@ const booksData = require("../../data/index.js") -const books = booksData.books +const { + MissingFieldsError, + ExistingDataError, + DataNotFoundError, +} = require("../errors/errors.js") +const books = booksData.books let newBookId = books.length + 1 const getAllBooks = (req, res) => { @@ -9,6 +14,15 @@ const getAllBooks = (req, res) => { const createNewBook = (req, res) => { const newBook = req.body + + if (!newBook.title || !newBook.author || !newBook.type) { + throw new MissingFieldsError("Missing fields in request body") + } + if (books.find((b) => b.title === newBook.title)) { + throw new ExistingDataError( + "A book with the provided title already exists" + ) + } newBook.id = newBookId newBookId += 1 books.push(newBook) @@ -18,12 +32,22 @@ const createNewBook = (req, res) => { const getBookById = (req, res) => { const bookId = Number(req.params.id) const foundBook = books.find((b) => b.id === bookId) + if (!foundBook) { + throw new DataNotFoundError( + "A book the provided ID does not exist" + ) + } res.status(200).json({ book: foundBook }) } const deleteBookById = (req, res) => { const bookId = Number(req.params.id) const bookToDelete = books.find((b) => b.id === bookId) + if (!bookToDelete) { + throw new DataNotFoundError( + "A book the provided ID does not exist" + ) + } const indexToDelete = books.indexOf(bookToDelete) books.splice(indexToDelete, 1) res.status(200).json({ book: bookToDelete }) @@ -32,6 +56,28 @@ const deleteBookById = (req, res) => { const updateBookById = (req, res) => { const bookId = Number(req.params.id) const updateBook = req.body + const foundBook = books.find((b) => b.id === bookId) + + if (!foundBook) { + throw new DataNotFoundError( + "A book the provided ID does not exist" + ) + } + + if ( + !updateBook.title || + !updateBook.type || + !updateBook.author + ) { + throw new MissingFieldsError('Missing fields in request body') + } + + const existingTitle = books.find((b) => b.title === updateBook.title) + if (existingTitle) { + throw new ExistingDataError( + "A book with the provided title already exists" + ) + } updateBook.id = bookId books.splice(bookId - 1, 1, updateBook) res.status(200).json({ book: updateBook }) diff --git a/src/controllers/filmsController.js b/src/controllers/filmsController.js index 022e04c..45d466b 100644 --- a/src/controllers/filmsController.js +++ b/src/controllers/filmsController.js @@ -1,4 +1,10 @@ const filmsData = require("../../data/index.js") +const { + MissingFieldsError, + ExistingDataError, + DataNotFoundError, +} = require("../errors/errors.js") + const films = filmsData.films let newFilmId = films.length + 1 @@ -9,6 +15,18 @@ const getAllFilms = (req, res) => { const createNewFilm = (req, res) => { const newFilm = req.body + if (!newFilm.title || !newFilm.director) { + throw new MissingFieldsError("Missing fields in request body") + } + const existingTitle = films.find( + (f) => f.title === newFilm.title + ) + if (existingTitle) { + throw new ExistingDataError( + "A film with the provided title already exists" + ) + } + newFilm.id = newFilmId newFilmId += 1 films.push(newFilm) @@ -17,13 +35,29 @@ const createNewFilm = (req, res) => { const getFilmById = (req, res) => { const filmId = Number(req.params.id) - const foundFilm = films.find((b) => b.id === filmId) + const foundFilm = films.find((f) => f.id === filmId) + if (!foundFilm) { + throw new DataNotFoundError( + "A film with provided ID does not exist" + ) + } res.status(200).json({ film: foundFilm }) } +const getFilmByDirector = (req, res) => { + const director = req.params.director + const filmsToShow = films.filter(f=>f.director === director) + return res.status(200).json({ films: filmsToShow }) +} + const deleteFilmById = (req, res) => { const filmId = Number(req.params.id) const filmToDelete = films.find((b) => b.id === filmId) + if (!filmToDelete) { + throw new DataNotFoundError( + "A film with provided ID does not exist" + ) + } const indexToDelete = films.indexOf(filmToDelete) films.splice(indexToDelete, 1) res.status(200).json({ film: filmToDelete }) @@ -32,13 +66,38 @@ const deleteFilmById = (req, res) => { const updateFilmById = (req, res) => { const filmId = Number(req.params.id) const updateFilm = req.body + + const foundFilm = films.find((f) => f.id === filmId) + + if (!foundFilm) { + throw new DataNotFoundError( + "A film with provided ID does not exist" + ) + } + + if (!updateFilm.title || !updateFilm.director) { + throw new MissingFieldsError("Missing fields in request body") + } + + const existingTitle = films.find( + (f) => f.title === updateFilm.title + ) + if (existingTitle) { + throw new ExistingDataError( + "A film with the provided title already exists" + ) + } + updateFilm.id = filmId films.splice(filmId - 1, 1, updateFilm) res.status(200).json({ film: updateFilm }) } + + module.exports = { getAllFilms, + getFilmByDirector, createNewFilm, getFilmById, deleteFilmById, diff --git a/src/controllers/usersController.js b/src/controllers/usersController.js index 15b57e9..fd8fb05 100644 --- a/src/controllers/usersController.js +++ b/src/controllers/usersController.js @@ -1,4 +1,10 @@ const usersData = require("../../data/index.js") +const { + MissingFieldsError, + ExistingDataError, + DataNotFoundError, +} = require("../errors/errors.js") + const users = usersData.users let newUserId = users.length + 1 @@ -9,6 +15,16 @@ const getAllUsers = (req, res) => { const createNewUser = (req, res) => { const newUser = req.body + + if (!newUser.email) { + throw new MissingFieldsError("Missing fields in request body") + } + if (users.find((u) => u.email === newUser.email)) { + throw new ExistingDataError( + "A user with the provided email already exists" + ) + } + newUser.id = newUserId newUserId += 1 users.push(newUser) @@ -16,24 +32,50 @@ const createNewUser = (req, res) => { } const getUserById = (req, res) => { - const userId = Number(req.params.id) - const foundUser = users.find(u=>u.id === userId) - res.status(200).json({user: foundUser }) + const userId = Number(req.params.id) + const foundUser = users.find((u) => u.id === userId) + if (!foundUser) { + throw new DataNotFoundError( + "A user with the provided ID does not exist" + ) + } + res.status(200).json({ user: foundUser }) } const deleteUserById = (req, res) => { - const userId = Number(req.params.id) - const userToDelete = users.find((u) => u.id === userId) - const indexToDelete = users.indexOf(userToDelete) - users.splice(indexToDelete,1) + const userId = Number(req.params.id) + const userToDelete = users.find((u) => u.id === userId) + if (!userToDelete) { + throw new DataNotFoundError( + "A user with the provided ID does not exist" + ) + } + const indexToDelete = users.indexOf(userToDelete) + users.splice(indexToDelete, 1) res.status(200).json({ user: userToDelete }) } const updateUserById = (req, res) => { const userId = Number(req.params.id) const updateUser = req.body - updateUser.id = userId - users.splice(userId - 1, 1, updateUser) + + const foundUser = users.find((u) => u.id === userId) + if (!foundUser) { + throw new DataNotFoundError( + 'A user with the provided ID does not exist' + ) + } + if (!updateUser.email) { + throw new MissingFieldsError("Missing fields in request body") + } + if (users.find((u) => u.email === updateUser.email)) { + throw new ExistingDataError( + "A user with the provided email already exists" + ) + } + + updateUser.id = userId + users.splice(userId - 1, 1, updateUser) res.status(200).json({ user: updateUser }) } diff --git a/src/domain/booksRep.js b/src/domain/booksRep.js deleted file mode 100644 index 8f0e863..0000000 --- a/src/domain/booksRep.js +++ /dev/null @@ -1,42 +0,0 @@ -const { books } = require("../../data/index.js") -// const usersDb = require(users) - -let newBkId = books.length + 1 - -const getAllBks = () => { - return books -} - -const createNewBk = (data) => { - const newBk = data - newBk.id = newBkId - newUsrId += 1 - books.push(newBk) -} - -const getBkById = (id) => { - return books.find((b) => b.id === id) -} - -const deleteBk = (id) => { - const bookToDelete = books.find((b) => b.id === id) - books = books.filter((b) => b.id !== id) - - return bookToDelete -} - -const updateBkById = (id, data) => { - const bookId = id - const updatedBook = data - updatedBook.id = bookId - books.splice(bookId - 1, 1, updatedBook) - return updatedUser -} - -module.exports = { - getAllBks, - createNewBk, - getBkById, - deleteBk, - updateBkById, -} diff --git a/src/domain/filmsRep.js b/src/domain/filmsRep.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/errors/errors.js b/src/errors/errors.js new file mode 100644 index 0000000..534fbc2 --- /dev/null +++ b/src/errors/errors.js @@ -0,0 +1,11 @@ +class MissingFieldsError extends Error {} + +class ExistingDataError extends Error {} + +class DataNotFoundError extends Error {} + +module.exports = { + MissingFieldsError, + ExistingDataError, + DataNotFoundError +} diff --git a/src/routers/films.js b/src/routers/films.js index e1dbb64..ab50c36 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -3,7 +3,7 @@ const filmsRouter = express.Router() const { getAllFilms, createNewFilm, - getFilmById, + getFilmById,getFilmByDirector, deleteFilmById, updateFilmById, } = require("../controllers/filmsController.js") @@ -14,6 +14,8 @@ filmsRouter.post("/", createNewFilm) filmsRouter.get("/:id", getFilmById) +filmsRouter.get("/:dir", getFilmByDirector) + filmsRouter.delete("/:id", deleteFilmById) filmsRouter.put("/:id", updateFilmById) diff --git a/src/server.js b/src/server.js index fe7d048..4e936a7 100644 --- a/src/server.js +++ b/src/server.js @@ -1,24 +1,44 @@ -const express = require("express"); -const app = express(); - -const cors = require("cors"); -const morgan = require("morgan"); +const express = require("express") +const app = express() +// const{ MissingFieldsError} = require('./errors/errors.js') +const cors = require("cors") +const morgan = require("morgan") // SETUP MIDDLEWARE -app.use(cors()); -app.use(express.json()); -app.use(morgan("dev")); +app.use(cors()) +app.use(express.json()) +app.use(morgan("dev")) // REQUIRE ROUTERS const usersRouter = require("./routers/users.js") const filmsRouter = require("./routers/films.js") const booksRouter = require("./routers/books.js") +const { + MissingFieldsError, + ExistingDataError, + DataNotFoundError, +} = require("./errors/errors.js") // ADD ROUTERS TO APP app.use("/users", usersRouter) -app.use('/films', filmsRouter) +app.use("/films", filmsRouter) app.use("/books", booksRouter) +app.use((error, req, res, next) => { + if (error instanceof MissingFieldsError) { + return res.status(400).json({ error: error.message }) + } + if (error instanceof DataNotFoundError) { + return res.status(404).json({ error: error.message }) + } + if (error instanceof ExistingDataError) { + return res.status(409).json({ error: error.message }) + } + + res.status(500).json({ + message: "Something went wrong", + }) +}) module.exports = app From 3ccc8fc8c566c753fcbe3afec7c678bfe18c6e4f Mon Sep 17 00:00:00 2001 From: Periklis Date: Wed, 19 Jun 2024 04:28:36 +0300 Subject: [PATCH 6/7] proper filmRouter get accounts for query --- src/controllers/filmsController.js | 14 +++++--------- src/routers/films.js | 16 ++++++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/controllers/filmsController.js b/src/controllers/filmsController.js index 45d466b..be97784 100644 --- a/src/controllers/filmsController.js +++ b/src/controllers/filmsController.js @@ -18,9 +18,7 @@ const createNewFilm = (req, res) => { if (!newFilm.title || !newFilm.director) { throw new MissingFieldsError("Missing fields in request body") } - const existingTitle = films.find( - (f) => f.title === newFilm.title - ) + const existingTitle = films.find((f) => f.title === newFilm.title) if (existingTitle) { throw new ExistingDataError( "A film with the provided title already exists" @@ -44,10 +42,10 @@ const getFilmById = (req, res) => { res.status(200).json({ film: foundFilm }) } -const getFilmByDirector = (req, res) => { - const director = req.params.director - const filmsToShow = films.filter(f=>f.director === director) - return res.status(200).json({ films: filmsToShow }) +function getFilmByDirector(req, res) { + const director = req.query.director + const foundFilms = films.filter((f) => f.director === director) + res.status(200).json({ films: foundFilms }) } const deleteFilmById = (req, res) => { @@ -93,8 +91,6 @@ const updateFilmById = (req, res) => { res.status(200).json({ film: updateFilm }) } - - module.exports = { getAllFilms, getFilmByDirector, diff --git a/src/routers/films.js b/src/routers/films.js index ab50c36..2b53395 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -3,23 +3,27 @@ const filmsRouter = express.Router() const { getAllFilms, createNewFilm, - getFilmById,getFilmByDirector, + getFilmById, + getFilmByDirector, deleteFilmById, updateFilmById, } = require("../controllers/filmsController.js") -filmsRouter.get("/", getAllFilms) +// filmsRouter.get("/", getAllFilms) +filmsRouter.get("/", (req, res) => { + if (req.query.director) { + return getFilmByDirector(req, res) + } else { + return getAllFilms(req, res) + } +}) filmsRouter.post("/", createNewFilm) filmsRouter.get("/:id", getFilmById) -filmsRouter.get("/:dir", getFilmByDirector) - filmsRouter.delete("/:id", deleteFilmById) filmsRouter.put("/:id", updateFilmById) module.exports = filmsRouter - - From 34d1c297b2ce3d1cc6347e766f375b06205b92b1 Mon Sep 17 00:00:00 2001 From: Periklis Date: Wed, 19 Jun 2024 11:26:20 +0300 Subject: [PATCH 7/7] reorder error checks in books controller -put --- src/controllers/booksController.js | 14 ++++++++------ src/routers/films.js | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/controllers/booksController.js b/src/controllers/booksController.js index 74f1253..b13c478 100644 --- a/src/controllers/booksController.js +++ b/src/controllers/booksController.js @@ -58,12 +58,6 @@ const updateBookById = (req, res) => { const updateBook = req.body const foundBook = books.find((b) => b.id === bookId) - if (!foundBook) { - throw new DataNotFoundError( - "A book the provided ID does not exist" - ) - } - if ( !updateBook.title || !updateBook.type || @@ -71,6 +65,13 @@ const updateBookById = (req, res) => { ) { throw new MissingFieldsError('Missing fields in request body') } + + if (!foundBook) { + throw new DataNotFoundError( + "A book the provided ID does not exist" + ) + } + const existingTitle = books.find((b) => b.title === updateBook.title) if (existingTitle) { @@ -78,6 +79,7 @@ const updateBookById = (req, res) => { "A book with the provided title already exists" ) } + updateBook.id = bookId books.splice(bookId - 1, 1, updateBook) res.status(200).json({ book: updateBook }) diff --git a/src/routers/films.js b/src/routers/films.js index 2b53395..2952b27 100644 --- a/src/routers/films.js +++ b/src/routers/films.js @@ -9,7 +9,7 @@ const { updateFilmById, } = require("../controllers/filmsController.js") -// filmsRouter.get("/", getAllFilms) + filmsRouter.get("/", (req, res) => { if (req.query.director) { return getFilmByDirector(req, res)