From f536faddde4365ddeed10f8ed44ea66b16359cec Mon Sep 17 00:00:00 2001 From: IanNgacha Date: Thu, 8 Aug 2024 14:55:51 +0300 Subject: [PATCH] Completed lab --- index.js | 55 ++++++++++++++++++++++++++++++------------ models/Recipe.model.js | 37 ++++++++++++++++++++++++++-- package.json | 2 +- 3 files changed, 76 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index d92f163..3769a7f 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 recipes = require('./data.json'); -const MONGODB_URI = 'mongodb://localhost:27017/recipe-app'; +// Connection to the database +mongoose.connect('mongodb://localhost:27017/recipe-app', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => { + console.log('Connected to the database'); -// 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() + // Insert multiple recipes from data.json + return Recipe.insertMany(recipes); }) - .then(() => { - // Run your code here, after you have insured that the connection was made + .then(insertedRecipes => { + // Log the title of each inserted recipe + insertedRecipes.forEach(recipe => { + console.log('Recipe created:', recipe.title); + }); + + // Update the duration of "Rigatoni alla Genovese" + return Recipe.findOneAndUpdate( + { title: 'Rigatoni alla Genovese' }, + { duration: 100 }, + { new: true } + ); + }) + .then(updatedRecipe => { + if (updatedRecipe) { + console.log(`Success! The duration of "${updatedRecipe.title}" has been updated to ${updatedRecipe.duration} minutes.`); + } else { + console.log('Recipe not found.'); + } + + // Delete the "Carrot Cake" recipe + return Recipe.deleteOne({ title: 'Carrot Cake' }); + }) + .then(deletionInfo => { + if (deletionInfo.deletedCount > 0) { + console.log('Success! The "Carrot Cake" recipe has been removed from the database.'); + } else { + console.log('The "Carrot Cake" recipe was not found in the database.'); + } + + // Disconnect from the database after all operations + return mongoose.connection.close(); }) .catch(error => { - console.error('Error connecting to the database', error); + console.error('Error during database operations:', error); }); diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..bec29cf 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -2,9 +2,42 @@ 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; diff --git a/package.json b/package.json index 13acab9..891a573 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,6 @@ "nodemon": "^2.0.2" }, "dependencies": { - "mongoose": "^6.1.2" + "mongoose": "^6.13.0" } }