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
45 changes: 40 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,47 @@ 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()
return Recipe.deleteMany();
})
.then(() => {
// Run your code here, after you have insured that the connection was made
const newRecipe = {
title: "Asian Glazed Chicken Thighs",
level: "Amateur Chef",
ingredients: [
// ingredients here
],
cuisine: "Asian",
dishType: "main_course",
image: "https://images.media-allrecipes.com/userphotos/720x405/815964.jpg",
duration: 40,
creator: "Chef LePapu"
};

return Recipe.create(newRecipe);
})
.then(result => {
console.log(`Object inserted - ${result.title}`);
return Recipe.insertMany(data);
})
.then(results => {
results.forEach(recipe => {
console.log(`Inserted - ${recipe.title}`);
});
return Recipe.findOneAndUpdate(
{ title: 'Rigatoni alla Genovese' },
{ duration: 100 },
{ new: true }
);
})
.then(updatedRecipe => {
console.log(`Updated ${updatedRecipe.title} successfully!`);
return Recipe.deleteOne({ title: 'Carrot Cake' });
})
.then(() => {
console.log('Carrot Cake removed successfully!');
mongoose.connection.close();
})
.catch(error => {
console.error('Error connecting to the database', error);
});
console.error('Error:', error);
mongoose.connection.close();
});
18 changes: 16 additions & 2 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ 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;
module.exports = Recipe;