From 557745d343a8c4537a28b5018a81497ba9f6e9f0 Mon Sep 17 00:00:00 2001 From: Bharath Kompally Date: Thu, 13 Jun 2024 19:47:41 +0530 Subject: [PATCH] done --- .vscode/launch.json | 15 +++++++++++++ index.js | 50 ++++++++++++++++++++++++++++++++++++++---- models/Recipe.model.js | 32 +++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2ba986f --- /dev/null +++ b/.vscode/launch.json @@ -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}" + } + ] +} \ No newline at end of file diff --git a/index.js b/index.js index d92f163..4849710 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..572b5cf 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -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);