From 2a23b55f82c07bc0fcbe4143ef9fe15a3ef2f75e Mon Sep 17 00:00:00 2001 From: Kanak sharma Date: Mon, 16 Dec 2024 14:35:39 +0530 Subject: [PATCH] Done --- index.js | 78 ++++++++++++++++++++++++++++++++---------- models/Recipe.model.js | 46 +++++++++++++++++++++++-- package.json | 3 +- 3 files changed, 104 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index d92f163..3b863db 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,63 @@ -const mongoose = require('mongoose'); +// index.js -// Import of the model Recipe from './models/Recipe.model.js' +const mongoose = require('mongoose'); const Recipe = require('./models/Recipe.model'); -// Import of the data from './data.json' -const data = require('./data'); +const recipes = require('./data.json'); // Import recipes from data.json + + +mongoose.connect('mongodb://localhost:27017/recipeDB', { + useNewUrlParser: true, + useUnifiedTopology: true +}) +.then(() => { + console.log('Database connected'); + + const newRecipe = { + title: 'Spaghetti Bolognese', + level: 'Amateur Chef', + ingredients: ['spaghetti', 'ground beef', 'tomato sauce', 'onion', 'garlic'], + cuisine: 'Italian', + dishType: 'main_course', + image: 'https://example.com/spaghetti.jpg', + duration: 45, + creator: 'Chef John' + }; + + // Create the new recipe + Recipe.create(newRecipe) + .then(recipe => { + console.log(`Recipe created: ${recipe.title}`); + mongoose.connection.close(); + // Iteration 3: Insert multiple recipes + Recipe.insertMany(recipes) + .then(insertedRecipes => { + insertedRecipes.forEach(recipe => { + console.log(`Recipe created: ${recipe.title}`); + }); + mongoose.connection.close(); -const MONGODB_URI = 'mongodb://localhost:27017/recipe-app'; + // Iteration 4: Update a recipe + Recipe.findOneAndUpdate( + { title: 'Rigatoni alla Genovese' }, + { duration: 100 }, + { new: true } + ) + .then(updatedRecipe => { + console.log(`Updated Recipe: ${updatedRecipe.title}`); + mongoose.connection.close(); -// 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() - }) - .then(() => { - // Run your code here, after you have insured that the connection was made - }) - .catch(error => { - console.error('Error connecting to the database', error); - }); + // Iteration 5: Remove a recipe + Recipe.deleteOne({ title: 'Carrot Cake' }) + .then(() => { + console.log('Carrot Cake has been removed from the database'); + mongoose.connection.close(); + }) + .catch(err => console.log(err)); + }) + .catch(err => console.log(err)); + }) + .catch(err => console.log(err)); + }) + .catch(err => console.log(err)); +}) +.catch(err => console.log('Database connection error:', err)); diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..f8e6103 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -1,10 +1,50 @@ +// models/Recipe.model.js + const mongoose = require('mongoose'); -const Schema = mongoose.Schema; -const recipeSchema = new Schema({ - // TODO: write the schema +const recipeSchema = new mongoose.Schema({ + title: { + type: String, + required: true, + unique: true + }, + level: { + type: String, + enum: ['Easy Peasy', 'Amateur Chef', 'UltraPro Chef'], + required: true + }, + ingredients: { + type: [String], + required: true + }, + cuisine: { + type: String, + required: true + }, + dishType: { + type: String, + enum: ['breakfast', 'main_course', 'soup', 'snack', 'drink', 'dessert', 'other'], + required: true + }, + image: { + type: String, + default: 'https://images.media-allrecipes.com/images/75131.jpg' + }, + duration: { + type: Number, + min: 0, + required: true + }, + creator: { + type: String + }, + created: { + type: Date, + default: Date.now + } }); const Recipe = mongoose.model('Recipe', recipeSchema); module.exports = Recipe; + diff --git a/package.json b/package.json index 13acab9..0b1c799 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "nodemon": "^2.0.2" }, "dependencies": { - "mongoose": "^6.1.2" + "lab-mongoose-recipes": "file:", + "mongoose": "^6.13.5" } }