From 9baebd9bc8b22afee0fa0bbd22b331da5e56b3b5 Mon Sep 17 00:00:00 2001 From: maaz Date: Fri, 6 Dec 2024 22:01:41 -0600 Subject: [PATCH] Completed Lab --- index.js | 57 +++++++++++++++++++++++++++++++----------- models/Recipe.model.js | 47 ++++++++++++++++++++++++++++++++-- package.json | 16 +++++++++--- 3 files changed, 101 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index d92f163..2daa61a 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,52 @@ -const mongoose = require('mongoose'); +const mongoose = require("mongoose"); +const Recipe = require("./models/Recipe.model"); +const data = require("./data"); -// Import of the model Recipe from './models/Recipe.model.js' -const Recipe = require('./models/Recipe.model'); -// Import of the data from './data.json' -const data = require('./data'); +const MONGODB_URI = "mongodb://127.0.0.1:27017/recipe-App"; -const MONGODB_URI = 'mongodb://localhost:27017/recipe-app'; - -// Connection to the database "recipe-app" +mongoose.set("strictQuery", false); mongoose .connect(MONGODB_URI) - .then(x => { + .then((x) => { console.log(`Connected to the database: "${x.connection.name}"`); - // Before adding any recipes to the database, let's remove all existing ones - return Recipe.deleteMany() + return Recipe.deleteMany(); + }) + .then(() => { + console.log("Inserting recipes from data.json..."); + return Recipe.insertMany(data); + }) + .then(() => { + console.log('Updating duration for "Rigatoni alla Genovese"...'); + return Recipe.findOneAndUpdate( + { title: "Rigatoni alla Genovese" }, + { duration: 100 }, + { new: true } + ); + }) + .then((updatedRecipe) => { + console.log(`Successfully updated duration for "${updatedRecipe.title}"`); + // Find all recipes after updating the duration + //deleting the Carrot Cake recipe + return Recipe.deleteOne({ title: "Carrot Cake" }); + }) + //promise for deleted recipe + .then(() => { + console.log("Successfully removed Carrot Cake recipe"); + return Recipe.find(); + }) + //logs of remaining recipes + .then((recipes) => { + console.log("Remaining recipes:"); + recipes.forEach((recipe) => { + console.log(recipe.title); + }); + return mongoose.connection.close(); }) .then(() => { - // Run your code here, after you have insured that the connection was made + console.log("Database connection closed."); }) - .catch(error => { - console.error('Error connecting to the database', error); + .catch((error) => { + console.error("Error:", error); + //db connection is closedin case of an error + mongoose.connection.close(); }); diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..d253c2f 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -1,10 +1,53 @@ -const mongoose = require('mongoose'); +const mongoose = require("mongoose"); const Schema = mongoose.Schema; const recipeSchema = new Schema({ // TODO: write the schema + title: { + type: String, + required: true, + unique: true, + }, + level: { + type: String, + enum: ["Easy Peasy", "Amateur Chef", "UltraPro Chef"], + }, + ingredients: { + type: [String], + }, + cuisine: { + type: String, + required: true, + }, + dishType: { + type: String, + enum: [ + "breakfast", + "main_course", + "soup", + "snack", + "drink", + "dessert", + "other ", + ], + }, + image: { + type: String, + default: "https://images.media-allrecipes.com/images/75131.jpg", + }, + duration: { + type: Number, + min: 0, + }, + creator: { + type: String, + }, + created: { + type: Date, + default: Date.no, + }, }); -const Recipe = mongoose.model('Recipe', recipeSchema); +const Recipe = mongoose.model("Recipe", recipeSchema); module.exports = Recipe; diff --git a/package.json b/package.json index 13acab9..7460890 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "lab-mongoose-recipes", "version": "1.0.0", - "description": "", + "description": "", "main": "index.js", "scripts": { "start": "node ./index.js", @@ -12,6 +12,16 @@ "nodemon": "^2.0.2" }, "dependencies": { - "mongoose": "^6.1.2" - } + "mongoose": "^6.13.5" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Hadiquaa/lab-mongoose-recipes.git" + }, + "keywords": [], + "author": "", + "bugs": { + "url": "https://github.com/Hadiquaa/lab-mongoose-recipes/issues" + }, + "homepage": "https://github.com/Hadiquaa/lab-mongoose-recipes#readme" }