From 8704e6124b8adf62531c7870d66b9f0ef254f9c4 Mon Sep 17 00:00:00 2001 From: Yerko Arce Date: Fri, 14 Mar 2025 22:40:16 -0300 Subject: [PATCH 1/4] Iteration 1 completed, Model of Recipe --- models/Recipe.model.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..1698fa4 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 ], + default: [] + }, + 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); From c45112d11c75d67d38027cdc28d509adb69e6ca1 Mon Sep 17 00:00:00 2001 From: Yerko Arce Date: Fri, 14 Mar 2025 23:38:07 -0300 Subject: [PATCH 2/4] Iteration 2 completed --- index.js | 18 +++++++++++++++++- package.json | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d92f163..19e0925 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,9 @@ 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 = 'mongodb+srv://yerkoarcegalaz:Brd7tkNL2GATAMxX@openbootcamp.8xxrw.mongodb.net/?retryWrites=true&w=majority&appName=openbootcamp' + + // Connection to the database "recipe-app" mongoose @@ -17,6 +19,20 @@ mongoose }) .then(() => { // Run your code here, after you have insured that the connection was made + Recipe.create({ + title: 'Primera receta', + level: 'Amateur Chef', + ingredients: ['algo', 'algo mas', 'Otra cosa mas'], + cuisine: 'Cocina', + dishType: 'breakfast', + duration: 5, + creator: 'Yerko Arce' + }) + .then(recipe => { + console.log('Recipe created', recipe.title) + mongoose.connection.close() + }) + .catch(error => console.log(`Error creating recipe: ${error}`)); }) .catch(error => { console.error('Error connecting to the database', error); diff --git a/package.json b/package.json index 13acab9..d397f78 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "nodemon": "^2.0.2" }, "dependencies": { + "mongodb": "^6.14.2", "mongoose": "^6.1.2" } } From a6c6a3a7de06640d8b5bb735f8b356e75f3ed6c8 Mon Sep 17 00:00:00 2001 From: Yerko Arce Date: Sat, 15 Mar 2025 09:53:36 -0300 Subject: [PATCH 3/4] Iteration 4 completed, update recipe --- index.js | 62 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 19e0925..f77c5b9 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,54 @@ -const mongoose = require('mongoose'); +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 { ReturnDocument } = require('mongodb'); const MONGODB_URI = 'mongodb+srv://yerkoarcegalaz:Brd7tkNL2GATAMxX@openbootcamp.8xxrw.mongodb.net/?retryWrites=true&w=majority&appName=openbootcamp' +async function manageRecipes() { + try { + // Connection to the database "recipe-app" + await mongoose.connect(MONGODB_URI); + console.log(`Connected to the database: "${mongoose.connection.name}"`); + // Before adding any recipes to the database, let's remove all existing ones + await Recipe.deleteMany(); + + // Insert new recipes + const recipes = await Recipe.insertMany(data) + console.log('Recipes created!!') + recipes.forEach(recipe => { + console.log(recipe.title) + }) + + // Update recipe duration + await Recipe.findOneAndUpdate({ title: 'Rigatoni alla Genovese' }, { duration: 100 }, {new: true}) + .then(updateRecipe => { + if (updateRecipe) { + console.log('Recipe update: ', updateRecipe) + } else { + console.log("Don't find recipe") + } + }) + .catch(error => { + console.error('Error to update recipe: ', error) + }) + + // Disconnect from the database + await mongoose.disconnect(); + + } catch (error) { + console.error('Error: ', error); + } +} + +manageRecipes() + +/* // Connection to the database "recipe-app" mongoose .connect(MONGODB_URI) @@ -19,21 +59,17 @@ mongoose }) .then(() => { // Run your code here, after you have insured that the connection was made - Recipe.create({ - title: 'Primera receta', - level: 'Amateur Chef', - ingredients: ['algo', 'algo mas', 'Otra cosa mas'], - cuisine: 'Cocina', - dishType: 'breakfast', - duration: 5, - creator: 'Yerko Arce' - }) - .then(recipe => { - console.log('Recipe created', recipe.title) + Recipe.insertMany(data) + .then(recipes => { + console.log('Recipes created') + recipes.forEach(recipe => { + console.log(recipe.title) + }) mongoose.connection.close() }) - .catch(error => console.log(`Error creating recipe: ${error}`)); + .catch(error => console.log(`Error adding recipe: ${error}`)); }) .catch(error => { console.error('Error connecting to the database', error); }); +*/ \ No newline at end of file From ac0e8205964aafef3ee9a768e951a3e02233482e Mon Sep 17 00:00:00 2001 From: Yerko Arce Date: Sat, 15 Mar 2025 10:32:28 -0300 Subject: [PATCH 4/4] Complete the lab, The application was refactored to use async await --- index.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index f77c5b9..5dd00fc 100644 --- a/index.js +++ b/index.js @@ -26,17 +26,27 @@ async function manageRecipes() { }) // Update recipe duration - await Recipe.findOneAndUpdate({ title: 'Rigatoni alla Genovese' }, { duration: 100 }, {new: true}) - .then(updateRecipe => { - if (updateRecipe) { - console.log('Recipe update: ', updateRecipe) - } else { - console.log("Don't find recipe") - } - }) - .catch(error => { - console.error('Error to update recipe: ', error) - }) + const updatedRecipe = await Recipe.findOneAndUpdate( + { title: 'Rigatoni alla Genovese' }, + { duration: 100 }, + { new: true } + ); + + if (updatedRecipe) { + console.log('Recipe updated:', updatedRecipe); + } else { + console.log("Recipe not found"); + } + + + // Delete one recipe + const result = await Recipe.deleteOne({ title: 'Carrot Cake' }); + + if (result.deletedCount > 0) { + console.log('Recipe removed!'); + } else { + console.log("Recipe not found."); + } // Disconnect from the database await mongoose.disconnect();