generated from Technigo/express-api-starter
-
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
Open
JoyceKuode
wants to merge
3
commits into
Technigo:master
Choose a base branch
from
JoyceKuode:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
# Project Express API | ||
|
||
Replace this readme with your own information about your project. | ||
|
||
Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
This project is a RESTful API built with Express.js that uses data from a JSON file of popular songs. It allows users to fetch a list of songs, get details for a specific song, and filter songs by various criteria such as genre, artist, BPM, and popularity. | ||
|
||
## The problem | ||
|
||
Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
I planned the project by deciding which endpoints I needed and how to structure the data to handle route logic. I used Express List Endpoints to generate automatic documentation, making it easy to navigate and test the API. I tested using Chrome to verify that each endpoint returned the correct data and returned meaningful error messages for invalid inputs. | ||
|
||
If I had more time I would add more filtering capabilities, such as filtering by energy or danceability. I would also build a frontend interface and implement pagination to better organize and display the data. | ||
|
||
## View it live | ||
|
||
Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about. | ||
https://project-express-api-f8ka.onrender.com/ |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import express from "express"; | ||
import cors from "cors"; | ||
import expressListEndpoints from "express-list-endpoints"; | ||
|
||
// If you're using one of our datasets, uncomment the appropriate import below | ||
// to get started! | ||
// import avocadoSalesData from "./data/avocado-sales.json"; | ||
// import booksData from "./data/books.json"; | ||
// import goldenGlobesData from "./data/golden-globes.json"; | ||
// import netflixData from "./data/netflix-titles.json"; | ||
// import topMusicData from "./data/top-music.json"; | ||
import topMusicData from "./data/top-music.json"; | ||
|
||
// Defines the port the app will run on. Defaults to 8080, but can be overridden | ||
// when starting the server. Example command to overwrite PORT env variable value: | ||
|
@@ -20,8 +21,81 @@ app.use(cors()); | |
app.use(express.json()); | ||
|
||
// Start defining your routes here | ||
|
||
// Route to return entire song array | ||
app.get("/songs", (req, res) => { | ||
res.json(topMusicData); | ||
}); | ||
|
||
// Route for songs by artist | ||
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" }); | ||
} | ||
}); | ||
|
||
// Route for songs by genre | ||
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" }); | ||
} | ||
}); | ||
Comment on lines
+45
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These too: (ascending/descending) |
||
|
||
// Route for single song info by ID | ||
app.get("/songs/:id", (req, res) => { | ||
const songId = Number(req.params.id); | ||
const song = topMusicData.find((song) => song.id === songId); | ||
|
||
if (song) { | ||
res.json(song); | ||
} else { | ||
res.status(404).json({ error: "Song not found" }); | ||
} | ||
}); | ||
|
||
// Documentation route | ||
app.get("/", (req, res) => { | ||
res.send("Hello Technigo!"); | ||
res.json({ | ||
message: "Welcome to Joyce's Song API", | ||
documentation: expressListEndpoints(app), | ||
}); | ||
}); | ||
|
||
// Start the server | ||
|
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.
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