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
57 changes: 43 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
const mongoose = require('mongoose');
const mongoose = require("mongoose");
const Recipe = require("./models/Recipe.model");
const data = require("./data");

// 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 MONGODB_URI = "mongodb://127.0.0.1:27017/recipe-App";

const MONGODB_URI = 'mongodb://localhost:27017/recipe-app';

// Connection to the database "recipe-app"
mongoose.set("strictQuery", false);
mongoose
.connect(MONGODB_URI)
.then(x => {
.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(() => {
console.log("Inserting recipes from data.json...");
return Recipe.insertMany(data);
})
.then(() => {
console.log('Updating duration for "Rigatoni alla Genovese"...');
return Recipe.findOneAndUpdate(
{ title: "Rigatoni alla Genovese" },
{ duration: 100 },
{ new: true }
);
})
.then((updatedRecipe) => {
console.log(`Successfully updated duration for "${updatedRecipe.title}"`);
// Find all recipes after updating the duration
//deleting the Carrot Cake recipe
return Recipe.deleteOne({ title: "Carrot Cake" });
})
//promise for deleted recipe
.then(() => {
console.log("Successfully removed Carrot Cake recipe");
return Recipe.find();
})
//logs of remaining recipes
.then((recipes) => {
console.log("Remaining recipes:");
recipes.forEach((recipe) => {
console.log(recipe.title);
});
return mongoose.connection.close();
})
.then(() => {
// Run your code here, after you have insured that the connection was made
console.log("Database connection closed.");
})
.catch(error => {
console.error('Error connecting to the database', error);
.catch((error) => {
console.error("Error:", error);
//db connection is closedin case of an error
mongoose.connection.close();
});
47 changes: 45 additions & 2 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
const mongoose = require('mongoose');
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.no,
},
});

const Recipe = mongoose.model('Recipe', recipeSchema);
const Recipe = mongoose.model("Recipe", recipeSchema);

module.exports = Recipe;
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lab-mongoose-recipes",
"version": "1.0.0",
"description": "",
"description": "<img src=\"https://imgur.com/XOS1Vdh.png\" width=\"150px\" height=\"150px\">",
"main": "index.js",
"scripts": {
"start": "node ./index.js",
Expand All @@ -12,6 +12,16 @@
"nodemon": "^2.0.2"
},
"dependencies": {
"mongoose": "^6.1.2"
}
"mongoose": "^6.13.5"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Hadiquaa/lab-mongoose-recipes.git"
},
"keywords": [],
"author": "",
"bugs": {
"url": "https://github.com/Hadiquaa/lab-mongoose-recipes/issues"
},
"homepage": "https://github.com/Hadiquaa/lab-mongoose-recipes#readme"
}