From b9c16f94052e2c9035752c1a2ca8242537b76006 Mon Sep 17 00:00:00 2001 From: Farshad Date: Mon, 24 Jun 2024 16:44:08 +0200 Subject: [PATCH 1/6] getallbook & createBook done --- db/dbConnection.js | 16 ++++++++++++++++ db/index.js | 25 ------------------------- src/dal/booksRepo.js | 24 ++++++++++++++++++++++++ src/index.js | 3 +++ src/routers/books.js | 12 +++++++++++- src/server.js | 3 ++- 6 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 db/dbConnection.js delete mode 100644 db/index.js create mode 100644 src/dal/booksRepo.js diff --git a/db/dbConnection.js b/db/dbConnection.js new file mode 100644 index 00000000..4f622fcc --- /dev/null +++ b/db/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 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/dal/booksRepo.js b/src/dal/booksRepo.js new file mode 100644 index 00000000..8b4e4b13 --- /dev/null +++ b/src/dal/booksRepo.js @@ -0,0 +1,24 @@ +const dbConnection = require('../../db/dbConnection.js') + +const getAllBooks = async () => { + const sqlQuery = 'select * from books' + const result = await dbConnection.query(sqlQuery) + return result.rows +} + +const createBook = async (book) => { + const {title, type, author, topic, publication_date, pages } = book + + const sqlQuery = `INSERT INTO books(title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` + + const values = [title, type, author, topic, publication_date, pages] + + const result = await dbConnection.query(sqlQuery, values) + console.log(result) + return result.rows[0] +} + + + + +module.exports = { getAllBooks, createBook } \ No newline at end of file diff --git a/src/index.js b/src/index.js index c712fde7..3560f709 100644 --- a/src/index.js +++ b/src/index.js @@ -4,3 +4,6 @@ const port = 3030; app.listen(port, () => { console.log(`[SERVER] Running on http://localhost:${port}/`); }); + + +// setup a pool conection \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 1551dd87..9e4f6322 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,9 +1,19 @@ const express = require('express') const router = express.Router() -const db = require("../../db"); +const { getAllBooks, createBook } = require('../dal/booksRepo.js') router.get('/', async (req, res) => { + const books = await getAllBooks() + res.status(200).json({ + books + }) +}) +router.post('/', async (req, res) => { + const book = await createBook(req.body) + res.status(200).json({ + book + }) }) module.exports = router diff --git a/src/server.js b/src/server.js index dac55e5d..a0b40578 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"); @@ -11,6 +12,6 @@ app.use(express.json()); //TODO: Implement books and pets APIs using Express Modular Routers const booksRouter = require('./routers/books.js') -app.use('/books', booksRouter) +app.use('/books', booksRouter) module.exports = app From 55a0af4aff55dccc1ac496ae188f6b7f040c97d2 Mon Sep 17 00:00:00 2001 From: Farshad Date: Mon, 24 Jun 2024 18:54:18 +0200 Subject: [PATCH 2/6] books --- db/dbConnection.js | 26 ++++++++++++++------------ src/dal/booksRepo.js | 30 ++++++++++++++++++++++++++++-- src/routers/books.js | 29 ++++++++++++++++++++++++++++- test/database-cleaner/index.js | 4 ++-- test/helpers/createBook.js | 2 +- 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/db/dbConnection.js b/db/dbConnection.js index 4f622fcc..3fc2c96d 100644 --- a/db/dbConnection.js +++ b/db/dbConnection.js @@ -1,16 +1,18 @@ -const { Pool } = require('pg') +// Load our .env file +require('dotenv').config() -const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD } = process.env +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 - } +const dbClient = new Pool({ + host: PGHOST, + database: PGDATABASE, + username: PGUSER, + password: PGPASSWORD, + port: 5432, + ssl: { + require: true, + }, }) -module.exports = dbConnection \ No newline at end of file +module.exports = dbClient; \ No newline at end of file diff --git a/src/dal/booksRepo.js b/src/dal/booksRepo.js index 8b4e4b13..5f94434b 100644 --- a/src/dal/booksRepo.js +++ b/src/dal/booksRepo.js @@ -14,11 +14,37 @@ const createBook = async (book) => { const values = [title, type, author, topic, publication_date, pages] const result = await dbConnection.query(sqlQuery, values) - console.log(result) return result.rows[0] } +const getBookById = async (id) => { + const sqlQuery = `select * from books where id = $1` + const result = await dbConnection.query(sqlQuery, [id]) + return result.rows[0] +} + +const updateBook = async (id, book) => { + const {title, type, author, topic, publication_date, pages } = book + + const sqlQuery = `UPDATE books + SET title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 + WHERE id = $7 + RETURNING *` + + const values = [title, type, author, topic, publication_date, pages, id] + const result = await dbConnection.query(sqlQuery, values) + return result.rows[0] +} +const deleteBook = async (id) => { +const getBook = `select * from books where id = $1` + const deletedBook = await dbConnection.query(getBook, [id]) + + const sqlQuery = 'delete from books where id = $1' + const result = await dbConnection.query(sqlQuery, [id]) + + return deletedBook.rows[0] +} -module.exports = { getAllBooks, createBook } \ No newline at end of file +module.exports = { getAllBooks, createBook, getBookById, updateBook, deleteBook } \ No newline at end of file diff --git a/src/routers/books.js b/src/routers/books.js index 9e4f6322..d0484ecc 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -1,6 +1,6 @@ const express = require('express') const router = express.Router() -const { getAllBooks, createBook } = require('../dal/booksRepo.js') +const { getAllBooks, createBook, getBookById, updateBook, deleteBook } = require('../dal/booksRepo.js') router.get('/', async (req, res) => { const books = await getAllBooks() @@ -11,9 +11,36 @@ router.get('/', async (req, res) => { router.post('/', async (req, res) => { const book = await createBook(req.body) + res.status(201).json({ + book + }) +}) + +router.get('/:id', async (req, res) => { + const id = Number(req.params.id) + const book = await getBookById(id) res.status(200).json({ book }) }) +router.put('/:id', async (req, res) => { + const id = Number(req.params.id) + const newBookInfo = req.body + + const updatedBook = await updateBook(id, newBookInfo) + + res.status(201).json({ + book : updatedBook + }) +}) + +router.delete('/:id', async (req, res) => { + const id = Number(req.params.id) + const deletedBook = await deleteBook(id) + res.status(201).json({ + book : deletedBook + }) +}) + module.exports = router diff --git a/test/database-cleaner/index.js b/test/database-cleaner/index.js index 13a4e464..ebcfd643 100644 --- a/test/database-cleaner/index.js +++ b/test/database-cleaner/index.js @@ -1,5 +1,5 @@ const fs = require('fs/promises') -const client = require("../../db"); +const client = require("../../db/dbConnection.js") global.beforeEach(async() => { const sqlDataForBooks = await fs.readFile('./sql/create-books.sql') @@ -11,4 +11,4 @@ global.beforeEach(async() => { const sqlStringForPets = sqlDataForPets.toString() await client.query(sqlStringForPets) -}) +}) \ No newline at end of file diff --git a/test/helpers/createBook.js b/test/helpers/createBook.js index 5f7e9c76..aed16ec1 100644 --- a/test/helpers/createBook.js +++ b/test/helpers/createBook.js @@ -1,4 +1,4 @@ -const client = require("../../db"); +const client = require("../../db/dbConnection.js"); const createBook = async (values) => { const sqlString = `INSERT INTO "books" (title, type, author, topic, publication_date, pages) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;` From a9ed956a6e2b00813975f1d0dbf2ec36c821e9cc Mon Sep 17 00:00:00 2001 From: Farshad Date: Mon, 24 Jun 2024 19:13:40 +0200 Subject: [PATCH 3/6] made ssl false(temporary) --- db/dbConnection.js | 2 +- src/dal/petsRepo.js | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/routers/pets.js | 46 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/dal/petsRepo.js create mode 100644 src/routers/pets.js diff --git a/db/dbConnection.js b/db/dbConnection.js index 3fc2c96d..f61f1a52 100644 --- a/db/dbConnection.js +++ b/db/dbConnection.js @@ -11,7 +11,7 @@ const dbClient = new Pool({ password: PGPASSWORD, port: 5432, ssl: { - require: true, + require: false, }, }) diff --git a/src/dal/petsRepo.js b/src/dal/petsRepo.js new file mode 100644 index 00000000..5f94434b --- /dev/null +++ b/src/dal/petsRepo.js @@ -0,0 +1,50 @@ +const dbConnection = require('../../db/dbConnection.js') + +const getAllBooks = async () => { + const sqlQuery = 'select * from books' + const result = await dbConnection.query(sqlQuery) + return result.rows +} + +const createBook = async (book) => { + const {title, type, author, topic, publication_date, pages } = book + + const sqlQuery = `INSERT INTO books(title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` + + const values = [title, type, author, topic, publication_date, pages] + + const result = await dbConnection.query(sqlQuery, values) + return result.rows[0] +} + +const getBookById = async (id) => { + const sqlQuery = `select * from books where id = $1` + const result = await dbConnection.query(sqlQuery, [id]) + return result.rows[0] +} + +const updateBook = async (id, book) => { + const {title, type, author, topic, publication_date, pages } = book + + const sqlQuery = `UPDATE books + SET title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 + WHERE id = $7 + RETURNING *` + + + const values = [title, type, author, topic, publication_date, pages, id] + const result = await dbConnection.query(sqlQuery, values) + return result.rows[0] +} + +const deleteBook = async (id) => { +const getBook = `select * from books where id = $1` + const deletedBook = await dbConnection.query(getBook, [id]) + + const sqlQuery = 'delete from books where id = $1' + const result = await dbConnection.query(sqlQuery, [id]) + + return deletedBook.rows[0] +} + +module.exports = { getAllBooks, createBook, getBookById, updateBook, deleteBook } \ No newline at end of file diff --git a/src/routers/pets.js b/src/routers/pets.js new file mode 100644 index 00000000..d0484ecc --- /dev/null +++ b/src/routers/pets.js @@ -0,0 +1,46 @@ +const express = require('express') +const router = express.Router() +const { getAllBooks, createBook, getBookById, updateBook, deleteBook } = require('../dal/booksRepo.js') + +router.get('/', async (req, res) => { + const books = await getAllBooks() + res.status(200).json({ + books + }) +}) + +router.post('/', async (req, res) => { + const book = await createBook(req.body) + res.status(201).json({ + book + }) +}) + +router.get('/:id', async (req, res) => { + const id = Number(req.params.id) + const book = await getBookById(id) + res.status(200).json({ + book + }) +}) + +router.put('/:id', async (req, res) => { + const id = Number(req.params.id) + const newBookInfo = req.body + + const updatedBook = await updateBook(id, newBookInfo) + + res.status(201).json({ + book : updatedBook + }) +}) + +router.delete('/:id', async (req, res) => { + const id = Number(req.params.id) + const deletedBook = await deleteBook(id) + res.status(201).json({ + book : deletedBook + }) +}) + +module.exports = router From 0a9210df7f1ffc0831a03ce1a817bf2b6514bbc6 Mon Sep 17 00:00:00 2001 From: Farshad Date: Mon, 24 Jun 2024 19:15:50 +0200 Subject: [PATCH 4/6] back ssl to true --- db/dbConnection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/dbConnection.js b/db/dbConnection.js index f61f1a52..3fc2c96d 100644 --- a/db/dbConnection.js +++ b/db/dbConnection.js @@ -11,7 +11,7 @@ const dbClient = new Pool({ password: PGPASSWORD, port: 5432, ssl: { - require: false, + require: true, }, }) From fad41ecb99e115f42ffda814fe4e862f32a47118 Mon Sep 17 00:00:00 2001 From: Farshad Date: Mon, 24 Jun 2024 19:22:23 +0200 Subject: [PATCH 5/6] temporary changes --- src/dal/petsRepo.js | 72 +++++++++++++++++------------------ src/routers/pets.js | 92 ++++++++++++++++++++++----------------------- 2 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/dal/petsRepo.js b/src/dal/petsRepo.js index 5f94434b..08dd5d24 100644 --- a/src/dal/petsRepo.js +++ b/src/dal/petsRepo.js @@ -1,50 +1,50 @@ -const dbConnection = require('../../db/dbConnection.js') +// const dbConnection = require('../../db/dbConnection.js') -const getAllBooks = async () => { - const sqlQuery = 'select * from books' - const result = await dbConnection.query(sqlQuery) - return result.rows -} +// const getAllBooks = async () => { +// const sqlQuery = 'select * from books' +// const result = await dbConnection.query(sqlQuery) +// return result.rows +// } -const createBook = async (book) => { - const {title, type, author, topic, publication_date, pages } = book +// const createBook = async (book) => { +// const {title, type, author, topic, publication_date, pages } = book - const sqlQuery = `INSERT INTO books(title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` +// const sqlQuery = `INSERT INTO books(title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` - const values = [title, type, author, topic, publication_date, pages] +// const values = [title, type, author, topic, publication_date, pages] - const result = await dbConnection.query(sqlQuery, values) - return result.rows[0] -} +// const result = await dbConnection.query(sqlQuery, values) +// return result.rows[0] +// } -const getBookById = async (id) => { - const sqlQuery = `select * from books where id = $1` - const result = await dbConnection.query(sqlQuery, [id]) - return result.rows[0] -} +// const getBookById = async (id) => { +// const sqlQuery = `select * from books where id = $1` +// const result = await dbConnection.query(sqlQuery, [id]) +// return result.rows[0] +// } -const updateBook = async (id, book) => { - const {title, type, author, topic, publication_date, pages } = book +// const updateBook = async (id, book) => { +// const {title, type, author, topic, publication_date, pages } = book - const sqlQuery = `UPDATE books - SET title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 - WHERE id = $7 - RETURNING *` +// const sqlQuery = `UPDATE books +// SET title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 +// WHERE id = $7 +// RETURNING *` - const values = [title, type, author, topic, publication_date, pages, id] - const result = await dbConnection.query(sqlQuery, values) - return result.rows[0] -} +// const values = [title, type, author, topic, publication_date, pages, id] +// const result = await dbConnection.query(sqlQuery, values) +// return result.rows[0] +// } -const deleteBook = async (id) => { -const getBook = `select * from books where id = $1` - const deletedBook = await dbConnection.query(getBook, [id]) +// const deleteBook = async (id) => { +// const getBook = `select * from books where id = $1` +// const deletedBook = await dbConnection.query(getBook, [id]) - const sqlQuery = 'delete from books where id = $1' - const result = await dbConnection.query(sqlQuery, [id]) +// const sqlQuery = 'delete from books where id = $1' +// const result = await dbConnection.query(sqlQuery, [id]) - return deletedBook.rows[0] -} +// return deletedBook.rows[0] +// } -module.exports = { getAllBooks, createBook, getBookById, updateBook, deleteBook } \ No newline at end of file +// module.exports = { getAllBooks, createBook, getBookById, updateBook, deleteBook } \ No newline at end of file diff --git a/src/routers/pets.js b/src/routers/pets.js index d0484ecc..39ad0cf2 100644 --- a/src/routers/pets.js +++ b/src/routers/pets.js @@ -1,46 +1,46 @@ -const express = require('express') -const router = express.Router() -const { getAllBooks, createBook, getBookById, updateBook, deleteBook } = require('../dal/booksRepo.js') - -router.get('/', async (req, res) => { - const books = await getAllBooks() - res.status(200).json({ - books - }) -}) - -router.post('/', async (req, res) => { - const book = await createBook(req.body) - res.status(201).json({ - book - }) -}) - -router.get('/:id', async (req, res) => { - const id = Number(req.params.id) - const book = await getBookById(id) - res.status(200).json({ - book - }) -}) - -router.put('/:id', async (req, res) => { - const id = Number(req.params.id) - const newBookInfo = req.body - - const updatedBook = await updateBook(id, newBookInfo) - - res.status(201).json({ - book : updatedBook - }) -}) - -router.delete('/:id', async (req, res) => { - const id = Number(req.params.id) - const deletedBook = await deleteBook(id) - res.status(201).json({ - book : deletedBook - }) -}) - -module.exports = router +// const express = require('express') +// const router = express.Router() +// const { getAllBooks, createBook, getBookById, updateBook, deleteBook } = require('../dal/booksRepo.js') + +// router.get('/', async (req, res) => { +// const books = await getAllBooks() +// res.status(200).json({ +// books +// }) +// }) + +// router.post('/', async (req, res) => { +// const book = await createBook(req.body) +// res.status(201).json({ +// book +// }) +// }) + +// router.get('/:id', async (req, res) => { +// const id = Number(req.params.id) +// const book = await getBookById(id) +// res.status(200).json({ +// book +// }) +// }) + +// router.put('/:id', async (req, res) => { +// const id = Number(req.params.id) +// const newBookInfo = req.body + +// const updatedBook = await updateBook(id, newBookInfo) + +// res.status(201).json({ +// book : updatedBook +// }) +// }) + +// router.delete('/:id', async (req, res) => { +// const id = Number(req.params.id) +// const deletedBook = await deleteBook(id) +// res.status(201).json({ +// book : deletedBook +// }) +// }) + +// module.exports = router From 97469daa4d320707049e159c0c4389be876cf2ae Mon Sep 17 00:00:00 2001 From: Farshad Date: Tue, 25 Jun 2024 09:01:07 +0200 Subject: [PATCH 6/6] pets tests Done --- src/dal/booksRepo.js | 2 +- src/dal/petsRepo.js | 71 ++++++++++++++-------------- src/routers/pets.js | 93 +++++++++++++++++++------------------ src/server.js | 2 + test/helpers/createPet.js | 2 +- test/helpers/insertBooks.js | 2 +- test/helpers/insertPets.js | 2 +- 7 files changed, 88 insertions(+), 86 deletions(-) diff --git a/src/dal/booksRepo.js b/src/dal/booksRepo.js index 5f94434b..403d04b2 100644 --- a/src/dal/booksRepo.js +++ b/src/dal/booksRepo.js @@ -9,7 +9,7 @@ const getAllBooks = async () => { const createBook = async (book) => { const {title, type, author, topic, publication_date, pages } = book - const sqlQuery = `INSERT INTO books(title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` + const sqlQuery = `INSERT INTO books (title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` const values = [title, type, author, topic, publication_date, pages] diff --git a/src/dal/petsRepo.js b/src/dal/petsRepo.js index 08dd5d24..79aa3308 100644 --- a/src/dal/petsRepo.js +++ b/src/dal/petsRepo.js @@ -1,50 +1,49 @@ -// const dbConnection = require('../../db/dbConnection.js') +const dbConnection = require('../../db/dbConnection.js') -// const getAllBooks = async () => { -// const sqlQuery = 'select * from books' -// const result = await dbConnection.query(sqlQuery) -// return result.rows -// } +const getAllPets = async () => { + const sqlQuery = 'select * from pets' + const result = await dbConnection.query(sqlQuery) + return result.rows +} -// const createBook = async (book) => { -// const {title, type, author, topic, publication_date, pages } = book +const createPet = async (pet) => { + const {name, age, type, breed, has_microchip } = pet -// const sqlQuery = `INSERT INTO books(title, type, author, topic, publication_date, pages) values ($1, $2, $3, $4, $5, $6) returning *` + const sqlQuery = `INSERT INTO pets (name, age, type, breed, has_microchip) values ($1, $2, $3, $4, $5) returning *` -// const values = [title, type, author, topic, publication_date, pages] + const values = [name, age, type, breed, has_microchip] -// const result = await dbConnection.query(sqlQuery, values) -// return result.rows[0] -// } + const result = await dbConnection.query(sqlQuery, values) + return result.rows[0] +} -// const getBookById = async (id) => { -// const sqlQuery = `select * from books where id = $1` -// const result = await dbConnection.query(sqlQuery, [id]) -// return result.rows[0] -// } +const getPetById = async (id) => { + const sqlQuery = `select * from pets where id = $1` + const result = await dbConnection.query(sqlQuery, [id]) + return result.rows[0] +} -// const updateBook = async (id, book) => { -// const {title, type, author, topic, publication_date, pages } = book +const updatePet = async (id, pet) => { + const {name, age, type, breed, has_microchip } = pet -// const sqlQuery = `UPDATE books -// SET title = $1, type = $2, author = $3, topic = $4, publication_date = $5, pages = $6 -// WHERE id = $7 -// RETURNING *` + const sqlQuery = `UPDATE pets + SET name = $1, age = $2, type = $3, breed = $4, has_microchip = $5 WHERE id = $6 + RETURNING *` -// const values = [title, type, author, topic, publication_date, pages, id] -// const result = await dbConnection.query(sqlQuery, values) -// return result.rows[0] -// } + const values = [name, age, type, breed, has_microchip, id] + const result = await dbConnection.query(sqlQuery, values) + return result.rows[0] +} -// const deleteBook = async (id) => { -// const getBook = `select * from books where id = $1` -// const deletedBook = await dbConnection.query(getBook, [id]) +const deletePet = async (id) => { +const getPet = `select * from pets where id = $1` + const deletedPet = await dbConnection.query(getPet, [id]) -// const sqlQuery = 'delete from books where id = $1' -// const result = await dbConnection.query(sqlQuery, [id]) + const sqlQuery = 'delete from pets where id = $1' + const result = await dbConnection.query(sqlQuery, [id]) -// return deletedBook.rows[0] -// } + return deletedPet.rows[0] +} -// module.exports = { getAllBooks, createBook, getBookById, updateBook, deleteBook } \ No newline at end of file +module.exports = { getAllPets, createPet,getPetById, updatePet, deletePet } \ No newline at end of file diff --git a/src/routers/pets.js b/src/routers/pets.js index 39ad0cf2..c9558fc0 100644 --- a/src/routers/pets.js +++ b/src/routers/pets.js @@ -1,46 +1,47 @@ -// const express = require('express') -// const router = express.Router() -// const { getAllBooks, createBook, getBookById, updateBook, deleteBook } = require('../dal/booksRepo.js') - -// router.get('/', async (req, res) => { -// const books = await getAllBooks() -// res.status(200).json({ -// books -// }) -// }) - -// router.post('/', async (req, res) => { -// const book = await createBook(req.body) -// res.status(201).json({ -// book -// }) -// }) - -// router.get('/:id', async (req, res) => { -// const id = Number(req.params.id) -// const book = await getBookById(id) -// res.status(200).json({ -// book -// }) -// }) - -// router.put('/:id', async (req, res) => { -// const id = Number(req.params.id) -// const newBookInfo = req.body - -// const updatedBook = await updateBook(id, newBookInfo) - -// res.status(201).json({ -// book : updatedBook -// }) -// }) - -// router.delete('/:id', async (req, res) => { -// const id = Number(req.params.id) -// const deletedBook = await deleteBook(id) -// res.status(201).json({ -// book : deletedBook -// }) -// }) - -// module.exports = router +const express = require('express') +const router = express.Router() +const { getAllPets, createPet + , getPetById, updatePet, deletePet} = require('../dal/petsRepo.js') + +router.get('/', async (req, res) => { + const pets = await getAllPets() + res.status(200).json({ + pets + }) +}) + +router.post('/', async (req, res) => { + const pet = await createPet(req.body) + res.status(201).json({ + pet + }) +}) + +router.get('/:id', async (req, res) => { + const id = Number(req.params.id) + const pet = await getPetById(id) + res.status(200).json({ + pet + }) +}) + +router.put('/:id', async (req, res) => { + const id = Number(req.params.id) + const newPetInfo = req.body + + const updatedPet = await updatePet(id, newPetInfo) + + res.status(201).json({ + pet : updatedPet + }) +}) + +router.delete('/:id', async (req, res) => { + const id = Number(req.params.id) + const deletedPet = await deletePet(id) + res.status(201).json({ + pet : deletedPet + }) +}) + +module.exports = router diff --git a/src/server.js b/src/server.js index a0b40578..a45a8f80 100644 --- a/src/server.js +++ b/src/server.js @@ -11,7 +11,9 @@ app.use(express.json()); //TODO: Implement books and pets APIs using Express Modular Routers const booksRouter = require('./routers/books.js') +const petsRouter = require('./routers/pets.js') app.use('/books', booksRouter) +app.use('/pets', petsRouter) module.exports = app diff --git a/test/helpers/createPet.js b/test/helpers/createPet.js index 8dfca845..3453a2dc 100644 --- a/test/helpers/createPet.js +++ b/test/helpers/createPet.js @@ -1,4 +1,4 @@ -const client = require("../../db"); +const client = require("../../db/dbConnection.js"); const createPet = async (values) => { const sqlString = `INSERT INTO "pets" (name, age, type, breed, has_microchip) VALUES ($1, $2, $3, $4, $5) RETURNING *;` diff --git a/test/helpers/insertBooks.js b/test/helpers/insertBooks.js index 54e720ca..28aa88a9 100644 --- a/test/helpers/insertBooks.js +++ b/test/helpers/insertBooks.js @@ -1,5 +1,5 @@ const fs = require('fs/promises') -const client = require("../../db"); +const client = require("../../db/dbConnection.js"); const insertBooks = async () => { const sqlDataForBooks = await fs.readFile('./sql/insert-books.sql') diff --git a/test/helpers/insertPets.js b/test/helpers/insertPets.js index eadddec2..8b885c40 100644 --- a/test/helpers/insertPets.js +++ b/test/helpers/insertPets.js @@ -1,5 +1,5 @@ const fs = require('fs/promises') -const client = require("../../db"); +const client = require("../../db/dbConnection.js"); const insertPets = async () => { const sqlDataForPets = await fs.readFile('./sql/insert-pets.sql')