From 26ca8d776d0641559ea4c780716c4d992ec92a71 Mon Sep 17 00:00:00 2001 From: b41r4m Date: Fri, 6 Sep 2024 22:13:35 +0530 Subject: [PATCH] Completed lab --- index.js | 59 ++++++++++++++++++++++++++++++++++++------ models/Recipe.model.js | 43 ++++++++++++++++++++++++++++-- package.json | 2 +- 3 files changed, 93 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index d92f163..6108f75 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,66 @@ -const mongoose = require('mongoose'); +const mongoose = require("mongoose"); // Import of the model Recipe from './models/Recipe.model.js' -const Recipe = require('./models/Recipe.model'); +const Recipe = require("./models/Recipe.model"); // Import of the data from './data.json' -const data = require('./data'); +const data = require("./data"); -const MONGODB_URI = 'mongodb://localhost:27017/recipe-app'; +const MONGODB_URI = "mongodb://127.0.0.1:27017/recipe-app"; // Connection to the database "recipe-app" 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(() => { // Run your code here, after you have insured that the connection was made + + // Iteration 2 - Create a recipe + // Recipe.create({ + // title: "Asian Glazed Chicken Thighs", + // level: "Amateur Chef", + // ingredients: [ + // "1/2 cup rice vinegar", + // "5 tablespoons honey", + // "1/3 cup soy sauce (such as Silver Swan®)", + // "1/4 cup Asian (toasted) sesame oil", + // "3 tablespoons Asian chili garlic sauce", + // "3 tablespoons minced garlic", + // "salt to taste", + // "8 skinless, boneless chicken thighs", + // ], + // cuisine: "Asian", + // dishType: "main_course", + // image: + // "https://images.media-allrecipes.com/userphotos/720x405/815964.jpg", + // duration: 40, + // creator: "Chef LePapu", + // }) + // .then((r) => console.log(r.title)) + // .then(() => mongoose.connection.close()); + + Recipe.insertMany(data) + .then((recipes) => { + recipes.forEach((recipe) => console.log(recipe.title)); + }) + .then(() => + Recipe.findOneAndUpdate( + { title: "Rigatoni alla Genovese" }, + { duration: 100 } + ).then((x) => + console.log(`Duration of ${x.title} was updated successfully.`) + ) + ) + .then(() => + Recipe.deleteOne({ title: "Carrot Cake" }).then(() => + console.log(`Recipe was removed successfully.`) + ) + ) + .then(() => mongoose.connection.close()); }) - .catch(error => { - console.error('Error connecting to the database', error); + .catch((error) => { + console.error("Error connecting to the database", error); }); diff --git a/models/Recipe.model.js b/models/Recipe.model.js index 7b2ad14..7927dae 100644 --- a/models/Recipe.model.js +++ b/models/Recipe.model.js @@ -1,10 +1,49 @@ -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: [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: String, + created: { + type: Date, + default: new Date(), + }, }); -const Recipe = mongoose.model('Recipe', recipeSchema); +const Recipe = mongoose.model("Recipe", recipeSchema); module.exports = Recipe; diff --git a/package.json b/package.json index 13acab9..891a573 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,6 @@ "nodemon": "^2.0.2" }, "dependencies": { - "mongoose": "^6.1.2" + "mongoose": "^6.13.0" } }