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! Just remember in future projects to keep it RESTful by making use of query params instead of creating endpoints for different filters. Endpoints should be named after what they return.
Comment on lines
+31
to
+42
| app.get("/songs/artist/:artistName", (req, res) => { | ||
| const artistName = req.params.artistName.toLowerCase(); | ||
| const songsByArtist = topMusicData.filter( | ||
| (song) => song.artistName.toLowerCase() === artistName | ||
| ); | ||
|
|
||
| if (songsByArtist.length > 0) { | ||
| res.json(songsByArtist); | ||
| } else { | ||
| res.status(404).json({ error: "No songs found for this artist" }); | ||
| } | ||
| }); |
Contributor
There was a problem hiding this comment.
This could be a query param under the songs endpoint to make it more RESTful: /songs?artist=beyonce
Comment on lines
+45
to
+79
| app.get("/songs/genre/:genre", (req, res) => { | ||
| const genre = req.params.genre.toLowerCase(); | ||
| const songsByGenre = topMusicData.filter( | ||
| (song) => song.genre.toLowerCase() === genre | ||
| ); | ||
|
|
||
| if (songsByGenre.length > 0) { | ||
| res.json(songsByGenre); | ||
| } else { | ||
| res.status(404).json({ error: "No songs found in this genre" }); | ||
| } | ||
| }); | ||
|
|
||
| // Filter by bpm and popularity | ||
| app.get("/songs/filter", (req, res) => { | ||
| const { bpm, popularity } = req.query; | ||
|
|
||
| let filteredSongs = topMusicData; | ||
|
|
||
| if (bpm) { | ||
| filteredSongs = filteredSongs.filter((song) => song.bpm === Number(bpm)); | ||
| } | ||
|
|
||
| if (popularity) { | ||
| filteredSongs = filteredSongs.filter( | ||
| (song) => song.popularity >= Number(popularity) | ||
| ); | ||
| } | ||
|
|
||
| if (filteredSongs.length > 0) { | ||
| res.json(filteredSongs); | ||
| } else { | ||
| res.status(404).json({ error: "No songs match the given criteria" }); | ||
| } | ||
| }); |
Contributor
There was a problem hiding this comment.
These too:
/songs?bpm=asc
/songs?popularity=asc
(ascending/descending)
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.
Render link
https://project-express-api-f8ka.onrender.com