-
Notifications
You must be signed in to change notification settings - Fork 515
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
project-18-mongo-api #516
base: master
Are you sure you want to change the base?
project-18-mongo-api #516
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good, just need to see a working deploy! Let us know if you need help debugging
|
||
|
||
// Ge books based on number of pages (needs to be befor bookID otherwise that route will override this) | ||
app.get("/books/pages", async (req, res) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be named just /books, because that's what this endpoint is returning
app.get("/books/authors/:author", async (req, res) => { | ||
const author = req.params.author; | ||
|
||
try { | ||
// $regex = to match name and not case sensitive | ||
const booksByAuthor = await Book.find({ | ||
authors: { $regex: author, $options: "i" }, // "i" case-insensitive | ||
}); | ||
|
||
if (booksByAuthor.length > 0) { | ||
res.json(booksByAuthor); // Returnera böckerna om de finns | ||
} else { | ||
res.status(404).json({ error: "No books found for this author" }); | ||
} | ||
} catch (error) { | ||
res.status(500).json({ error: "An error occurred while fetching books" }); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this should also be a query under your /books endpoint to make it more RESTful. Even though you're filtering on author, you still return books
Render
https://project-18-mongo-api.onrender.com/ (not working yet..)
Collaborators