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
62 changes: 58 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,73 @@ 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';
const MONGODB_URI = 'mongodb://127.0.0.1:27017/recipeApp';

// 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()

// Remove all existing recipes
return Recipe.deleteMany();
})
.then(() => {
// Run your code here, after you have insured that the connection was made
console.log('Connected to MongoDB!');

// Create a new recipe
return Recipe.create({
title: "Spaghetti Carbonara",
level: "Amateur Chef",
ingredients: ["Spaghetti", "Eggs", "Pancetta", "Parmesan Cheese", "Pepper"],
cuisine: "Italian",
dishType: "main_course",
image: "https://example.com/carbonara.jpg",
duration: 20,
creator: "Chef Luigi"
});
})

//log the recipe with its title
.then((recipe) => {
console.log(`Recipe created: ${recipe.title}`);
})
.then(() => {
// Insert multiple recipes from data.json
return Recipe.insertMany(data);
})
//loop through the recipes and log them all.
.then((recipes) => {
recipes.forEach(recipe => {
console.log(`Inserted recipe: ${recipe.title}`);
});
recipes.forEach(recipe => {
console.log(`Inserted recipe: ${recipe.title}`);
});

// It4: Update duration recipe "Rigatoni alla Genovese"
return Recipe.findOneAndUpdate(
{ title: "Rigatoni alla Genovese" }, // find it
{ duration: 100 }, // Update duration
{ new: true } // Return updated document
);
})
.then((updatedRecipe) => {
console.log(`Updated recipe: ${updatedRecipe.title}, new duration: ${updatedRecipe.duration}`);

// Deleting "Carrot Cake"
return Recipe.deleteOne({ title: "Carrot Cake" });
})
.then(() => {
console.log("Successfully deleted the Carrot Cake recipe");
})

.catch(error => {
console.error('Error connecting to the database', error);
})
.finally(() => {
// Close the connection in the finally block to ensure it runs after all operations
mongoose.connection.close()
.then(() => console.log('Connection to MongoDB closed.'))
.catch(err => console.error('Error closing connection:', err));
});
33 changes: 31 additions & 2 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,37 @@ const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const recipeSchema = new Schema({
// TODO: write the schema
});
title: {
type: String,
required: true,
unique: true // title is unique within the collection
},
level: {
type: String,
enum: ['Easy Peasy', 'Amateur Chef', 'UltraPro Chef'], // Limits values to these only
},
ingredients: [String], // Array of strings for ingredients
cuisine: {
type: String,
required: true,
},
dishType: {
type: String,
enum: ['breakfast', 'main_course', 'soup', 'snack', 'drink', 'dessert', 'other'], // Limits to these specific dishtypes
},
image: {
type: String,
default: "https://images.media-allrecipes.com/images/75131.jpg" // Default image if nn provided
},
duration: {
type: Number,
min: 0 // minimum value
},
creator: String,
created: {
type: Date,
default: Date.now // Autosets the current date if not provided
}});

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"nodemon": "^2.0.2"
},
"dependencies": {
"mongoose": "^6.1.2"
"mongoose": "^6.13.3"
}
}