From e36612f124c22cd18a084aea1e5e3ac5e8b8a046 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 13:36:48 +0200 Subject: [PATCH 01/14] set up project and get all books --- dal/booksRepo.js | 34 ++++++++++++++++++++++++++++++++++ db/index.js | 25 ------------------------- src/routers/books.js | 9 ++++++++- src/server.js | 2 +- utils/dbConnection.js | 16 ++++++++++++++++ 5 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 dal/booksRepo.js delete mode 100644 db/index.js create mode 100644 utils/dbConnection.js diff --git a/dal/booksRepo.js b/dal/booksRepo.js new file mode 100644 index 00000000..feecef4d --- /dev/null +++ b/dal/booksRepo.js @@ -0,0 +1,34 @@ +const dbConnection = require('../utils/dbConnection.js') + +const getAllBooks = async (type, topic) => { + const db = await dbConnection.connect() + + try { + if (type) { + const sqlQuery = `select * from books where type = $1` + const result = await db.query(sqlQuery, [type]) + + return result.rows + } + + if (topic) { + const sqlQuery = `select * from books where topic = $1` + const result = await db.query(sqlQuery, [topic]) + + return result.rows + } + + const sqlQuery = 'select * from books' + const result = await db.query(sqlQuery) + + return result.rows + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + +module.exports = { + getAllBooks, +} \ No newline at end of file diff --git a/db/index.js b/db/index.js deleted file mode 100644 index af723442..00000000 --- a/db/index.js +++ /dev/null @@ -1,25 +0,0 @@ -// Load our .env file -require('dotenv').config() - -// Require Client obj from the postgres node module -const { Client } = require("pg"); - -const client = { - query: async (str, values) => { - // Get the connection string from process.env - - // the dotenv library sets this variable based - // on the contents of our env file - // Create a new connection to the database using the Client - // object provided by the postgres node module - const dbClient = new Client(process.env.PGURL) - // connect a connection - await dbClient.connect() - // execute the query - const result = await dbClient.query(str, values) - // close the connection - await dbClient.end() - return result - } -} - -module.exports = client; diff --git a/src/routers/books.js b/src/routers/books.js index 1551dd87..c3e1fcc2 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,9 +1,16 @@ const express = require('express') +const { getAllBooks } = require('../../dal/booksRepo.js') const router = express.Router() -const db = require("../../db"); router.get('/', async (req, res) => { + const type = req.query.type + const topic = req.query.topic + const books = await getAllBooks(type, topic) + + res.json({ + books + }) }) module.exports = router diff --git a/src/server.js b/src/server.js index dac55e5d..4528c656 100644 --- a/src/server.js +++ b/src/server.js @@ -1,3 +1,4 @@ +require('dotenv').config() const express = require("express"); const morgan = require("morgan"); const cors = require("cors"); @@ -8,7 +9,6 @@ app.use(morgan("dev")); app.use(cors()); app.use(express.json()); -//TODO: Implement books and pets APIs using Express Modular Routers const booksRouter = require('./routers/books.js') app.use('/books', booksRouter) diff --git a/utils/dbConnection.js b/utils/dbConnection.js new file mode 100644 index 00000000..0f9f11ed --- /dev/null +++ b/utils/dbConnection.js @@ -0,0 +1,16 @@ +const { Pool } = require('pg'); + +const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD } = process.env; + +const dbConnection = new Pool({ + host: PGHOST, + database: PGDATABASE, + username: PGUSER, + password: PGPASSWORD, + port: 5432, + ssl: { + require: true, + }, + }) + +module.exports = dbConnection \ No newline at end of file From 95eb627759f14d94a10bdade70300ce70ad6375b Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 13:57:38 +0200 Subject: [PATCH 02/14] add functions to create new book --- dal/booksRepo.js | 17 +++++++++++++++++ src/routers/books.js | 12 +++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/dal/booksRepo.js b/dal/booksRepo.js index feecef4d..a4d87d74 100644 --- a/dal/booksRepo.js +++ b/dal/booksRepo.js @@ -29,6 +29,23 @@ const getAllBooks = async (type, topic) => { } } +const createBook = async (book) => { + const db = await dbConnection.connect() + console.log(book) + + try { + const sqlQuery = `insert into books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` + const result = await db.query(sqlQuery, [book.title, book.type, book.author, book.topic, book.publication_date, book.pages]) + + return result.rows + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + module.exports = { getAllBooks, + createBook } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index c3e1fcc2..2bd1c180 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,5 +1,5 @@ const express = require('express') -const { getAllBooks } = require('../../dal/booksRepo.js') +const { getAllBooks, createBook } = require('../../dal/booksRepo.js') const router = express.Router() router.get('/', async (req, res) => { @@ -13,4 +13,14 @@ router.get('/', async (req, res) => { }) }) +router.post('/', async (req, res) => { + const book = req.body + + const newBook = await createBook(book) + + res.json({ + book: newBook + }) +}) + module.exports = router From 470d594189a4cc864843cd147a33f3253bed7028 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 14:05:55 +0200 Subject: [PATCH 03/14] add functions to get book by id --- dal/booksRepo.js | 20 ++++++++++++++++++-- src/routers/books.js | 12 +++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dal/booksRepo.js b/dal/booksRepo.js index a4d87d74..639fdfa3 100644 --- a/dal/booksRepo.js +++ b/dal/booksRepo.js @@ -37,7 +37,22 @@ const createBook = async (book) => { const sqlQuery = `insert into books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` const result = await db.query(sqlQuery, [book.title, book.type, book.author, book.topic, book.publication_date, book.pages]) - return result.rows + return result.rows[0] + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + +const getBookById = async (id) => { + const db = await dbConnection.connect() + + try { + const sqlQuery = `select * from books where id = $1` + const result = await db.query(sqlQuery, [id]) + + return result.rows[0] } catch (e) { console.log(e) } finally { @@ -47,5 +62,6 @@ const createBook = async (book) => { module.exports = { getAllBooks, - createBook + createBook, + getBookById } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 2bd1c180..48ba333b 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,5 +1,5 @@ const express = require('express') -const { getAllBooks, createBook } = require('../../dal/booksRepo.js') +const { getAllBooks, createBook, getBookById } = require('../../dal/booksRepo.js') const router = express.Router() router.get('/', async (req, res) => { @@ -23,4 +23,14 @@ router.post('/', async (req, res) => { }) }) +router.get('/:id', async (req, res) => { + const bookId = Number(req.params.id) + + const foundBook = await getBookById(bookId) + + res.json({ + book: foundBook + }) +}) + module.exports = router From adebe88d37ae21a5511d807d29124987d2947517 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 14:15:47 +0200 Subject: [PATCH 04/14] add functions to update a book --- dal/booksRepo.js | 19 +++++++++++++++++-- src/routers/books.js | 13 ++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dal/booksRepo.js b/dal/booksRepo.js index 639fdfa3..71064bb2 100644 --- a/dal/booksRepo.js +++ b/dal/booksRepo.js @@ -31,7 +31,6 @@ const getAllBooks = async (type, topic) => { const createBook = async (book) => { const db = await dbConnection.connect() - console.log(book) try { const sqlQuery = `insert into books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` @@ -60,8 +59,24 @@ const getBookById = async (id) => { } } +const updateBook = async (id, bookInfo) => { + const db = await dbConnection.connect() + + try { + const sqlQuery = `update books set title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 where id = $7 returning *` + const result = await db.query(sqlQuery, [bookInfo.title, bookInfo.type, bookInfo.author, bookInfo.topic, bookInfo.publication_date, bookInfo.pages, id]) + + return result.rows[0] + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + module.exports = { getAllBooks, createBook, - getBookById + getBookById, + updateBook } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 48ba333b..047d5189 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,5 +1,5 @@ const express = require('express') -const { getAllBooks, createBook, getBookById } = require('../../dal/booksRepo.js') +const { getAllBooks, createBook, getBookById, updateBook } = require('../../dal/booksRepo.js') const router = express.Router() router.get('/', async (req, res) => { @@ -33,4 +33,15 @@ router.get('/:id', async (req, res) => { }) }) +router.put('/:id', async (req, res) => { + const bookId = Number(req.params.id) + const bookInfo = req.body + + const updatedBook = await updateBook(bookId, bookInfo) + + res.json({ + book: updatedBook + }) +}) + module.exports = router From 61360c275a1717fce601be0df204fc1fe3cfe6a9 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 14:20:10 +0200 Subject: [PATCH 05/14] add functions to delete book by id --- dal/booksRepo.js | 18 +++++++++++++++++- src/routers/books.js | 12 +++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/dal/booksRepo.js b/dal/booksRepo.js index 71064bb2..4cd0ebe1 100644 --- a/dal/booksRepo.js +++ b/dal/booksRepo.js @@ -74,9 +74,25 @@ const updateBook = async (id, bookInfo) => { } } +const deleteBookById = async (id) => { + const db = await dbConnection.connect() + + try { + const sqlQuery = `delete from books where id = $1 returning *` + const result = await db.query(sqlQuery, [id]) + + return result.rows[0] + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + module.exports = { getAllBooks, createBook, getBookById, - updateBook + updateBook, + deleteBookById } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 047d5189..b90308a1 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,5 +1,5 @@ const express = require('express') -const { getAllBooks, createBook, getBookById, updateBook } = require('../../dal/booksRepo.js') +const { getAllBooks, createBook, getBookById, updateBook, deleteBookById } = require('../../dal/booksRepo.js') const router = express.Router() router.get('/', async (req, res) => { @@ -44,4 +44,14 @@ router.put('/:id', async (req, res) => { }) }) +router.delete('/:id', async (req, res) => { + const bookId = Number(req.params.id) + + const deletedBook = await deleteBookById(bookId) + + res.json({ + book: deletedBook + }) +}) + module.exports = router From 67ae0e1c67fcaead585d67de978445a8c663804e Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 14:36:18 +0200 Subject: [PATCH 06/14] add get, post, put and delete functions for pets --- dal/petsRepo.js | 91 +++++++++++++++++++++++++++++++++++++++++++++ src/routers/pets.js | 56 ++++++++++++++++++++++++++++ src/server.js | 2 + 3 files changed, 149 insertions(+) create mode 100644 dal/petsRepo.js create mode 100644 src/routers/pets.js diff --git a/dal/petsRepo.js b/dal/petsRepo.js new file mode 100644 index 00000000..d54f5c39 --- /dev/null +++ b/dal/petsRepo.js @@ -0,0 +1,91 @@ +const dbConnection = require('../utils/dbConnection.js') + +const getAllPets = async (type) => { + const db = await dbConnection.connect() + + try { + if (type) { + const sqlQuery = `select * from pets where type = $1` + const result = await db.query(sqlQuery, [type]) + + return result.rows + } + + const sqlQuery = 'select * from pets' + const result = await db.query(sqlQuery) + + return result.rows + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + +const createPet = async (pet) => { + const db = await dbConnection.connect() + + try { + const sqlQuery = `insert into pets (name, age, type, breed, has_microchip) values ($1, $2, $3, $4, $5) returning *` + const result = await db.query(sqlQuery, [pet.name, pet.age, pet.type, pet.breed, pet.has_microchip]) + + return result.rows[0] + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + +const getPetById = async (id) => { + const db = await dbConnection.connect() + + try { + const sqlQuery = `select * from pets where id = $1` + const result = await db.query(sqlQuery, [id]) + + return result.rows[0] + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + +const updatePet = async (id, petInfo) => { + const db = await dbConnection.connect() + + try { + const sqlQuery = `update pets set name = $1, age = $2, type = $3, breed = $4, has_microchip = $5 where id = $6 returning *` + const result = await db.query(sqlQuery, [petInfo.name, petInfo.age, petInfo.type, petInfo.breed, petInfo.has_microchip, id]) + + return result.rows[0] + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + +const deletePetById = async (id) => { + const db = await dbConnection.connect() + + try { + const sqlQuery = `delete from pets where id = $1 returning *` + const result = await db.query(sqlQuery, [id]) + + return result.rows[0] + } catch (e) { + console.log(e) + } finally { + db.release() + } +} + +module.exports = { + getAllPets, + createPet, + getPetById, + updatePet, + deletePetById +} \ No newline at end of file diff --git a/src/routers/pets.js b/src/routers/pets.js new file mode 100644 index 00000000..17ba7d59 --- /dev/null +++ b/src/routers/pets.js @@ -0,0 +1,56 @@ +const express = require('express') +const { getAllPets, createPet, getPetById, updatePet, deletePetById } = require('../../dal/petsRepo.js') +const router = express.Router() + +router.get('/', async (req, res) => { + const type = req.query.type + + const pets = await getAllPets(type) + + res.json({ + pets + }) +}) + +router.post('/', async (req, res) => { + const pet = req.body + + const newPet = await createPet(pet) + + res.json({ + pet: newPet + }) +}) + +router.get('/:id', async (req, res) => { + const petId = Number(req.params.id) + + const foundPet = await getPetById(petId) + + res.json({ + pet: foundPet + }) +}) + +router.put('/:id', async (req, res) => { + const petId = Number(req.params.id) + const petInfo = req.body + + const updatedPet = await updatePet(petId, petInfo) + + res.json({ + pet: updatedPet + }) +}) + +router.delete('/:id', async (req, res) => { + const petId = Number(req.params.id) + + const deletedPet = await deletePetById(petId) + + res.json({ + pet: deletedPet + }) +}) + +module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index 4528c656..3a5ecf18 100644 --- a/src/server.js +++ b/src/server.js @@ -10,7 +10,9 @@ app.use(cors()); app.use(express.json()); const booksRouter = require('./routers/books.js') +const petsRouter = require('./routers/pets.js') app.use('/books', booksRouter) +app.use('/pets', petsRouter) module.exports = app From 8334651aa8dd70a5f1f86442882c1e2f3b315d56 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 15:21:59 +0200 Subject: [PATCH 07/14] add search by author --- dal/booksRepo.js | 9 ++++++++- src/routers/books.js | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/dal/booksRepo.js b/dal/booksRepo.js index 4cd0ebe1..48b2ce93 100644 --- a/dal/booksRepo.js +++ b/dal/booksRepo.js @@ -1,6 +1,6 @@ const dbConnection = require('../utils/dbConnection.js') -const getAllBooks = async (type, topic) => { +const getAllBooks = async (type, topic, author) => { const db = await dbConnection.connect() try { @@ -18,6 +18,13 @@ const getAllBooks = async (type, topic) => { return result.rows } + if (author) { + const sqlQuery = `select * from books where author = $1` + const result = await db.query(sqlQuery, [author]) + + return result.rows + } + const sqlQuery = 'select * from books' const result = await db.query(sqlQuery) diff --git a/src/routers/books.js b/src/routers/books.js index b90308a1..ac8759ea 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -5,8 +5,9 @@ const router = express.Router() router.get('/', async (req, res) => { const type = req.query.type const topic = req.query.topic + const author = req.query.author - const books = await getAllBooks(type, topic) + const books = await getAllBooks(type, topic, author) res.json({ books From 6d07de72cd91a44a5b05a817db7128301c76106a Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 15:39:03 +0200 Subject: [PATCH 08/14] add pagination to books --- dal/booksRepo.js | 24 +++++++++++++++++++----- src/routers/books.js | 4 +++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/dal/booksRepo.js b/dal/booksRepo.js index 48b2ce93..73538eda 100644 --- a/dal/booksRepo.js +++ b/dal/booksRepo.js @@ -1,11 +1,25 @@ const dbConnection = require('../utils/dbConnection.js') -const getAllBooks = async (type, topic, author) => { +const getAllBooks = async (type, topic, author, pages, perpages) => { const db = await dbConnection.connect() - + try { + let page = 1 + let perpage = 20 + let offset = 0 + + if (pages) { + page = pages + offset = (page - 1) * perpage + } + + if (perpages > 9 && perpages < 51) { + perpage = perpages + offset = (page - 1) * perpage + } + if (type) { - const sqlQuery = `select * from books where type = $1` + const sqlQuery = `select * from books where type = $1 limit = $2 offset = $3` const result = await db.query(sqlQuery, [type]) return result.rows @@ -25,8 +39,8 @@ const getAllBooks = async (type, topic, author) => { return result.rows } - const sqlQuery = 'select * from books' - const result = await db.query(sqlQuery) + const sqlQuery = `select * from books limit $1 offset $2` + const result = await db.query(sqlQuery, [perpage, offset]) return result.rows } catch (e) { diff --git a/src/routers/books.js b/src/routers/books.js index ac8759ea..813b8133 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -6,8 +6,10 @@ router.get('/', async (req, res) => { const type = req.query.type const topic = req.query.topic const author = req.query.author + const page = req.query.page + const perPage = req.query.per_page - const books = await getAllBooks(type, topic, author) + const books = await getAllBooks(type, topic, author, page, perPage) res.json({ books From ec951dbd9882223e056e776ebb74ec930d4e301b Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Fri, 21 Jun 2024 16:53:36 +0200 Subject: [PATCH 09/14] add missing fields error handling --- package-lock.json | 55 +++++----------------------- package.json | 2 +- {dal => src/dal}/booksRepo.js | 18 +++++++++ {dal => src/dal}/petsRepo.js | 0 src/errors/missingFieldsError.js | 5 +++ src/routers/books.js | 9 ++++- src/routers/pets.js | 2 +- src/server.js | 14 +++++++ {utils => src/utils}/dbConnection.js | 0 9 files changed, 55 insertions(+), 50 deletions(-) rename {dal => src/dal}/booksRepo.js (86%) rename {dal => src/dal}/petsRepo.js (100%) create mode 100644 src/errors/missingFieldsError.js rename {utils => src/utils}/dbConnection.js (100%) diff --git a/package-lock.json b/package-lock.json index 37ccdff8..984867e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "body-parser": "^1.20.2", "cors": "^2.8.5", "dotenv": "^16.3.1", - "express": "^4.18.2", + "express": "^4.19.2", "faker": "^5.5.3", "morgan": "1.10.0", "pg": "8.6.0", @@ -1740,9 +1740,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" } @@ -2024,16 +2024,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", @@ -2064,43 +2064,6 @@ "node": ">= 0.10.0" } }, - "node_modules/express/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==", - "dependencies": { - "bytes": "3.1.2", - "content-type": "~1.0.4", - "debug": "2.6.9", - "depd": "2.0.0", - "destroy": "1.2.0", - "http-errors": "2.0.0", - "iconv-lite": "0.4.24", - "on-finished": "2.4.1", - "qs": "6.11.0", - "raw-body": "2.5.1", - "type-is": "~1.6.18", - "unpipe": "1.0.0" - }, - "engines": { - "node": ">= 0.8", - "npm": "1.2.8000 || >= 1.4.16" - } - }, - "node_modules/express/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==", - "dependencies": { - "bytes": "3.1.2", - "http-errors": "2.0.0", - "iconv-lite": "0.4.24", - "unpipe": "1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/faker": { "version": "5.5.3", "resolved": "https://registry.npmjs.org/faker/-/faker-5.5.3.tgz", diff --git a/package.json b/package.json index 57d8b4fb..a34a190c 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "body-parser": "^1.20.2", "cors": "^2.8.5", "dotenv": "^16.3.1", - "express": "^4.18.2", + "express": "^4.19.2", "faker": "^5.5.3", "morgan": "1.10.0", "pg": "8.6.0", diff --git a/dal/booksRepo.js b/src/dal/booksRepo.js similarity index 86% rename from dal/booksRepo.js rename to src/dal/booksRepo.js index 73538eda..3b10f98a 100644 --- a/dal/booksRepo.js +++ b/src/dal/booksRepo.js @@ -1,4 +1,6 @@ const dbConnection = require('../utils/dbConnection.js') +const MissingFieldsError = require('../errors/missingFieldsError.js') +const { as } = require('pg-promise') const getAllBooks = async (type, topic, author, pages, perpages) => { const db = await dbConnection.connect() @@ -53,6 +55,10 @@ const getAllBooks = async (type, topic, author, pages, perpages) => { const createBook = async (book) => { const db = await dbConnection.connect() + if (!verifyFields(book)) { + throw new MissingFieldsError('Missing fields in request body') + } + try { const sqlQuery = `insert into books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` const result = await db.query(sqlQuery, [book.title, book.type, book.author, book.topic, book.publication_date, book.pages]) @@ -110,6 +116,18 @@ const deleteBookById = async (id) => { } } +function verifyFields(object) { + const neededProperties = ['title', 'type', 'author', 'topic', 'publication_date', 'pages'] + + for (const item of neededProperties) { + if (object[item] === undefined) { + return false + } + } + + return true +} + module.exports = { getAllBooks, createBook, diff --git a/dal/petsRepo.js b/src/dal/petsRepo.js similarity index 100% rename from dal/petsRepo.js rename to src/dal/petsRepo.js diff --git a/src/errors/missingFieldsError.js b/src/errors/missingFieldsError.js new file mode 100644 index 00000000..7359cc1a --- /dev/null +++ b/src/errors/missingFieldsError.js @@ -0,0 +1,5 @@ +class MissingFieldsError extends Error { + +} + +module.exports = MissingFieldsError \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 813b8133..d3474860 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,5 +1,5 @@ const express = require('express') -const { getAllBooks, createBook, getBookById, updateBook, deleteBookById } = require('../../dal/booksRepo.js') +const { getAllBooks, createBook, getBookById, updateBook, deleteBookById } = require('../dal/booksRepo.js') const router = express.Router() router.get('/', async (req, res) => { @@ -16,14 +16,19 @@ router.get('/', async (req, res) => { }) }) -router.post('/', async (req, res) => { +router.post('/', async (req, res, next) => { const book = req.body + try { const newBook = await createBook(book) res.json({ book: newBook }) + } catch(e) { + next(e) + } + }) router.get('/:id', async (req, res) => { diff --git a/src/routers/pets.js b/src/routers/pets.js index 17ba7d59..6501f2f8 100644 --- a/src/routers/pets.js +++ b/src/routers/pets.js @@ -1,5 +1,5 @@ const express = require('express') -const { getAllPets, createPet, getPetById, updatePet, deletePetById } = require('../../dal/petsRepo.js') +const { getAllPets, createPet, getPetById, updatePet, deletePetById } = require('../dal/petsRepo.js') const router = express.Router() router.get('/', async (req, res) => { diff --git a/src/server.js b/src/server.js index 3a5ecf18..3f89e0e7 100644 --- a/src/server.js +++ b/src/server.js @@ -11,8 +11,22 @@ app.use(express.json()); const booksRouter = require('./routers/books.js') const petsRouter = require('./routers/pets.js') +const MissingFieldsError = require('./errors/missingFieldsError.js') app.use('/books', booksRouter) app.use('/pets', petsRouter) +app.use((error, req, res, next) => { + console.log('test') + if (error instanceof MissingFieldsError) { + return res.status(400).json({ + error: error.message + }) + } + + res.status(500).json({ + message: 'Something went wrong' + }) +}) + module.exports = app diff --git a/utils/dbConnection.js b/src/utils/dbConnection.js similarity index 100% rename from utils/dbConnection.js rename to src/utils/dbConnection.js From ddb32d9dbc2a1590c8c937382075886815df8824 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Mon, 24 Jun 2024 12:19:04 +0200 Subject: [PATCH 10/14] clean up code and add id not found error handling --- package-lock.json | 9 +++ package.json | 1 + src/dal/booksRepo.js | 128 ++++++++++++++---------------------- src/errors/notFoundError.js | 5 ++ src/routers/books.js | 9 +-- src/server.js | 9 ++- 6 files changed, 73 insertions(+), 88 deletions(-) create mode 100644 src/errors/notFoundError.js diff --git a/package-lock.json b/package-lock.json index 984867e9..e75c4691 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.19.2", + "express-async-errors": "^3.1.1", "faker": "^5.5.3", "morgan": "1.10.0", "pg": "8.6.0", @@ -2064,6 +2065,14 @@ "node": ">= 0.10.0" } }, + "node_modules/express-async-errors": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/express-async-errors/-/express-async-errors-3.1.1.tgz", + "integrity": "sha512-h6aK1da4tpqWSbyCa3FxB/V6Ehd4EEB15zyQq9qe75OZBp0krinNKuH4rAY+S/U/2I36vdLAUFSjQJ+TFmODng==", + "peerDependencies": { + "express": "^4.16.2" + } + }, "node_modules/faker": { "version": "5.5.3", "resolved": "https://registry.npmjs.org/faker/-/faker-5.5.3.tgz", diff --git a/package.json b/package.json index a34a190c..6e5d33de 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.19.2", + "express-async-errors": "^3.1.1", "faker": "^5.5.3", "morgan": "1.10.0", "pg": "8.6.0", diff --git a/src/dal/booksRepo.js b/src/dal/booksRepo.js index 3b10f98a..4e52d200 100644 --- a/src/dal/booksRepo.js +++ b/src/dal/booksRepo.js @@ -1,119 +1,87 @@ const dbConnection = require('../utils/dbConnection.js') const MissingFieldsError = require('../errors/missingFieldsError.js') -const { as } = require('pg-promise') +const NotFoundError = require('../errors/notFoundError.js') const getAllBooks = async (type, topic, author, pages, perpages) => { - const db = await dbConnection.connect() - - try { - let page = 1 - let perpage = 20 - let offset = 0 - - if (pages) { - page = pages - offset = (page - 1) * perpage - } - - if (perpages > 9 && perpages < 51) { - perpage = perpages - offset = (page - 1) * perpage - } + let page = 1 + let perpage = 20 + let offset = 0 - if (type) { - const sqlQuery = `select * from books where type = $1 limit = $2 offset = $3` - const result = await db.query(sqlQuery, [type]) + if (pages) { + page = pages + offset = (page - 1) * perpage + } - return result.rows - } + if (perpages > 9 && perpages < 51) { + perpage = perpages + offset = (page - 1) * perpage + } - if (topic) { - const sqlQuery = `select * from books where topic = $1` - const result = await db.query(sqlQuery, [topic]) + if (type) { + const sqlQuery = `select * from books where type = $1 limit = $2 offset = $3` + const result = await dbConnection.query(sqlQuery, [type]) - return result.rows - } + return result.rows + } - if (author) { - const sqlQuery = `select * from books where author = $1` - const result = await db.query(sqlQuery, [author]) + if (topic) { + const sqlQuery = `select * from books where topic = $1` + const result = await dbConnection.query(sqlQuery, [topic]) - return result.rows - } + return result.rows + } - const sqlQuery = `select * from books limit $1 offset $2` - const result = await db.query(sqlQuery, [perpage, offset]) + if (author) { + const sqlQuery = `select * from books where author = $1` + const result = await dbConnection.query(sqlQuery, [author]) return result.rows - } catch (e) { - console.log(e) - } finally { - db.release() } + + const sqlQuery = `select * from books limit $1 offset $2` + const result = await dbConnection.query(sqlQuery, [perpage, offset]) + + return result.rows } const createBook = async (book) => { - const db = await dbConnection.connect() - if (!verifyFields(book)) { throw new MissingFieldsError('Missing fields in request body') } - try { - const sqlQuery = `insert into books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` - const result = await db.query(sqlQuery, [book.title, book.type, book.author, book.topic, book.publication_date, book.pages]) + const sqlQuery = `insert into books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` + const result = await dbConnection.query(sqlQuery, [book.title, book.type, book.author, book.topic, book.publication_date, book.pages]) - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() - } + return result.rows[0] } const getBookById = async (id) => { - const db = await dbConnection.connect() - - try { - const sqlQuery = `select * from books where id = $1` - const result = await db.query(sqlQuery, [id]) + const sqlQuery = `select * from books where id = $1` + const result = await dbConnection.query(sqlQuery, [id]) - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() + if(result.rows.length === 0) { + throw new NotFoundError('A book with the provided ID does not exist') } + + return result.rows[0] } const updateBook = async (id, bookInfo) => { - const db = await dbConnection.connect() - - try { - const sqlQuery = `update books set title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 where id = $7 returning *` - const result = await db.query(sqlQuery, [bookInfo.title, bookInfo.type, bookInfo.author, bookInfo.topic, bookInfo.publication_date, bookInfo.pages, id]) + const sqlQuery = `update books set title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 where id = $7 returning *` + const result = await dbConnection.query(sqlQuery, [bookInfo.title, bookInfo.type, bookInfo.author, bookInfo.topic, bookInfo.publication_date, bookInfo.pages, id]) - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() - } + return result.rows[0] } const deleteBookById = async (id) => { - const db = await dbConnection.connect() + const sqlQuery = `delete from books where id = $1 returning *` + const result = await dbConnection.query(sqlQuery, [id]) - try { - const sqlQuery = `delete from books where id = $1 returning *` - const result = await db.query(sqlQuery, [id]) - - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() + if(result.rows.length === 0) { + throw new NotFoundError('A book with the provided ID does not exist') } + + return result.rows[0] } function verifyFields(object) { diff --git a/src/errors/notFoundError.js b/src/errors/notFoundError.js new file mode 100644 index 00000000..0a1f7abc --- /dev/null +++ b/src/errors/notFoundError.js @@ -0,0 +1,5 @@ +class NotFoundError extends Error { + +} + +module.exports = NotFoundError \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index d3474860..a37c6f39 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -16,19 +16,14 @@ router.get('/', async (req, res) => { }) }) -router.post('/', async (req, res, next) => { +router.post('/', async (req, res) => { const book = req.body - try { const newBook = await createBook(book) - res.json({ + res.status(201).json({ book: newBook }) - } catch(e) { - next(e) - } - }) router.get('/:id', async (req, res) => { diff --git a/src/server.js b/src/server.js index 3f89e0e7..9e954b87 100644 --- a/src/server.js +++ b/src/server.js @@ -1,5 +1,6 @@ require('dotenv').config() const express = require("express"); +require('express-async-errors'); const morgan = require("morgan"); const cors = require("cors"); @@ -12,18 +13,24 @@ app.use(express.json()); const booksRouter = require('./routers/books.js') const petsRouter = require('./routers/pets.js') const MissingFieldsError = require('./errors/missingFieldsError.js') +const NotFoundError = require('./errors/notFoundError.js') app.use('/books', booksRouter) app.use('/pets', petsRouter) app.use((error, req, res, next) => { - console.log('test') if (error instanceof MissingFieldsError) { return res.status(400).json({ error: error.message }) } + if (error instanceof NotFoundError) { + return res.status(404).json({ + error: error.message + }) + } + res.status(500).json({ message: 'Something went wrong' }) From abc3c1de27b7d4c5caf05ae53fdeb121cbd274c5 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Mon, 24 Jun 2024 12:24:06 +0200 Subject: [PATCH 11/14] add error handling for updating a book --- src/dal/booksRepo.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/dal/booksRepo.js b/src/dal/booksRepo.js index 4e52d200..bf4d62a0 100644 --- a/src/dal/booksRepo.js +++ b/src/dal/booksRepo.js @@ -67,9 +67,17 @@ const getBookById = async (id) => { } const updateBook = async (id, bookInfo) => { + if (!verifyFields(bookInfo)) { + throw new MissingFieldsError('Missing fields in request body') + } + const sqlQuery = `update books set title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 where id = $7 returning *` const result = await dbConnection.query(sqlQuery, [bookInfo.title, bookInfo.type, bookInfo.author, bookInfo.topic, bookInfo.publication_date, bookInfo.pages, id]) + if(result.rows.length === 0) { + throw new NotFoundError('A book with the provided ID does not exist') + } + return result.rows[0] } From cc5ae18d3b2a0c066b056dc33b780ef751bdc5d4 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Mon, 24 Jun 2024 12:39:42 +0200 Subject: [PATCH 12/14] clean up code and add pets pagination --- src/dal/booksRepo.js | 12 ++++++------ src/dal/petsRepo.js | 36 +++++++++++++++++++++--------------- src/routers/pets.js | 4 +++- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/dal/booksRepo.js b/src/dal/booksRepo.js index bf4d62a0..724027ec 100644 --- a/src/dal/booksRepo.js +++ b/src/dal/booksRepo.js @@ -18,22 +18,22 @@ const getAllBooks = async (type, topic, author, pages, perpages) => { } if (type) { - const sqlQuery = `select * from books where type = $1 limit = $2 offset = $3` - const result = await dbConnection.query(sqlQuery, [type]) + const sqlQuery = `select * from books where type = $1 limit $2 offset $3` + const result = await dbConnection.query(sqlQuery, [type, perpage, offset]) return result.rows } if (topic) { - const sqlQuery = `select * from books where topic = $1` - const result = await dbConnection.query(sqlQuery, [topic]) + const sqlQuery = `select * from books where topic = $1 limit $2 offset $3` + const result = await dbConnection.query(sqlQuery, [topic, perpage, offset]) return result.rows } if (author) { - const sqlQuery = `select * from books where author = $1` - const result = await dbConnection.query(sqlQuery, [author]) + const sqlQuery = `select * from books where author = $1 limit $2 offset $3` + const result = await dbConnection.query(sqlQuery, [author, perpage, offset]) return result.rows } diff --git a/src/dal/petsRepo.js b/src/dal/petsRepo.js index d54f5c39..a647994e 100644 --- a/src/dal/petsRepo.js +++ b/src/dal/petsRepo.js @@ -1,25 +1,31 @@ const dbConnection = require('../utils/dbConnection.js') -const getAllPets = async (type) => { - const db = await dbConnection.connect() - - try { - if (type) { - const sqlQuery = `select * from pets where type = $1` - const result = await db.query(sqlQuery, [type]) +const getAllPets = async (type, pages, perpages) => { + let page = 1 + let perpage = 20 + let offset = 0 + + if (pages) { + page = pages + offset = (page - 1) * perpage + } - return result.rows - } + if (perpages > 9 && perpages < 51) { + perpage = perpages + offset = (page - 1) * perpage + } - const sqlQuery = 'select * from pets' - const result = await db.query(sqlQuery) + if (type) { + const sqlQuery = `select * from pets where type = $1 limit $2 offset $3` + const result = await dbConnection.query(sqlQuery, [type, perpage, offset]) return result.rows - } catch (e) { - console.log(e) - } finally { - db.release() } + + const sqlQuery = 'select * from pets limit $1 offset $2' + const result = await dbConnection.query(sqlQuery, [perpage, offset]) + + return result.rows } const createPet = async (pet) => { diff --git a/src/routers/pets.js b/src/routers/pets.js index 6501f2f8..03e53142 100644 --- a/src/routers/pets.js +++ b/src/routers/pets.js @@ -4,8 +4,10 @@ const router = express.Router() router.get('/', async (req, res) => { const type = req.query.type + const page = req.query.page + const perPage = req.query.per_page - const pets = await getAllPets(type) + const pets = await getAllPets(type, page, perPage) res.json({ pets From 01fdc1c38779e2df6ef375f2379c1ef7dcb08342 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Mon, 24 Jun 2024 12:46:55 +0200 Subject: [PATCH 13/14] add error handling for pets --- src/dal/petsRepo.js | 78 +++++++++++++++++++++++---------------------- src/routers/pets.js | 2 +- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/dal/petsRepo.js b/src/dal/petsRepo.js index a647994e..6842304a 100644 --- a/src/dal/petsRepo.js +++ b/src/dal/petsRepo.js @@ -1,3 +1,5 @@ +const MissingFieldsError = require('../errors/missingFieldsError.js') +const NotFoundError = require('../errors/notFoundError.js') const dbConnection = require('../utils/dbConnection.js') const getAllPets = async (type, pages, perpages) => { @@ -29,63 +31,63 @@ const getAllPets = async (type, pages, perpages) => { } const createPet = async (pet) => { - const db = await dbConnection.connect() + if (!verifyFields(pet)) { + throw new MissingFieldsError('Missing fields in request body') + } - try { - const sqlQuery = `insert into pets (name, age, type, breed, has_microchip) values ($1, $2, $3, $4, $5) returning *` - const result = await db.query(sqlQuery, [pet.name, pet.age, pet.type, pet.breed, pet.has_microchip]) + const sqlQuery = `insert into pets (name, age, type, breed, has_microchip) values ($1, $2, $3, $4, $5) returning *` + const result = await dbConnection.query(sqlQuery, [pet.name, pet.age, pet.type, pet.breed, pet.has_microchip]) - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() - } + return result.rows[0] } const getPetById = async (id) => { - const db = await dbConnection.connect() - - try { - const sqlQuery = `select * from pets where id = $1` - const result = await db.query(sqlQuery, [id]) + const sqlQuery = `select * from pets where id = $1` + const result = await dbConnection.query(sqlQuery, [id]) - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() + if(result.rows.length === 0) { + throw new NotFoundError('A pet with the provided ID does not exist') } + + return result.rows[0] } const updatePet = async (id, petInfo) => { - const db = await dbConnection.connect() + if (!verifyFields(petInfo)) { + throw new MissingFieldsError('Missing fields in request body') + } - try { - const sqlQuery = `update pets set name = $1, age = $2, type = $3, breed = $4, has_microchip = $5 where id = $6 returning *` - const result = await db.query(sqlQuery, [petInfo.name, petInfo.age, petInfo.type, petInfo.breed, petInfo.has_microchip, id]) + const sqlQuery = `update pets set name = $1, age = $2, type = $3, breed = $4, has_microchip = $5 where id = $6 returning *` + const result = await dbConnection.query(sqlQuery, [petInfo.name, petInfo.age, petInfo.type, petInfo.breed, petInfo.has_microchip, id]) - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() + if(result.rows.length === 0) { + throw new NotFoundError('A pet with the provided ID does not exist') } + + return result.rows[0] } const deletePetById = async (id) => { - const db = await dbConnection.connect() + const sqlQuery = `delete from pets where id = $1 returning *` + const result = await dbConnection.query(sqlQuery, [id]) - try { - const sqlQuery = `delete from pets where id = $1 returning *` - const result = await db.query(sqlQuery, [id]) + if(result.rows.length === 0) { + throw new NotFoundError('A pet with the provided ID does not exist') + } + + return result.rows[0] +} - return result.rows[0] - } catch (e) { - console.log(e) - } finally { - db.release() +function verifyFields(object) { + const neededProperties = ['name', 'age', 'type', 'breed', 'has_microchip'] + + for (const item of neededProperties) { + if (object[item] === undefined) { + return false + } } + + return true } module.exports = { diff --git a/src/routers/pets.js b/src/routers/pets.js index 03e53142..afe309ec 100644 --- a/src/routers/pets.js +++ b/src/routers/pets.js @@ -19,7 +19,7 @@ router.post('/', async (req, res) => { const newPet = await createPet(pet) - res.json({ + res.status(201).json({ pet: newPet }) }) From 675d166ca384d1825e92502ac33a97d3c6d54953 Mon Sep 17 00:00:00 2001 From: Myrthe Dullaart Date: Mon, 24 Jun 2024 13:46:10 +0200 Subject: [PATCH 14/14] add breeds functions --- src/dal/booksRepo.js | 1 + src/dal/petsRepo.js | 14 +++++++++++++- src/errors/notUniqueError.js | 5 +++++ src/routers/breeds.js | 15 +++++++++++++++ src/server.js | 9 +++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/errors/notUniqueError.js create mode 100644 src/routers/breeds.js diff --git a/src/dal/booksRepo.js b/src/dal/booksRepo.js index 724027ec..077320b2 100644 --- a/src/dal/booksRepo.js +++ b/src/dal/booksRepo.js @@ -1,6 +1,7 @@ const dbConnection = require('../utils/dbConnection.js') const MissingFieldsError = require('../errors/missingFieldsError.js') const NotFoundError = require('../errors/notFoundError.js') +const NotUniqueError = require('../errors/notUniqueError.js') const getAllBooks = async (type, topic, author, pages, perpages) => { let page = 1 diff --git a/src/dal/petsRepo.js b/src/dal/petsRepo.js index 6842304a..45a3c5f7 100644 --- a/src/dal/petsRepo.js +++ b/src/dal/petsRepo.js @@ -78,6 +78,17 @@ const deletePetById = async (id) => { return result.rows[0] } +const getAllBreeds = async (type) => { + if (!type) { + throw new MissingFieldsError('Animal type is required') + } + + const sqlQuery = `select distinct breed from pets where type = $1` + const result = await dbConnection.query(sqlQuery, [type]) + + return result.rows +} + function verifyFields(object) { const neededProperties = ['name', 'age', 'type', 'breed', 'has_microchip'] @@ -95,5 +106,6 @@ module.exports = { createPet, getPetById, updatePet, - deletePetById + deletePetById, + getAllBreeds } \ No newline at end of file diff --git a/src/errors/notUniqueError.js b/src/errors/notUniqueError.js new file mode 100644 index 00000000..a96e8f02 --- /dev/null +++ b/src/errors/notUniqueError.js @@ -0,0 +1,5 @@ +class NotUniqueError extends Error { + +} + +module.exports = NotUniqueError \ No newline at end of file diff --git a/src/routers/breeds.js b/src/routers/breeds.js new file mode 100644 index 00000000..7a15dbab --- /dev/null +++ b/src/routers/breeds.js @@ -0,0 +1,15 @@ +const express = require('express') +const { getAllBreeds } = require('../dal/petsRepo.js') +const router = express.Router() + +router.get('/', async (req, res) => { + const type = req.query.type + + const breeds = await getAllBreeds(type) + + res.json({ + breeds + }) +}) + +module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index 9e954b87..fc6c2639 100644 --- a/src/server.js +++ b/src/server.js @@ -12,11 +12,14 @@ app.use(express.json()); const booksRouter = require('./routers/books.js') const petsRouter = require('./routers/pets.js') +const breedsRouter = require('./routers/breeds.js') const MissingFieldsError = require('./errors/missingFieldsError.js') const NotFoundError = require('./errors/notFoundError.js') +const NotUniqueError = require('./errors/notUniqueError.js') app.use('/books', booksRouter) app.use('/pets', petsRouter) +app.use('/breeds', breedsRouter) app.use((error, req, res, next) => { if (error instanceof MissingFieldsError) { @@ -31,6 +34,12 @@ app.use((error, req, res, next) => { }) } + if (error instanceof NotUniqueError) { + return res.status(409).json({ + error: error.message + }) + } + res.status(500).json({ message: 'Something went wrong' })