diff --git a/Models/Song.js b/Models/Song.js new file mode 100644 index 000000000..d83f23b95 --- /dev/null +++ b/Models/Song.js @@ -0,0 +1,26 @@ +import mongoose from "mongoose" + +// Schema - the blueprint + +const { Schema } = mongoose + +const musicSchema = new Schema({ + trackName: { type: String, required: true }, + + artistName: { type: String, required: true }, + genre: { type: String, required: true }, + + bpm: { type: Number, required: true }, + + energy: Number, + danceability: Number, + loudness: Number, + liveness: Number, + valence: Number, + length: Number, + acousticness: Number, + speechiness: Number, + popularity: Number, +}) +// The Model +export const Song = mongoose.model("Song", musicSchema) diff --git a/README.md b/README.md index 35019cd8a..ff941aaba 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,14 @@ 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 weeks project was to get familiar using a database and also RESTful API. ## The problem +I started to use much of the code from last weeks project, the express api. I continued to create a schema and a mongoose model. The i build the requiered routes. I found it hard to deploy, it took a lot of trying before it went worked! 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? ## 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-mongo-api-axel.onrender.com/ diff --git a/package.json b/package.json index 6830a48aa..670b5daa2 100644 --- a/package.json +++ b/package.json @@ -12,9 +12,13 @@ "@babel/core": "^7.17.9", "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", + "@types/express-list-endpoints": "^6.0.3", "cors": "^2.8.5", - "express": "^4.17.3", - "mongoose": "^8.0.0", + "dotenv": "^16.4.5", + "express": "^4.19.2", + "express-list-endpoints": "^7.1.0", + "mongodb": "^6.6.1", + "mongoose": "^8.3.4", "nodemon": "^3.0.1" } } diff --git a/project-mongo-api.code-workspace b/project-mongo-api.code-workspace new file mode 100644 index 000000000..d6c7df5fe --- /dev/null +++ b/project-mongo-api.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } +], + "settings": {} +} diff --git a/server.js b/server.js index 647e7b144..e92085a9b 100644 --- a/server.js +++ b/server.js @@ -1,35 +1,101 @@ -import express from "express"; -import cors from "cors"; -import mongoose from "mongoose"; - -// 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"; - -const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; -mongoose.connect(mongoUrl); -mongoose.Promise = Promise; - -// 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: -// PORT=9000 npm start -const port = process.env.PORT || 8080; -const app = express(); +import express from "express" +import cors from "cors" + +import expressListEndpoints from "express-list-endpoints" +import mongoose from "mongoose" +import { Song } from "./Models/Song" +import dotenv from "dotenv" + +const mongoURL = process.env.MONGO_URL || "mongodb://localhost/top-music" +mongoose.connect(mongoURL) +mongoose.Promise = Promise + +// import the data +import topMusicData from "./data/top-music.json" + +// Configure dotenv +dotenv.config() + +//Seed the database +if (process.env.RESET_DB) { + const seedDatabase = async () => { + await Song.deleteMany({}) + + topMusicData.forEach((song) => { + new Song(song).save() + }) + } + seedDatabase() +} + +// Defines the port the app will run on. +const port = process.env.PORT || 8080 +const app = express() // Add middlewares to enable cors and json body parsing -app.use(cors()); -app.use(express.json()); +app.use(cors()) +app.use(express.json()) // Start defining your routes here +// http://localhost:8080 app.get("/", (req, res) => { - res.send("Hello Technigo!"); -}); + const endpoints = expressListEndpoints(app) + res.json(endpoints) +}) +// Get all top music songs +// http://localhost:8080/top-music +app.get("/top-music", async (req, res) => { + const allMusic = await Song.find() + + if (allMusic.length > 0) { + res.json(allMusic) + } else { + res.status(404).send("No music was found :(") + } +}) + +//Endpoint to fetch a song by trackname +app.get("/top-music/trackname", async (req, res) => { + const { trackName } = req.params + const songByTrackName = await Song.find({ + trackname: { $regex: new RegExp(trackName, "i") }, + }).exec() + + if (songByTrackName.length > 0) { + res.json(songByTrackName) + } else { + res.status(404).send(`No song was found based on typed trackname`) + } +}) + +//Endpoint to fetch a song genre +app.get("/top-music/genre", async (req, res) => { + // const genreSearch = await Song.find({req.query.genre; + const { genre } = req.params + const songByGenre = await Song.find({ + genre: { $regex: new RegExp(genre, "i") }, + }).exec() + + if (songByGenre.length > 0) { + res.json(songByGenre) + } else { + res.status(404).send(`No song was found based on typed genre`) + } +}) + +// Endpoint to fetch song by id +app.get("/top-music/:songID", async (req, res) => { + const { songID } = req.params + const song = await Song.findById(songID).exec() + + if (song) { + res.json(song) + } else { + res.status(404).send("No song with that ID was found :(") + } +}) // Start the server app.listen(port, () => { - console.log(`Server running on http://localhost:${port}`); -}); + console.log(`Server running on http://localhost:${port}`) +})