Skip to content

Conversation

ElinaEH
Copy link

@ElinaEH ElinaEH commented Dec 14, 2024

Copy link
Contributor

@HIPPIEKICK HIPPIEKICK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done, just remember to use query params for filtering in the future!

Comment on lines +96 to 111
// Route to fetch book by author
app.get("/books/authors/:author", async (req, res) => {
const { author } = req.params;
try {
// Use a case-insensitive regex to match authors
const books = await Book.find({ authors: new RegExp(author, "i") });

if (books.length === 0) {
return res.status(404).send("No books found for this author"); // Handle no results
}
res.json(books); // Send list of books by the author
} catch(error) {
console.error("Error retrieving books by author", error); // Log error
res.status(500).send("Server error"); // Handle server errors
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember that even though you're filtering on an author, you're still returning books. So it would be more RESTful to make this a query param under the /books endpoint. Remember for future!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants