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
55 changes: 40 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
});
37 changes: 35 additions & 2 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"nodemon": "^2.0.2"
},
"dependencies": {
"mongoose": "^6.1.2"
"mongoose": "^6.13.0"
}
}