-
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
Top Music Data API by Joyce Kuo #525
base: master
Are you sure you want to change the base?
Conversation
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 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.
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" }); | ||
} | ||
}); |
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.
This could be a query param under the songs endpoint to make it more RESTful: /songs?artist=beyonce
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" }); | ||
} | ||
}); |
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.
These too:
/songs?bpm=asc
/songs?popularity=asc
(ascending/descending)
Render link
https://project-express-api-f8ka.onrender.com