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
50 changes: 34 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,37 @@ 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';

// 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);
});
const MONGODB_URI = 'mongodb://127.0.0.1:27017/recipe-app';

// Function to connect to the MongoDB database
async function main() {
try {
// Connect to the database
await mongoose.connect(MONGODB_URI);
console.log(`Connected to the database: "${mongoose.connection.name}"`);

// Iteration 3 - Insert multiple recipes
await Recipe.insertMany(data);
console.log('Added the following recipes to the database:');
data.forEach(recipe => {
console.log(recipe.title);
});

// Iteration 4 - Update recipe
await Recipe.findOneAndUpdate({ title: 'Rigatoni alla Genovese' }, { duration: 100 });
console.log('Updated duration for Rigatoni alla Genovese to 100 minutes.');

// Iteration 5 - Remove a recipe
await Recipe.deleteOne({ title: 'Carrot Cake' });
console.log('Removed Carrot Cake from the database.');

// Iteration 6 - Close the database
await mongoose.connection.close();
console.log('Database connection closed.');
} catch (error) {
console.error('Error:', error);
}
}

// Call the main function to execute the tasks
main();
41 changes: 41 additions & 0 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,47 @@ 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);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"nodemon": "^2.0.2"
},
"dependencies": {
"mongoose": "^6.1.2"
"mongodb": "^6.3.0",
"mongoose": "^6.12.6"
}
}