Просмотр исходного кода

New recipe ingredient can be added. Not immediately displaying.

Lee Morgan 6 лет назад
Родитель
Сommit
d90a1ae1d6
4 измененных файлов с 82 добавлено и 2 удалено
  1. 24 2
      controllers/home.js
  2. 1 0
      routes.js
  3. 2 0
      views/recipesPage/recipes.ejs
  4. 55 0
      views/recipesPage/recipes.js

+ 24 - 2
controllers/home.js

@@ -174,13 +174,13 @@ module.exports = {
             .catch((err)=>{
                 console.log(err);
                 return res.render("error");
-            });
-            
+            });  
     },
 
     displayRecipes: function(req, res){
         Merchant.findOne({posId: merchantId})
             .populate("recipes.ingredients.ingredient")
+            .populate("inventory.ingredient")
             .then((merchant)=>{
                 return res.render("recipesPage/recipes", {merchant: merchant});
             })
@@ -305,5 +305,27 @@ module.exports = {
                 console.log(err);
                 return res.render("error");
             });
+    },
+
+    addRecipeIngredient: function(req, res){
+        console.log(req.body.recipeId);
+        Merchant.findOne({_id: req.body.merchantId})
+            .then((merchant)=>{
+                let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
+                recipe.ingredients.push(req.body.item)
+                console.log(recipe);
+                merchant.save()
+                    .then((newMerchant)=>{
+                        return res.json(newMerchant);
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
     }
 }

+ 1 - 0
routes.js

@@ -13,4 +13,5 @@ module.exports = function(app){
     app.get("/recipes/update", home.updateRecipes);
     app.get("/ingredients", home.getIngredients);
     app.post("/merchant/ingredients/create", home.addMerchantIngredient);
+    app.post("/merchant/recipes/ingredients/create", home.addRecipeIngredient);
 }

+ 2 - 0
views/recipesPage/recipes.ejs

@@ -16,6 +16,8 @@
         <div id="recipes" class="container"></div>
 
         <div id="ingredient" class="container">
+            <button id="addButton">Add Ingredient</button>
+
             <table>
                 <thead>
                     <tr>

+ 55 - 0
views/recipesPage/recipes.js

@@ -38,6 +38,8 @@ let recipesPage = {
         let tbody = document.querySelector("tbody");
         let title = document.querySelector("#title");
         let recipeUpdate = document.querySelector("#recipeUpdate");
+
+        document.querySelector("#addButton").onclick = ()=>{this.displayAdd(recipe)};
         title.innerText = recipe.name;
 
         recipesDiv.style.display = "none";
@@ -72,6 +74,59 @@ let recipesPage = {
         }
     },
 
+    displayAdd: function(recipe){
+        let tbody = document.querySelector("tbody");
+
+        let row = document.createElement("tr");
+        tbody.appendChild(row);
+
+        let nameTd = document.createElement("td");
+        row.appendChild(nameTd);
+        let name = document.createElement("select");
+        nameTd.appendChild(name);
+
+        for(let item of merchant.inventory){
+            let nameOption = document.createElement("option");
+            nameOption.innerText = item.ingredient.name;
+            nameOption.value = item.ingredient._id;
+            name.appendChild(nameOption);
+        }
+
+        let quantityTd = document.createElement("td");
+        row.appendChild(quantityTd);
+        let quantity = document.createElement("input");
+        quantity.type = "text";
+        quantity.step = "0.01";
+        quantityTd.appendChild(quantity);
+
+        let actionTd = document.createElement("td");
+        row.appendChild(actionTd);
+
+        let saveButton = document.createElement("button");
+        saveButton.innerText = "Save";
+        saveButton.onclick = ()=>{console.log(name);};
+        actionTd.appendChild(saveButton);
+        saveButton.onclick = ()=>{this.addIngredient(recipe, name.value, quantity.value);};
+    },
+
+    addIngredient: function(recipe, ingredientId, quantity){
+        let item = {
+            ingredient: ingredientId,
+            quantity: quantity
+        };
+        
+        axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item, merchantId: merchant._id})
+            .then((newMerchant)=>{
+                merchant = newMerchant;
+                this.displayOneRecipe(recipe);
+                banner.createNotification("Ingredient successfully added to recipe");
+            })
+            .catch((err)=>{
+                console.log(err);
+                banner.createError("There was an error and the recipe could not be updated");
+            })
+    },
+
     //Delete ingredient from table
     //Delete ingredient from database
     deleteIngredient: function(recipeId, ingredientId, row){