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

project-mongo-api-axel #501

Open
wants to merge 10 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
26 changes: 26 additions & 0 deletions Models/Song.js
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 8 additions & 0 deletions project-mongo-api.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}
120 changes: 93 additions & 27 deletions server.js
Original file line number Diff line number Diff line change
@@ -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}`)
})