diff --git a/index.js b/index.js index d92f163..60f2e2f 100644 --- a/index.js +++ b/index.js @@ -9,15 +9,35 @@ const MONGODB_URI = 'mongodb://localhost:27017/recipe-app'; // Connection to the database "recipe-app" mongoose - .connect(MONGODB_URI) + .connect("mongodb://localhost:27017/Recipie",{ + useNewUrlParser: true, + useUnifiedTopology: true, + }) .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); + return Recipe.create({ + title: 'Spaghetti Carbonara', + level: 'Easy Peasy', + ingredients: ['spaghetti', 'egg', 'cheese', 'bacon', 'pepper'], + cuisine: 'Italian', + dishType: 'main_course', + duration: 30, + creator: 'Chef Luigi', }); +}) +.then((recipe) => { + console.log(`Recipe created: ${recipe.title}`); +}) +.catch((error) => { + console.error('Error creating recipe:', error); +}) +.finally(() => { + mongoose.connection.close(); +}); + diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..4128562 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -3,6 +3,41 @@ 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);