Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
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/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@babel/preset-env": "^7.16.11",
"cors": "^2.8.5",
"express": "^4.17.3",
"express-list-endpoints": "^7.1.1",
"nodemon": "^3.0.1"
}
}
78 changes: 76 additions & 2 deletions server.js
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:
Expand All @@ -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" });
}
});
Comment on lines +31 to +42
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


// 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
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)


// 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
Expand Down