Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kanak-internship #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
78 changes: 59 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,63 @@
const mongoose = require('mongoose');
// index.js

// Import of the model Recipe from './models/Recipe.model.js'
const mongoose = require('mongoose');
const Recipe = require('./models/Recipe.model');
// Import of the data from './data.json'
const data = require('./data');
const recipes = require('./data.json'); // Import recipes from data.json


mongoose.connect('mongodb://localhost:27017/recipeDB', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Database connected');

const newRecipe = {
title: 'Spaghetti Bolognese',
level: 'Amateur Chef',
ingredients: ['spaghetti', 'ground beef', 'tomato sauce', 'onion', 'garlic'],
cuisine: 'Italian',
dishType: 'main_course',
image: 'https://example.com/spaghetti.jpg',
duration: 45,
creator: 'Chef John'
};

// Create the new recipe
Recipe.create(newRecipe)
.then(recipe => {
console.log(`Recipe created: ${recipe.title}`);
mongoose.connection.close();
// Iteration 3: Insert multiple recipes
Recipe.insertMany(recipes)
.then(insertedRecipes => {
insertedRecipes.forEach(recipe => {
console.log(`Recipe created: ${recipe.title}`);
});
mongoose.connection.close();

const MONGODB_URI = 'mongodb://localhost:27017/recipe-app';
// Iteration 4: Update a recipe
Recipe.findOneAndUpdate(
{ title: 'Rigatoni alla Genovese' },
{ duration: 100 },
{ new: true }
)
.then(updatedRecipe => {
console.log(`Updated Recipe: ${updatedRecipe.title}`);
mongoose.connection.close();

// 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);
});
// Iteration 5: Remove a recipe
Recipe.deleteOne({ title: 'Carrot Cake' })
.then(() => {
console.log('Carrot Cake has been removed from the database');
mongoose.connection.close();
})
.catch(err => console.log(err));
})
.catch(err => console.log(err));
})
.catch(err => console.log(err));
})
.catch(err => console.log(err));
})
.catch(err => console.log('Database connection error:', err));
46 changes: 43 additions & 3 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
// models/Recipe.model.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const recipeSchema = new Schema({
// TODO: write the schema
const recipeSchema = new mongoose.Schema({
title: {
type: String,
required: true,
unique: true
},
level: {
type: String,
enum: ['Easy Peasy', 'Amateur Chef', 'UltraPro Chef'],
required: true
},
ingredients: {
type: [String],
required: true
},
cuisine: {
type: String,
required: true
},
dishType: {
type: String,
enum: ['breakfast', 'main_course', 'soup', 'snack', 'drink', 'dessert', 'other'],
required: true
},
image: {
type: String,
default: 'https://images.media-allrecipes.com/images/75131.jpg'
},
duration: {
type: Number,
min: 0,
required: true
},
creator: {
type: String
},
created: {
type: Date,
default: Date.now
}
});

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

module.exports = Recipe;

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"
"lab-mongoose-recipes": "file:",
"mongoose": "^6.13.5"
}
}