diff --git a/README.md b/README.md index 5241826b..d5238eed 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/ diff --git a/package.json b/package.json index f93ddb52..fcbf9202 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 b5fec6fe..b46856be 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,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" }); + } +}); + +// 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