From a391fffad7d6669f243a1fbf4de847807a9b8f6d Mon Sep 17 00:00:00 2001 From: Mobeen Date: Sun, 28 Jan 2024 12:40:02 +0100 Subject: [PATCH] done --- src/routers/books.js | 41 +++++++++++++++++++++++++++++++++++++-- src/routers/pets.js | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/server.js | 4 +++- 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/routers/pets.js diff --git a/src/routers/books.js b/src/routers/books.js index 1551dd87..54335f6e 100644 --- a/src/routers/books.js +++ b/src/routers/books.js @@ -3,7 +3,44 @@ const router = express.Router() const db = require("../../db"); router.get('/', async (req, res) => { - + const books = await db.query('SELECT * FROM books') + res.json({ books: books.rows }) }) -module.exports = router +router.get("/:id", async (req, res) => { + const { id } = req.params; + const result = await db.query("SELECT * FROM books WHERE id = $1", [id]); + res.json({ book: result.rows[0] }); + }); + + router.post('/', async (req, res) => { + const { title, type, author, topic, publication_date, pages } = req.body; + + const newBook = await db.query( + 'INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *', + [title, type, author, topic, publication_date, pages] + ) + res.status(201).json({ book: newBook.rows[0] }) + }) + + router.put('/:id', async (req, res) => { + const { id } = req.params; + const { title, type, author, topic, publication_date, pages } = req.body; + + const updatedBook = await db.query( + 'UPDATE books SET title = $2, type = $3, author = $4, topic = $5, publication_date = $6, pages = $7 WHERE id = $1 RETURNING *', + [id, title, type, author, topic, publication_date, pages] + ) + + res.status(201).json({ book: updatedBook.rows[0]}) + }) + + router.delete('/:id', async (req, res) => { + const { id } = req.params; + + const deletedBook = await db.query('DELETE FROM books WHERE id = $1 RETURNING *', [id]); + + res.status(201).json({ book: deletedBook.rows[0] }) + }) + +module.exports = router \ No newline at end of file diff --git a/src/routers/pets.js b/src/routers/pets.js new file mode 100644 index 00000000..d444b50a --- /dev/null +++ b/src/routers/pets.js @@ -0,0 +1,46 @@ +const express = require('express') +const router = express.Router() +const db = require("../../db"); + +router.get('/', async (req, res) => { + const pets = await db.query('SELECT * FROM pets') + res.json({ pets: pets.rows }) +}) + +router.get("/:id", async (req, res) => { + const { id } = req.params; + const result = await db.query("SELECT * FROM pets WHERE id = $1", [id]); + res.json({ pet: result.rows[0] }); + }); + + router.post('/', async (req, res) => { + const { name, age, type, breed, has_microchip } = req.body; + + const newpet = await db.query( + 'INSERT INTO pets (name, age, type, breed, has_microchip) VALUES ($1, $2, $3, $4, $5) RETURNING *', + [name, age, type, breed, has_microchip] + ) + res.status(201).json({ pet: newpet.rows[0] }) + }) + + router.put('/:id', async (req, res) => { + const { id } = req.params; + const { name, age, type, breed, has_microchip } = req.body; + + const updatedpet = await db.query( + 'UPDATE pets SET name = $2, age = $3, type = $4, breed = $5, has_microchip = $6 WHERE id = $1 RETURNING *', + [id, name, age, type, breed, has_microchip] + ) + + res.status(201).json({ pet: updatedpet.rows[0]}) + }) + + router.delete('/:id', async (req, res) => { + const { id } = req.params; + + const deletedpet = await db.query('DELETE FROM pets WHERE id = $1 RETURNING *', [id]); + + res.status(201).json({ pet: deletedpet.rows[0] }) + }) + +module.exports = router \ No newline at end of file diff --git a/src/server.js b/src/server.js index dac55e5d..3b2c276c 100644 --- a/src/server.js +++ b/src/server.js @@ -10,7 +10,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 +module.exports = app \ No newline at end of file