Procházet zdrojové kódy

Seperate recipes into seperate model. Fix updateRecipeIngredientsFunction.

Lee Morgan před 6 roky
rodič
revize
3ab2bb0a7f
3 změnil soubory, kde provedl 79 přidání a 45 odebrání
  1. 47 25
      controllers/home.js
  2. 6 20
      models/merchant.js
  3. 26 0
      models/recipe.js

+ 47 - 25
controllers/home.js

@@ -2,6 +2,7 @@ const axios = require("axios");
 
 const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
+const Recipe = require("../models/recipe");
 
 const homeHelper = require("./homeHelper");
 
@@ -13,9 +14,9 @@ const token = "b48068eb-411a-918e-ea64-52007147e42c";
 
 module.exports = {
     displayInventory: function(req, res){
-
         Merchant.findOne({posId: merchantId})
             .populate("inventory.ingredient")
+            .populate("recipes")
             .then((merchant)=>{
                 if(merchant){
                     req.session.user = merchant._id;
@@ -85,7 +86,14 @@ module.exports = {
         }
 
         Merchant.findOne({_id: req.session.user})
-            .populate("recipes.ingredients.ingredient")
+            .populate({
+                path: "recipes",
+                model: "Recipe",
+                populate: {
+                    path: "ingredients.ingredient",
+                    model: "Ingredient"
+                }
+            })
             .populate("inventory.ingredient")
             .then((merchant)=>{
                 return res.render("recipesPage/recipes", {merchant: merchant});
@@ -182,19 +190,37 @@ module.exports = {
                     newMerchant.inventory.push(newIngredient);
                 }
 
+                let newRecipes = []
                 for(let recipe of data.recipes){
-                    newMerchant.recipes.push(recipe);
+                    let newRecipe = {
+                        posId: recipe.posId,
+                        name: recipe.name,
+                        ingredients: []
+                    };
+                    for(let ingredient of recipe.ingredients){
+                        newRecipe.ingredients.push(ingredient);
+                    }
+                    newRecipes.push(newRecipe);
                 }
 
-                newMerchant.save()
-                    .then((newMerchant)=>{
-                        req.session.user = newMerchant._id;
-                        return res.redirect("/");
+                Recipe.create(newRecipes)
+                    .then((recipes)=>{
+                        for(let recipe of recipes){
+                            newMerchant.recipes.push(recipe);
+                        }
+                        newMerchant.save()
+                                .then((merchant)=>{
+                                    return res.redirect("/");
+                                })
+                                .catch((err)=>{
+                                    console.log(err);
+                                    return res.render("error");
+                                });
                     })
                     .catch((err)=>{
                         console.log(err);
                         return res.render("error");
-                    })
+                    });
             })
             .catch((err)=>{
                 console.log(err);
@@ -306,25 +332,21 @@ module.exports = {
             return res.render("error");
         }
 
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
-
-                for(let i = 0; i < recipe.ingredients.length; i++){
-                    if(recipe.ingredients[i]._id.toString() === req.body.ingredient._id){
-                        recipe.ingredients[i].quantity = req.body.ingredient.quantity;
-                        break;
+        Recipe.findOne({_id: req.body.recipeId})
+            .then((recipe)=>{
+                for(let ingredient of recipe.ingredients){
+                    if(ingredient._id.toString() === req.body.ingredient._id){
+                        ingredient.quantity = req.body.ingredient.quantity;
+                        recipe.save()
+                            .then((recipe)=>{
+                                return res.json();
+                            })
+                            .catch((err)=>{
+                                console.log(err);
+                                return res.render("error");
+                            })
                     }
                 }
-
-                merchant.save()
-                    .then(()=>{
-                        return res.json();
-                    })
-                    .catch((err)=>{
-                        console.log(err);
-                        return res.render("error");
-                    });
             })
             .catch((err)=>{
                 console.log(err);

+ 6 - 20
models/merchant.js

@@ -13,6 +13,10 @@ const MerchantSchema = new mongoose.Schema({
         type: String,
         default: Date.now()
     },
+    createdAt: {
+        type: Date,
+        default: Date.now
+    },
     inventory: [{
         ingredient: {
             type: mongoose.Schema.Types.ObjectId,
@@ -26,26 +30,8 @@ const MerchantSchema = new mongoose.Schema({
         }
     }],
     recipes: [{
-        posId: {
-            type: String,
-            required: true
-        },
-        name: {
-            type: String,
-            required: true,
-        },
-        ingredients: [{
-            ingredient: {
-                type: mongoose.Schema.Types.ObjectId,
-                ref: "Ingredient",
-                required: [true, "Must provide ingredient"]
-            },
-            quantity: {
-                type: Number,
-                min: [0, "Cannot have a negative quantity"],
-                required: [true, "Must provide a quantity"]
-            }
-        }]
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Recipe",
     }]
 });
 

+ 26 - 0
models/recipe.js

@@ -0,0 +1,26 @@
+const mongoose = require("mongoose");
+
+const RecipeSchema = new mongoose.Schema({
+    posId: {
+        type: String,
+        required: true
+    },
+    name: {
+        type: String,
+        required: true,
+    },
+    ingredients: [{
+        ingredient: {
+            type: mongoose.Schema.Types.ObjectId,
+            ref: "Ingredient",
+            required: [true, "Must provide ingredient"]
+        },
+        quantity: {
+            type: Number,
+            min: [0, "Cannot have a negative quantity"],
+            required: [true, "Must provide a quantity"]
+        }
+    }]
+});
+
+module.exports = mongoose.model("Recipe", RecipeSchema);