From 8c81a2395a7e9c1f7e286d6c6b203da295a4e7c9 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Fri, 24 May 2024 22:17:29 +0200 Subject: [PATCH 01/10] First commit --- Models/Song.js | 36 +++++++++++++++++++++++++ package.json | 3 ++- project-mongo-api.code-workspace | 8 ++++++ server.js | 46 ++++++++++++++++---------------- 4 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 Models/Song.js create mode 100644 project-mongo-api.code-workspace diff --git a/Models/Song.js b/Models/Song.js new file mode 100644 index 000000000..7ac0dc178 --- /dev/null +++ b/Models/Song.js @@ -0,0 +1,36 @@ +import mongoose from "mongoose" + +// Schema - the blueprint + +const { Schema } = mongoose + +const musicSchema = new Schema({ +trackName: +{type: String, +required: true +}, + +artistName: {String, + required: true +}, +genre: {String, + required: true }, + +bpm: {Number, + required:true +}, + +energy: Number, +danceability: Number, +loudness: Number, +liveness: Number, +valence: Number, +length: Number, +acousticness: Number, +speechiness: Number, +popularity: Number +}) +// The Model +const Song = mongoose.model('Song', musicSchema) + +export default Song \ No newline at end of file diff --git a/package.json b/package.json index 6830a48aa..2d71dcaad 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", - "mongoose": "^8.0.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..15534516b 100644 --- a/server.js +++ b/server.js @@ -1,22 +1,28 @@ import express from "express"; import cors from "cors"; + +import expressListEndpoints from "express-list-endpoints" 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 mongoURL = process.env.MONGO_URL || "mongodb://localhost/topmusic" +mongoose.connect(mongoURL) +mongoose.Promise = Promise + +// import the data +import topMusicData from "./data/top-music.json" + +//Seed the database +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(); @@ -25,11 +31,5 @@ app.use(cors()); app.use(express.json()); // Start defining your routes here -app.get("/", (req, res) => { - res.send("Hello Technigo!"); -}); - -// Start the server -app.listen(port, () => { - console.log(`Server running on http://localhost:${port}`); -}); +// http://localhost:8080 + From c1b349ee28a4240c891c022a0403e184cc7adc1a Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Thu, 20 Jun 2024 20:45:49 +0200 Subject: [PATCH 02/10] added 4 routes --- package.json | 3 +- server.js | 80 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 2d71dcaad..eeffa8877 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,9 @@ "@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", + "express": "^4.19.2", "mongodb": "^6.6.1", "mongoose": "^8.3.4", "nodemon": "^3.0.1" diff --git a/server.js b/server.js index 15534516b..a759f8365 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,11 @@ -import express from "express"; -import cors from "cors"; +import express from "express" +import cors from "cors" import expressListEndpoints from "express-list-endpoints" -import mongoose from "mongoose"; +import mongoose from "mongoose" +import { Song } from "./Models/Song" -const mongoURL = process.env.MONGO_URL || "mongodb://localhost/topmusic" +const mongoURL = process.env.MONGO_URL || "mongodb://localhost/top-music" mongoose.connect(mongoURL) mongoose.Promise = Promise @@ -15,21 +16,80 @@ import topMusicData from "./data/top-music.json" const seedDatabase = async () => { await Song.deleteMany() - topMusicData.forEach(song => { + 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(); +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) => { + 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}`) +}) From 542db7914b2c032f63c1aec377fbb77faa0b1f67 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Thu, 20 Jun 2024 21:04:03 +0200 Subject: [PATCH 03/10] added dotenv --- package.json | 1 + server.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/package.json b/package.json index eeffa8877..8ffb0386e 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@babel/preset-env": "^7.16.11", "@types/express-list-endpoints": "^6.0.3", "cors": "^2.8.5", + "dotenv": "^16.4.5", "express": "^4.19.2", "mongodb": "^6.6.1", "mongoose": "^8.3.4", diff --git a/server.js b/server.js index a759f8365..ab883d3e0 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,7 @@ 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) @@ -12,6 +13,9 @@ mongoose.Promise = Promise // import the data import topMusicData from "./data/top-music.json" +// Configure dotenv +dotenv.config() + //Seed the database const seedDatabase = async () => { await Song.deleteMany() From ceb4c41594c9cb543cd7e2dfe0d48ba31122bb1d Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Thu, 20 Jun 2024 21:15:23 +0200 Subject: [PATCH 04/10] installed express-list-endpoints --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 8ffb0386e..670b5daa2 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "cors": "^2.8.5", "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" From 136716500b44e0e6fb2d90b4a65e58f8fc25c2fd Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Thu, 20 Jun 2024 21:19:40 +0200 Subject: [PATCH 05/10] updated schema --- Models/Song.js | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/Models/Song.js b/Models/Song.js index 7ac0dc178..9c2bc2a75 100644 --- a/Models/Song.js +++ b/Models/Song.js @@ -5,32 +5,24 @@ import mongoose from "mongoose" const { Schema } = mongoose const musicSchema = new Schema({ -trackName: -{type: String, -required: true -}, + trackName: { type: String }, -artistName: {String, - required: true -}, -genre: {String, - required: true }, + artistName: { String }, + genre: { String }, -bpm: {Number, - required:true -}, + bpm: { Number }, -energy: Number, -danceability: Number, -loudness: Number, -liveness: Number, -valence: Number, -length: Number, -acousticness: Number, -speechiness: Number, -popularity: Number + energy: Number, + danceability: Number, + loudness: Number, + liveness: Number, + valence: Number, + length: Number, + acousticness: Number, + speechiness: Number, + popularity: Number, }) // The Model -const Song = mongoose.model('Song', musicSchema) +const Song = mongoose.model("Song", musicSchema) -export default Song \ No newline at end of file +export default Song From 334cb8b4fa9969af811dca0d331500ac7f3bfa16 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Thu, 20 Jun 2024 21:32:28 +0200 Subject: [PATCH 06/10] updated schema again --- Models/Song.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Models/Song.js b/Models/Song.js index 9c2bc2a75..d83f23b95 100644 --- a/Models/Song.js +++ b/Models/Song.js @@ -5,12 +5,12 @@ import mongoose from "mongoose" const { Schema } = mongoose const musicSchema = new Schema({ - trackName: { type: String }, + trackName: { type: String, required: true }, - artistName: { String }, - genre: { String }, + artistName: { type: String, required: true }, + genre: { type: String, required: true }, - bpm: { Number }, + bpm: { type: Number, required: true }, energy: Number, danceability: Number, @@ -23,6 +23,4 @@ const musicSchema = new Schema({ popularity: Number, }) // The Model -const Song = mongoose.model("Song", musicSchema) - -export default Song +export const Song = mongoose.model("Song", musicSchema) From c432136778c5638d9fcfbdfe6067873133a6f8d0 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Thu, 20 Jun 2024 21:41:49 +0200 Subject: [PATCH 07/10] updated README file --- README.md | 3 +++ 1 file changed, 3 insertions(+) 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/ From 400867bd1803c207d2d2b6aac823b8a2d160e1af Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Mon, 24 Jun 2024 14:59:57 +0200 Subject: [PATCH 08/10] updated function for seeding the database --- server.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index ab883d3e0..349214d7e 100644 --- a/server.js +++ b/server.js @@ -17,14 +17,16 @@ import topMusicData from "./data/top-music.json" dotenv.config() //Seed the database -const seedDatabase = async () => { - await Song.deleteMany() +if (process.env.RESET_DB) { + const seedDatabase = async () => { + await Song.deleteMany({}) - topMusicData.forEach((song) => { - new Song(song).save() - }) + topMusicData.forEach((song) => { + new Song(song).save() + }) + } + seedDatabase() } -seedDatabase() // Defines the port the app will run on. const port = process.env.PORT || 8080 From c3da28e5c12ec75111e4acc974cf7abf8ecdcc1d Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Mon, 24 Jun 2024 15:08:57 +0200 Subject: [PATCH 09/10] updated from param to query --- server.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 349214d7e..d92ed9bef 100644 --- a/server.js +++ b/server.js @@ -56,7 +56,7 @@ app.get("/top-music", async (req, res) => { //Endpoint to fetch a song by trackname app.get("/top-music/trackname", async (req, res) => { - const { trackName } = req.params + const { trackName } = req.query const songByTrackName = await Song.find({ trackname: { $regex: new RegExp(trackName, "i") }, }).exec() @@ -71,7 +71,7 @@ app.get("/top-music/trackname", async (req, res) => { //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 { genre } = req.query const songByGenre = await Song.find({ genre: { $regex: new RegExp(genre, "i") }, }).exec() @@ -86,7 +86,7 @@ app.get("/top-music/genre", async (req, res) => { // 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() + const song = await Song.findById(songID).exec() if (song) { res.json(song) From f215d63224248fc8071f7191099ee4fd530e51b3 Mon Sep 17 00:00:00 2001 From: AHPIXI <148851757+AHPIXI@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:06:51 +0200 Subject: [PATCH 10/10] changed back --- server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index d92ed9bef..e92085a9b 100644 --- a/server.js +++ b/server.js @@ -56,7 +56,7 @@ app.get("/top-music", async (req, res) => { //Endpoint to fetch a song by trackname app.get("/top-music/trackname", async (req, res) => { - const { trackName } = req.query + const { trackName } = req.params const songByTrackName = await Song.find({ trackname: { $regex: new RegExp(trackName, "i") }, }).exec() @@ -71,7 +71,7 @@ app.get("/top-music/trackname", async (req, res) => { //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.query + const { genre } = req.params const songByGenre = await Song.find({ genre: { $regex: new RegExp(genre, "i") }, }).exec()