diff --git a/package.json b/package.json index 6830a48aa..1023d06b8 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,10 @@ "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", - "express": "^4.17.3", + "express": "^4.19.2", + "express-list-endpoints": "^7.1.0", + "mongo": "^0.1.0", + "mongodb": "^6.6.1", "mongoose": "^8.0.0", "nodemon": "^3.0.1" } diff --git a/server.js b/server.js index 647e7b144..7891a2a3c 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,38 @@ 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"; +import goldenGlobes from "./data/golden-globes.json"; +import expressListEndpoints from "express-list-endpoints"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; +const { Schema } = mongoose; +const globesSchema = new Schema({ + year_film: Number, + year_award: Number, + ceremony: Number, + category: String, + nominee: String, + film: String, + win: Boolean, +}); + +const Globes = mongoose.model("Globes", globesSchema); + +if (process.env.RESET_DATABASE) { + const seedDatabase = async () => { + await Globes.deleteMany(); + + goldenGlobes.forEach((globesData) => { + new Globes(globesData).save(); + }); + }; + + seedDatabase(); +} + // 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 @@ -26,9 +45,83 @@ app.use(express.json()); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + const endpoints = expressListEndpoints(app); + const info = endpoints.map((endpoint) => ({ + path: endpoint.path, + methods: endpoint.methods.join(", "), + })); + res.json(info); +}); + + +// SHOW ALL NOMINATIONS +app.get("/nominations", async (req, res) => { + const allNominations = await Globes.find(); + if (allNominations.length > 0) { + res.json(allNominations); + } else { + res.status(404).send("No nominations were found"); + } +}); + + +// SHOW ALL NOMINATIONS FOR SPECIFIC YEAR +app.get("/nominations/:awardYear", async (req, res) => { + const { awardYear } = req.params; + const yearOfAward = await Globes.find({ year_award: awardYear }).exec(); + + if (yearOfAward) { + res.json(yearOfAward); + } else { + res.status(404).send("This year's nominations were not found"); + } }); + +// SHOW ALL WINNERS +app.get("/nominations/wins", async (req, res) => { + const allWins = await Globes.find({ win: true }).exec(); + + if (allWins) { + res.json(allWins); + } else { + res.status(404).send("This year's wins were not found"); + } +}); + +// SHOW ALL WINNERS OF A SPECIFIC YEAR +app.get("/nominations/wins/:yearWon", async (req, res) => { + const { yearWon } = req.params; + const wonYear = await Globes.find({ + win: true, + year_award: yearWon, + }).exec(); + + if (wonYear) { + res.json(wonYear); + } else { + res.status(404).send("This year's nominations were not found"); + } +}); + + +// SHOW WINNER OF BEST DIRECTOR FOR SPECIFIC YEAR +app.get("/nominations/wins/:yearWon/best-director", async (req, res) => { + const { yearWon } = req.params; + const bestDirector = await Globes.find({ + win: true, + year_award: yearWon, + category: "Best Director - Motion Picture", + }).exec(); + + if (bestDirector) { + res.json(bestDirector); + } else { + res.status(404).send("This year's nominations were not found"); + } +}); + + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`);