From 1f2f1074e618222c2c5caf113af9ffb76066c90f Mon Sep 17 00:00:00 2001 From: Gowtham Jangiti Date: Mon, 14 Oct 2024 19:29:58 +0530 Subject: [PATCH 1/2] Initial Setup --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index d92f163..a858e3a 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const data = require('./data'); const MONGODB_URI = 'mongodb://localhost:27017/recipe-app'; + // Connection to the database "recipe-app" mongoose .connect(MONGODB_URI) From 8b88b4ed9ee08c44efbe8a7f20be2c954072dfcc Mon Sep 17 00:00:00 2001 From: Gowtham Jangiti Date: Mon, 14 Oct 2024 19:33:25 +0530 Subject: [PATCH 2/2] Completed --- index.js | 37 +++++++++++++++++++++++++++++++------ models/Recipe.model.js | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index a858e3a..c2ffcf8 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,48 @@ const mongoose = require('mongoose'); - -// 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://localhost:27017/recipe-app'; - // Connection to the database "recipe-app" mongoose .connect(MONGODB_URI) .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(() => { + // Add new recipes after existing ones are deleted + return Recipe.insertMany(data); + }) + .then(() => { + // Find and display all recipes + return Recipe.find(); + }) + .then(recipes => { + recipes.forEach(r => { + console.log(r.title); + }); + + // Update a recipe + return Recipe.findOneAndUpdate( + { title: "Rigatoni alla Genovese" }, + { duration: 100 }, + { new: true } + ); + }) + .then(updatedRecipe => { + console.log('Updated recipe:', updatedRecipe); + + // Delete a recipe + return Recipe.deleteOne({ title: 'Carrot Cake' }); + }) + .then(() => { + console.log('Carrot Cake recipe has been successfully deleted!'); }) .then(() => { // Run your code here, after you have insured that the connection was made + return mongoose.connection.close(); }) .catch(error => { console.error('Error connecting to the database', error); diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..0dd009d 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -1,8 +1,42 @@ 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: { + 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.now, + }, }); const Recipe = mongoose.model('Recipe', recipeSchema);