浏览代码

Remove ingredient from database when removed from merchant

Lee Morgan 5 年之前
父节点
当前提交
40d4fc99d4
共有 5 个文件被更改,包括 36 次插入37 次删除
  1. 30 1
      controllers/ingredientData.js
  2. 0 29
      controllers/merchantData.js
  3. 1 1
      routes.js
  4. 4 5
      views/dashboardPage/bundle.js
  5. 1 1
      views/dashboardPage/js/ingredientDetails.js

+ 30 - 1
controllers/ingredientData.js

@@ -123,5 +123,34 @@ module.exports = {
             .catch((err)=>{
             .catch((err)=>{
                 return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
                 return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
             });
             });
-    }
+    },
+
+    //POST - Removes an ingredient from the merchant's inventory
+    removeIngredient: function(req, res){
+        if(!req.session.user){
+            req.session.error = "MUST BE LOGGED IN TO DO THAT";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                for(let i = 0; i < merchant.inventory.length; i++){
+                    if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
+                        merchant.inventory.splice(i, 1);
+                        break;
+                    }
+                }
+
+                return merchant.save()
+            })
+            .then((merchant)=>{
+                return Ingredient.deleteOne({_id: req.params.id});
+            })
+            .then((ingredient)=>{
+                return res.json({});
+            })
+            .catch((err)=>{
+                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
+            });
+    },
 }
 }

+ 0 - 29
controllers/merchantData.js

@@ -237,35 +237,6 @@ module.exports = {
             });
             });
     },
     },
 
 
-    //POST - Removes an ingredient from the merchant's inventory
-    removeMerchantIngredient: function(req, res){
-        if(!req.session.user){
-            req.session.error = "MUST BE LOGGED IN TO DO THAT";
-            return res.redirect("/");
-        }
-
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                for(let i = 0; i < merchant.inventory.length; i++){
-                    if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
-                        merchant.inventory.splice(i, 1);
-                        break;
-                    }
-                }
-
-                merchant.save()
-                    .then((merchant)=>{
-                        return res.json({});
-                    })
-                    .catch((err)=>{
-                        return res.json("ERROR: UNABLE TO SAVE USER DATA");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
-            });
-    },
-
     //PUT - Update the default unit for a single ingredient
     //PUT - Update the default unit for a single ingredient
     ingredientDefaultUnit: function(req, res){
     ingredientDefaultUnit: function(req, res){
         if(!req.session.user){
         if(!req.session.user){

+ 1 - 1
routes.js

@@ -19,7 +19,6 @@ module.exports = function(app){
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.get("/merchant/create/square", merchantData.createMerchantSquare);
     app.get("/merchant/create/square", merchantData.createMerchantSquare);
     app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
     app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
-    app.delete("/merchant/ingredients/remove/:id", merchantData.removeMerchantIngredient);
     app.put("/merchant/ingredients/update/:id/:unit", merchantData.ingredientDefaultUnit);
     app.put("/merchant/ingredients/update/:id/:unit", merchantData.ingredientDefaultUnit);
     app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient); //also updates some data in ingredients
     app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient); //also updates some data in ingredients
     app.post("/merchant/password", merchantData.updatePassword);
     app.post("/merchant/password", merchantData.updatePassword);
@@ -28,6 +27,7 @@ module.exports = function(app){
     app.get("/ingredients", ingredientData.getIngredients);
     app.get("/ingredients", ingredientData.getIngredients);
     app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
     app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
     app.put("/ingredients/update", ingredientData.updateIngredient);
     app.put("/ingredients/update", ingredientData.updateIngredient);
+    app.delete("/ingredients/remove/:id", ingredientData.removeIngredient);
 
 
     //Recipes
     //Recipes
     app.post("/recipe/create", recipeData.createRecipe);
     app.post("/recipe/create", recipeData.createRecipe);

+ 4 - 5
views/dashboardPage/bundle.js

@@ -109,11 +109,10 @@ class Merchant{
     /*
     /*
     Updates all specified item in the merchant's inventory and updates the page
     Updates all specified item in the merchant's inventory and updates the page
     If ingredient doesn't exist, add it
     If ingredient doesn't exist, add it
-    ingredients = {
+    ingredients = [{
         ingredient: Ingredient object,
         ingredient: Ingredient object,
         quantity: new quantity,
         quantity: new quantity,
-        defaultUnit: the default unit to be displayed
-    }
+    }]
     remove = set true if removing
     remove = set true if removing
     isOrder = set true if this is coming from an order
     isOrder = set true if this is coming from an order
     */
     */
@@ -122,7 +121,7 @@ class Merchant{
             let isNew = true;
             let isNew = true;
             for(let j = 0; j < this.ingredients.length; j++){
             for(let j = 0; j < this.ingredients.length; j++){
                 if(this.ingredients[j].ingredient === ingredients[i].ingredient){
                 if(this.ingredients[j].ingredient === ingredients[i].ingredient){
-                    if(remove){
+                    if(remove && !isOrder){
                         this.ingredients.splice(j, 1);
                         this.ingredients.splice(j, 1);
                     }else if(!remove && isOrder){
                     }else if(!remove && isOrder){
                         this.ingredients[j].quantity += ingredients[i].quantity;
                         this.ingredients[j].quantity += ingredients[i].quantity;
@@ -1573,7 +1572,7 @@ let ingredientDetails = {
         let loader = document.getElementById("loaderContainer");
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
         loader.style.display = "flex";
 
 
-        fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
+        fetch(`/ingredients/remove/${this.ingredient.ingredient.id}`, {
             method: "DELETE",
             method: "DELETE",
         })
         })
             .then((response) => response.json())
             .then((response) => response.json())

+ 1 - 1
views/dashboardPage/js/ingredientDetails.js

@@ -119,7 +119,7 @@ let ingredientDetails = {
         let loader = document.getElementById("loaderContainer");
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
         loader.style.display = "flex";
 
 
-        fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
+        fetch(`/ingredients/remove/${this.ingredient.ingredient.id}`, {
             method: "DELETE",
             method: "DELETE",
         })
         })
             .then((response) => response.json())
             .then((response) => response.json())