From 0051255bf9f85b913b69db9ba22f41c2642db68a Mon Sep 17 00:00:00 2001 From: bvijayalakshmi7711 <149994121+bvijayalakshmi7711@users.noreply.github.com> Date: Fri, 1 Aug 2025 11:10:32 +0530 Subject: [PATCH] Update general.js --- final_project/router/general.js | 72 +++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/final_project/router/general.js b/final_project/router/general.js index 9eb0ac1a91..128ea0e4e9 100644 --- a/final_project/router/general.js +++ b/final_project/router/general.js @@ -11,33 +11,73 @@ public_users.post("/register", (req,res) => { }); // Get the book list available in the shop -public_users.get('/',function (req, res) { - //Write your code here - return res.status(300).json({message: "Yet to be implemented"}); +const books = require('./booksdb.js'); + +public_users.get('/', function (req, res) { + return res.status(200).send(JSON.stringify(books, null, 4)); }); // Get book details based on ISBN -public_users.get('/isbn/:isbn',function (req, res) { - //Write your code here - return res.status(300).json({message: "Yet to be implemented"}); - }); +public_users.get('/isbn/:isbn', function (req, res) { + const isbn = req.params.isbn; + const book = books[isbn]; + if (book) { + return res.status(200).send(book); + } else { + return res.status(404).send("Book not found"); + } +}); + // Get book details based on author -public_users.get('/author/:author',function (req, res) { - //Write your code here - return res.status(300).json({message: "Yet to be implemented"}); +public_users.get('/author/:author', function (req, res) { + const author = req.params.author; + const matchingBooks = []; + + for (let isbn in books) { + if (books[isbn].author === author) { + matchingBooks.push(books[isbn]); + } + } + + if (matchingBooks.length > 0) { + return res.status(200).send(matchingBooks); + } else { + return res.status(404).send("No books found by this author"); + } }); + // Get all books based on title -public_users.get('/title/:title',function (req, res) { - //Write your code here - return res.status(300).json({message: "Yet to be implemented"}); +public_users.get('/title/:title', function (req, res) { + const title = req.params.title; + const matchingBooks = []; + + for (let isbn in books) { + if (books[isbn].title === title) { + matchingBooks.push(books[isbn]); + } + } + + if (matchingBooks.length > 0) { + return res.status(200).send(matchingBooks); + } else { + return res.status(404).send("No books found with this title"); + } }); + // Get book review -public_users.get('/review/:isbn',function (req, res) { - //Write your code here - return res.status(300).json({message: "Yet to be implemented"}); +public_users.get('/review/:isbn', function (req, res) { + const isbn = req.params.isbn; + const book = books[isbn]; + + if (book && book.reviews) { + return res.status(200).send(book.reviews); + } else { + return res.status(404).send("No reviews found for this book"); + } }); + module.exports.general = public_users;