Skip to content

Conversation

@JoyceKuode
Copy link

Copy link
Contributor

@HIPPIEKICK HIPPIEKICK left a 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.

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" });
}
});
Copy link
Contributor

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

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" });
}
});
Copy link
Contributor

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants