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

Frida #487

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Frida #487

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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
111 changes: 102 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`);
Expand Down