Project-Mongo-API - Elina Eriksson Hult#509
Open
ElinaEH wants to merge 6 commits intoTechnigo:masterfrom
Open
Conversation
HIPPIEKICK
approved these changes
Dec 18, 2024
Contributor
HIPPIEKICK
left a comment
There was a problem hiding this comment.
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 | ||
| } | ||
| }); |
Contributor
There was a problem hiding this comment.
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://project-mongo-api-elina.onrender.com