Express API - Kpop albums - Cholpon #532
Open
cholpoun wants to merge 6 commits intoTechnigo:masterfrom
Open
Conversation
HIPPIEKICK
approved these changes
Dec 10, 2024
Contributor
HIPPIEKICK
left a comment
There was a problem hiding this comment.
Good job creating your first API Cholpon! Just remember going forward to make use of query params instead of creating one endpoint for each filter
Contributor
There was a problem hiding this comment.
You don't need to include the compiled files, you can add it to your gitignore file
Comment on lines
+7
to
+18
| // Path to your JSON dataset | ||
| const dataPath = path.resolve("./data/kpop-album-releases.json"); | ||
|
|
||
| // Load the dataset | ||
| let data = []; | ||
| try { | ||
| const rawData = fs.readFileSync(dataPath, "utf-8"); | ||
| data = JSON.parse(rawData); | ||
| console.log("Dataset loaded successfully:", data.length, "records found."); | ||
| } catch (error) { | ||
| console.error("Error loading dataset:", error.message); | ||
| } |
Comment on lines
+64
to
113
| app.get("/api/albums/artist/:artist", (req, res) => { | ||
| const artist = req.params.artist.toLowerCase(); | ||
| const albums = data.filter((album) => | ||
| album.artist.toLowerCase().includes(artist) | ||
| ); | ||
|
|
||
| if (albums.length > 0) { | ||
| res.json(albums); | ||
| } else { | ||
| res.status(404).json({ error: "No albums found for the given artist" }); | ||
| } | ||
| }); | ||
|
|
||
| // Get albums by category | ||
| app.get("/api/albums/category/:category", (req, res) => { | ||
| const category = req.params.category.toLowerCase(); | ||
| const albums = data.filter( | ||
| (album) => album.category.toLowerCase() === category | ||
| ); | ||
|
|
||
| if (albums.length > 0) { | ||
| res.json(albums); | ||
| } else { | ||
| res.status(404).json({ error: "No albums found in the given category" }); | ||
| } | ||
| }); | ||
|
|
||
| // Get albums released in a specific year | ||
| app.get("/api/albums/year/:year", (req, res) => { | ||
| const year = parseInt(req.params.year); | ||
| const albums = data.filter((album) => album.year === year); | ||
|
|
||
| if (albums.length > 0) { | ||
| res.json(albums); | ||
| } else { | ||
| res.status(404).json({ error: "No albums found for the given year" }); | ||
| } | ||
| }); | ||
|
|
||
| // Get albums with a minimum rating | ||
| app.get("/api/albums/rating/:rating", (req, res) => { | ||
| const rating = parseFloat(req.params.rating); | ||
| const albums = data.filter((album) => album.rating >= rating); | ||
|
|
||
| if (albums.length > 0) { | ||
| res.json(albums); | ||
| } else { | ||
| res.status(404).json({ error: "No albums found with the given rating" }); | ||
| } | ||
| }); |
Contributor
There was a problem hiding this comment.
These could all be query params under the /albums endpoint, no need to create different endpoints for each filter. This way you also have the possibility to chain filters, e.g. if you want to filter on year and category
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-express-api-fy1c.onrender.com