Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 56 additions & 16 deletions final_project/router/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;