Conversation
- Set up Express server with CORS and JSON parsing middleware. - Added a root endpoint (/) that provides API documentation with available endpoints. - Implemented a GET endpoint (/boardgames) to return all boardgames. - Implemented a GET endpoint (/category) to filter boardgames by category using query parameters. - Implemented a GET endpoint (/maxPlayers) to filter boardgames by maximum number of players using query parameters. - Implemented a GET endpoint (/boardgames/:id) to retrieve a specific boardgame by its unique ID. - Enhanced error handling for cases where data is not found or invalid parameters are provided. - Included robust filtering and querying functionality for better usability. - Verified and tested all endpoints to ensure correct functionality and consistency.
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 Jonas! For coming weeks, please remember that endpoints should be named after what they return (so all filtering/sorting can happen in the /boardgames route in this case, since you're returning boardgames). Nice to see that you did some extra filtering though ⭐
Comment on lines
+13
to
+22
| res.json({ | ||
| message: "Welcome to the Jonas boardgames API! Choose from the endpoints below:", | ||
| endpoints: { | ||
| "/boardgames": "Explore all boardgames", | ||
| "/category?category=<category>": "Get all boardgames by category", | ||
| "/maxPlayers?maxPlayers=<number>": "Get all boardgames by maximum number of players", | ||
| "/boardgames/:id": "Get a single boardgames by ID" | ||
| } | ||
| }); | ||
| }); |
Contributor
There was a problem hiding this comment.
Express List Endpoints is a great package so you don't have to update your docs manually
Comment on lines
+30
to
+51
| app.get("/category", (req, res) => { | ||
| const category = req.query.category; | ||
| const filteredGames = boardgames.filter(game => | ||
| game.category.toLowerCase() === (category || "").toLowerCase() | ||
| ); | ||
| res.json(filteredGames); | ||
| }); | ||
|
|
||
| // Return all boardgames with a specific maximum number of players | ||
| app.get("/maxPlayers", (req, res) => { | ||
| const maxPlayers = parseInt(req.query.maxPlayers, 10); | ||
|
|
||
| if (isNaN(maxPlayers)) { | ||
| return res.json([]); | ||
| } | ||
|
|
||
| const filteredMaxPlayers = boardgames.filter(game => | ||
| game.maxPlayers === maxPlayers | ||
| ); | ||
|
|
||
| res.json(filteredMaxPlayers); | ||
| }); |
Contributor
There was a problem hiding this comment.
Both of these could be query params under the /boardgames route. Remember to name endpoints after what they return :)
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://jonas-project-express-api.onrender.com/
Root endpoint: Provides API documentation with a list of available endpoints and their descriptions.
https://jonas-project-express-api.onrender.com/boardgames
Returns a collection of all boardgames available in the API.
https://jonas-project-express-api.onrender.com/category?category=Strategy
Filters and returns boardgames that belong to the specified category ('Strategy' in this example).
https://jonas-project-express-api.onrender.com/maxPlayers?maxPlayers=4
Filters and returns boardgames that can be played by the specified maximum number of players (4 in this example).
https://jonas-project-express-api.onrender.com/boardgames/1
Returns details of a specific boardgame based on its unique ID (1 in this example).