Skip to content
Open

done #33

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
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
50 changes: 46 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,60 @@ 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://localhost:27017';

// Connection to the database "recipe-app"
mongoose.set('strictQuery', true);
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()
Recipe.create({
title: "Spagetti",
level: "Easy Peasy",
ingredients: ["1/2 cup rice vinegar", "5 tablespoons honey"],
cuisine: "Italian",
dishType: "main_course",
duration: 1
})
.then(() => {
// Run your code here, after you have insured that the connection was made
Recipe.find({},{title:1, _id:0}).then((res)=>{console.log(res)})
})

Recipe.insertMany(data)
.then(()=>{
Recipe.find({},{title:1, _id:0}).then((res)=>{console.log(res)})
})
.then(()=>{
Recipe.findOneAndUpdate({ duration: "220" }, { duration: 100 })
.then((res)=>{
console.log('Duration updated successfully')
console.log(res)
})
.catch((err)=>{
console.log('Failed to update', err)
})

})
.then(()=>{
Recipe.deleteOne({title: "Carrot Cake"})
.then((res)=>{
console.log('Successfully Deleted Reciepe', res);
mongoose.connection.close()
.then(()=>{
console.log('DB Connection closed ✅')
})
.catch((err)=>{
console.log('Failed to close the DB connection !',err)
})

})
.catch((err)=>{
console.log('Failed to delete', err);
})
})


})
.catch(error => {
console.error('Error connecting to the database', error);
Expand Down
32 changes: 32 additions & 0 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@ const Schema = mongoose.Schema;

const recipeSchema = new Schema({
// TODO: write the schema
title:{
type: String,
unique: true,
required: true
},
level:{
type: String,
enum:['Easy Peasy', 'Amateur Chef', 'UltraPro Chef']
},
ingredients:[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: String,
min:0
},
creator:String,
created:{
type: String,
default: Date.now
}

});

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