From 08b176db8a80f38f43e1afb9f2b4c631ae419e1b Mon Sep 17 00:00:00 2001 From: premkumardev909 Date: Fri, 29 Nov 2024 13:04:17 +0000 Subject: [PATCH] done --- index.js | 45 +++++++++++++++++++++++++++++++++++++----- models/Recipe.model.js | 18 +++++++++++++++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d92f163..469a390 100644 --- a/index.js +++ b/index.js @@ -12,12 +12,47 @@ 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(() => { - // Run your code here, after you have insured that the connection was made + const newRecipe = { + title: "Asian Glazed Chicken Thighs", + level: "Amateur Chef", + ingredients: [ + // ingredients here + ], + cuisine: "Asian", + dishType: "main_course", + image: "https://images.media-allrecipes.com/userphotos/720x405/815964.jpg", + duration: 40, + creator: "Chef LePapu" + }; + + return Recipe.create(newRecipe); + }) + .then(result => { + console.log(`Object inserted - ${result.title}`); + return Recipe.insertMany(data); + }) + .then(results => { + results.forEach(recipe => { + console.log(`Inserted - ${recipe.title}`); + }); + return Recipe.findOneAndUpdate( + { title: 'Rigatoni alla Genovese' }, + { duration: 100 }, + { new: true } + ); + }) + .then(updatedRecipe => { + console.log(`Updated ${updatedRecipe.title} successfully!`); + return Recipe.deleteOne({ title: 'Carrot Cake' }); + }) + .then(() => { + console.log('Carrot Cake removed successfully!'); + mongoose.connection.close(); }) .catch(error => { - console.error('Error connecting to the database', error); - }); + console.error('Error:', error); + mongoose.connection.close(); +}); \ No newline at end of file diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..d426fe2 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -2,9 +2,23 @@ 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.now }, }); const Recipe = mongoose.model('Recipe', recipeSchema); -module.exports = Recipe; +module.exports = Recipe; \ No newline at end of file