Parcourir la source

Fix ingredients not updating

Lee Morgan il y a 6 ans
Parent
commit
5614db0430
4 fichiers modifiés avec 23 ajouts et 4 suppressions
  1. 20 0
      controllers/home.js
  2. 1 0
      routes.js
  3. 0 2
      views/inventory/inventory.ejs
  4. 2 2
      views/inventory/inventory.js

+ 20 - 0
controllers/home.js

@@ -233,6 +233,26 @@ module.exports = {
             });
     },
 
+    updateMerchantIngredient: function(req, res){
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
+                updateIngredient.quantity = req.body.quantity;
+                merchant.save()
+                    .then((merchant)=>{
+                        return res.json();
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    })
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            })
+    },
+
     addRecipeIngredient: function(req, res){
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{

+ 1 - 0
routes.js

@@ -11,6 +11,7 @@ module.exports = function(app){
     app.post("/merchant/create", home.createMerchant);
     app.post("/merchant/ingredients/create", home.addMerchantIngredient);
     app.post("/merchant/ingredients/remove", home.removeMerchantIngredient);
+    app.post("/merchant/ingredients/update", home.updateMerchantIngredient);
     app.post("/merchant/recipes/ingredients/create", home.addRecipeIngredient);
     app.post("/merchant/recipes/ingredients/update", home.updateRecipeIngredient);
     app.post("/merchant/recipes/ingredients/remove", home.removeRecipeIngredient);

+ 0 - 2
views/inventory/inventory.ejs

@@ -30,8 +30,6 @@
                     <tbody></tbody>
                 </thead>
             </table>
-
-            
         </div>
 
         <div class="add-ingredient">

+ 2 - 2
views/inventory/inventory.js

@@ -1,6 +1,6 @@
 let inventoryPage = {
     items: [], //the ingredients to be displayed
-    currentSort: "",
+    currentSort: "", //boolean used for sorting
 
     //Remove any existing ingredients in table
     //loop through this.items and create rows for the table
@@ -110,7 +110,7 @@ let inventoryPage = {
         if(validator.ingredient.quantity(quantity)){
             let updateIngredient = merchant.inventory.find(i => i._id === id);
             updateIngredient.quantity = quantity;
-            axios.post("merchant/update", merchant)
+            axios.post("/merchant/ingredients/update", {ingredientId: id, quantity: quantity})
                 .then((merchant)=>{
                     banner.createNotification("The ingredient has been successfully updated");
                 })