Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 64 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
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://localhost:27017/recipe-app';
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
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();

} catch (error) {
console.error('Error: ', error);
}
}

manageRecipes()

/*
// Connection to the database "recipe-app"
mongoose
.connect(MONGODB_URI)
Expand All @@ -17,7 +69,17 @@ mongoose
})
.then(() => {
// Run your code here, after you have insured that the connection was made
Recipe.insertMany(data)
.then(recipes => {
console.log('Recipes created')
recipes.forEach(recipe => {
console.log(recipe.title)
})
mongoose.connection.close()
})
.catch(error => console.log(`Error adding recipe: ${error}`));
})
.catch(error => {
console.error('Error connecting to the database', error);
});
*/
38 changes: 36 additions & 2 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"nodemon": "^2.0.2"
},
"dependencies": {
"mongodb": "^6.14.2",
"mongoose": "^6.1.2"
}
}