Skip to content

Update general.js #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
65 changes: 56 additions & 9 deletions final_project/router/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,68 @@ 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"});
public_users.get('/',async function (req, res) {
try{
const getBooksAsync = () => {
return new promise((resolve, reject) => {
setTimeout(() => {
resolve(books);
},100);
});
});

const allBooks = awit getBooksAsync();
return res.status(200).send(JSON.stringify(allBooks, null, 4));
}catch(err){
return res.status(500).json({message: "Failed to retrieve books."});
}

});

// 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',async function (req, res) {
const isbn = req.parms.isbn;

const getBookByISBN = (isbn) => {
return new promise((resolve, reject) => {
const book = books[isbn]
if(book) {
resolve(book);
}else{
reject("Book not found");
}
});
};

try{
const book = await getBookByISBN(isbn);
return res.status(200).json(book);
}catch(error){
return res.status(404).json({ message: error });
}
});

// 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',async function (req, res) {
const author = req.params.author;

const getBooksByAuthor = (author) => {
return new Promise((resolve, reject) => {
const booksByAuthor = Object.values(books).filter(book => book.author === author);
if (booksByAuthor.length > 0) {
resolve(booksByAuthor);
} else {
reject("No books found by this author");
}
});
};

try {
const booksByAuthor = await getBooksByAuthor(author);
return res.status(200).json(booksByAuthor);
} catch (error) {
return res.status(404).json({ message: error });
}
});

// Get all books based on title
Expand Down