-
Notifications
You must be signed in to change notification settings - Fork 529
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
Jonas Express API #518
base: master
Are you sure you want to change the base?
Jonas Express API #518
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.
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.
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 ⭐
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" | ||
} | ||
}); | ||
}); |
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.
Express List Endpoints is a great package so you don't have to update your docs manually
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); | ||
}); |
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.
Both of these could be query params under the /boardgames route. Remember to name endpoints after what they return :)
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).