From a0209cbe21fa4f7f84c774d3961ae3a4c863bb0d Mon Sep 17 00:00:00 2001 From: Muhammad Irshad <85778819+Irshadmdk19@users.noreply.github.com> Date: Fri, 27 Dec 2024 17:25:13 +0530 Subject: [PATCH] Completed Iterations --- .env | 1 + index.js | 44 ++++++++++++++++++++++++++++++++++++++---- models/Recipe.model.js | 21 ++++++++++++++++++-- package.json | 2 +- 4 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..53c69f9 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +const MONGODB_URI="+srv://irshadmdk99:irshamongo@cluster0.dapdax3.mongodb.net/" \ No newline at end of file diff --git a/index.js b/index.js index d92f163..b179d64 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const Recipe = require('./models/Recipe.model'); // Import of the data from './data.json' const data = require('./data'); -const MONGODB_URI = 'mongodb://localhost:27017/recipe-app'; +const MONGODB_URI = process.env.MONGODB_URI // Connection to the database "recipe-app" mongoose @@ -13,11 +13,47 @@ mongoose .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(() => { - // Run your code here, after you have insured that the connection was made + // Iteration 2 - Create a recipe + return Recipe.create({ + title: 'Pasta Carbonara', + level: 'Amateur Chef', + ingredients: ['Pasta', 'Eggs', 'Pancetta', 'Parmesan Cheese'], + cuisine: 'Italian', + dishType: 'main_course', + image: '', + duration: 30, + creator: 'Chef John' + }); + }) + .then(newRecipe => { + console.log(`Recipe created: ${newRecipe.title}`); + // Iteration 3 - Insert multiple recipes + return Recipe.insertMany(data); + }) + .then(allRecipes => { + allRecipes.forEach(recipe => console.log(`Inserted recipe: ${recipe.title}`)); + // Iteration 4 - Update a recipe + return Recipe.findOneAndUpdate( + { title: 'Rigatoni alla Genovese' }, + { duration: 100 }, + { new: true } + ); + }) + .then(updatedRecipe => { + console.log(`Updated recipe: ${updatedRecipe.title}, duration is now ${updatedRecipe.duration} minutes`); + // Iteration 5 - Remove a recipe + return Recipe.deleteOne({ title: 'Carrot Cake' }); + }) + .then(() => { + console.log('Successfully removed Carrot Cake'); + // Iteration 6 - Close the database + mongoose.connection.close(() => { + console.log('Database connection closed'); + }); }) .catch(error => { - console.error('Error connecting to the database', error); + console.error('Error performing operations on the database', error); }); diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..68a1d3f 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -1,8 +1,25 @@ const mongoose = require('mongoose'); -const Schema = mongoose.Schema; +const { Schema } = mongoose; 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: [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: String, + created: { type: Date, default: Date.now } }); const Recipe = mongoose.model('Recipe', recipeSchema); diff --git a/package.json b/package.json index 13acab9..04709d2 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,6 @@ "nodemon": "^2.0.2" }, "dependencies": { - "mongoose": "^6.1.2" + "mongoose": "^6.13.5" } }