Open
Conversation
…ery params using if else statements and tried adding name in a separate route
HIPPIEKICK
approved these changes
Dec 10, 2024
Contributor
HIPPIEKICK
left a comment
There was a problem hiding this comment.
Good job with your first API Fanny! You've met the requirements, but I'd like to challenge you to think of a way to chain your filters 👀
Comment on lines
+25
to
+55
| app.get("/harryPotterCharacters", (req, res) => { | ||
| const house = req.query.house; // Query parameter for house | ||
| const year = req.query.yearIntroduced; // Query parameter for yearIntroduced | ||
| const role = req.query.role; // Query parameter for role | ||
|
|
||
| // Filter by house | ||
| if (house) { | ||
| const filteredCharacters = harryPotterCharactersData.filter(character => | ||
| character.house.toLowerCase() === house.toLowerCase() | ||
| ); | ||
| res.json(filteredCharacters); | ||
| } | ||
| // Filter by role | ||
| else if (role) { | ||
| const characterRole = harryPotterCharactersData.filter(character => | ||
| character.role.toLowerCase() === role.toLowerCase() | ||
| ); | ||
| res.json(characterRole); | ||
| } | ||
| // Filter by yearIntroduced | ||
| else if (year) { | ||
| const charactersIntroducedInYear = harryPotterCharactersData.filter(character => | ||
| character.yearIntroduced === +year | ||
| ); | ||
| res.json(charactersIntroducedInYear); | ||
| } | ||
| else { | ||
| res.json(harryPotterCharactersData); // No filters = return all characters | ||
|
|
||
| } | ||
| }); |
Contributor
There was a problem hiding this comment.
Nice job making use of query params to filter ⭐ Could you think of a way to improve this code so that the user can filter on two things at once? E.g. /characters?role=student&year=1997
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.
Netlify link
https://project-express-api-qyuo.onrender.com/