diff --git a/README.md b/README.md index 35019cd8a..37bf3f448 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ # Project Mongo API -Replace this readme with your own information about your project. +Create my own database with Mongo DB. -Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. ## The problem -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? +This project was built on last week's assignment. So, I started by copying code from the previous project to use as a boilerplate. This one was quite challenging. For ages, I struggled with importing data from JSON to MongoDB. I ended up installing an older version of MongoDB - and it worked. ## 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://mongo-nominations.onrender.com/nominations/123 \ No newline at end of file diff --git a/package.json b/package.json index 6830a48aa..24886c021 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.0", "mongoose": "^8.0.0", "nodemon": "^3.0.1" } diff --git a/server.js b/server.js index 647e7b144..7b94f7804 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,39 @@ 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 expressListEndpoints from "express-list-endpoints"; +// this the import from json file. +import goldenGlobesData from "./data/golden-globes.json"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; +// copied from the instructions and change the name from person to nomination +const Nomination = mongoose.model("Nomination", { + // this is the schema that tells the data base what kind of data we are expecting. like year-film, category and so on. + year_film: Number, + year_award: Number, + ceremony: Number, + category: String, + nominee: String, + film: String, + win: Boolean, +}); + +// this put the data from json into mongo db. +if (process.env.RESET_DB) { + const seedDatabase = async () => { + await Nomination.deleteMany({}); + + goldenGlobesData.forEach((nominationData) => { + new Nomination(nominationData).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,7 +46,56 @@ app.use(express.json()); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + res.send(expressListEndpoints(app)); +}); + +// copied this code from the express api projcet +// this is route one, where i get the full data about the nominations. +app.get("/nominations", (req, res) => { + // this where i get the data from mongo db and send it as json ... + Nomination.find().then((results) => { + res.json(results); + }); +}); + +// copied this code from the express api projcet +// async needs to be there because we are using await. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await +// here we are replacing the json file. im now getting the data from mongo db. +// this is routw two where we get a specifc nomination. +app.get("/nominations/:id", async (req, res) => { + const id = req.params.id; // if /nominations/1, req.params.id is 1 + const nomination = await Nomination.find().skip(Number(id)).limit(1).exec(); + // if we find something with that id, we return it. if not we return a 404. + if (nomination) { + res.json(nomination); + } else { + // remmber 404 means "not found". if it start with a 2 evertyhing is ok. if it starts with a 4 or 5 something is wrong. 4 means your request is wrong. 5 means the server is wrong. + res.status(404).send(`No nomination with id ${id} found.`); + } +}); + +app.post("/nominations", (req, res) => { + // https://stackoverflow.com/questions/67557955/what-is-the-purpose-of-req-body-in-express-js + // here we create the route where we can post new nominations. + // https://blog.hubspot.com/website/curl-command + // wrote promPT to chat gpt: generate me a curl command that sends a post request to http://localhost:8080/nominations containing a new nomination matching the above schema. it returned this command: + // curl -X POST \ + // -H "Content-Type: application/json" \ + // -d '{ + // "year_film": 2023, + // "year_award": 2024, + // "ceremony": 1, + // "category": "Best Actor", + // "nominee": "John Doe", + // "film": "The Great Movie", + // "win": true + // }' \ + // http://localhost:8080/nominations + + const newNomination = new Nomination(req.body); + newNomination.save().then(() => { + res.json(newNomination); + }); }); // Start the server