From eba5db9395e615e9f05c0c00a015d507ab2ddd6a Mon Sep 17 00:00:00 2001 From: JoyceKuode Date: Sat, 7 Dec 2024 15:32:16 +0100 Subject: [PATCH 1/3] Add documentation route, routes for song array, by artist, by genre, and by ID --- package.json | 1 + server.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f93ddb524..fcbf92020 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/server.js b/server.js index b5fec6fe2..14f3fb4af 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ 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! @@ -7,7 +8,7 @@ import cors from "cors"; // 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,61 @@ app.use(cors()); app.use(express.json()); // Start defining your routes here +// app.get("/", (req, res) => { +// res.send("Hello Technigo!"); +// }); + +// Route to return entire song array +app.get("/songs", (req, res) => { + res.json(topMusicData); +}); + +// 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" }); + } +}); + +// 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" }); + } +}); + +// Documentation route app.get("/", (req, res) => { - res.send("Hello Technigo!"); + res.json({ + message: "Welcome to the API", + documentation: expressListEndpoints(app), + }); }); // Start the server From 7cd4f172593ca0d2c9efe3e26bf498956a17949d Mon Sep 17 00:00:00 2001 From: JoyceKuode Date: Sat, 7 Dec 2024 15:58:01 +0100 Subject: [PATCH 2/3] Add route to filter by bpm and popularity --- server.js | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/server.js b/server.js index 14f3fb4af..b46856bec 100644 --- a/server.js +++ b/server.js @@ -21,27 +21,12 @@ app.use(cors()); app.use(express.json()); // Start defining your routes here -// app.get("/", (req, res) => { -// res.send("Hello Technigo!"); -// }); // Route to return entire song array app.get("/songs", (req, res) => { res.json(topMusicData); }); -// 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" }); - } -}); - // Route for songs by artist app.get("/songs/artist/:artistName", (req, res) => { const artistName = req.params.artistName.toLowerCase(); @@ -70,10 +55,45 @@ app.get("/songs/genre/:genre", (req, res) => { } }); +// 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" }); + } +}); + +// 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.json({ - message: "Welcome to the API", + message: "Welcome to Joyce's Song API", documentation: expressListEndpoints(app), }); }); From 5ee84c363340229e1c9e66386e042af45b2969c2 Mon Sep 17 00:00:00 2001 From: JoyceKuode Date: Sat, 7 Dec 2024 16:05:56 +0100 Subject: [PATCH 3/3] Update README --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5241826b3..d5238eed0 100644 --- a/README.md +++ b/README.md @@ -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/