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

Log changes for removing a recipe ingredient

Lee Morgan 6 лет назад
Родитель
Сommit
307f573ae8
2 измененных файлов с 25 добавлено и 9 удалено
  1. 21 4
      controllers/merchantData.js
  2. 4 5
      views/recipesPage/singleRecipe.js

+ 21 - 4
controllers/merchantData.js

@@ -572,7 +572,7 @@ module.exports = {
                         recipe.save()
                             .then((recipe)=>{
                                 res.json({});
-                                
+
                                 let rc = new RecipeChange({
                                     recipe: recipe,
                                     ingredient: ingredient.ingredient,
@@ -591,7 +591,6 @@ module.exports = {
                                 return;
                             })
                             .catch((err)=>{
-                                console.log(err);
                                 let errorMessage = "There was an error and the recipe could not be updated";
                                 let error = new Error({
                                     code: 547,
@@ -606,7 +605,6 @@ module.exports = {
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 let errorMessage = "There was an error and the recipe could not be updated";
                 let error = new Error({
                     code: 626,
@@ -623,6 +621,7 @@ module.exports = {
     //Inputs:
     //  req.body.ingredientId: Id of ingredient to be removed
     //  req.body.recipeId: Id of recipe to remove ingredient from
+    //  req.body.quantity: quantity of recipe ingredient for storing
     //Returns: Nothing
     removeRecipeIngredient: function(req, res){
         if(!req.session.user){
@@ -635,12 +634,30 @@ module.exports = {
                 for(let i = 0; i < recipe.ingredients.length; i++){
                     if(recipe.ingredients[i].ingredient._id.toString() === req.body.ingredientId){
                         recipe.ingredients.splice(i, 1);
+                        break;
                     }
                 }
 
                 recipe.save()
                     .then((recipe)=>{
-                        return res.json({});
+                        res.json({});
+
+                        let rc = new RecipeChange({
+                            recipe: req.body.recipeId,
+                            ingredient: req.body.ingredientId,
+                            change: -req.body.quantity
+                        });
+                        rc.save()
+                            .catch((err)=>{
+                                let error = new Error({
+                                    code: 120,
+                                    displayMessage: "none",
+                                    error: err
+                                });
+                                error.save();
+                            });
+
+                        return;
                     })
                     .catch((err)=>{
                         let errorMessage = "There was an error and the ingredient could not be remove from the recipe";

+ 4 - 5
views/recipesPage/singleRecipe.js

@@ -37,7 +37,7 @@ let singleRecipeObj = {
             let removeButton = document.createElement("button");
             removeButton.innerText = "Remove";
             removeButton.classList = "button-small";
-            removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient.ingredient._id, row);};
+            removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredient.ingredient._id, ingredient.quantity, row);};
             actions.appendChild(removeButton);
         }
     },
@@ -144,7 +144,7 @@ let singleRecipeObj = {
                         let removeButton = document.createElement("button");
                         removeButton.innerText = "Remove";
                         removeButton.classList = "button-small";
-                        removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, row);};
+                        removeButton.onclick = ()=>{this.deleteIngredient(recipe._id, ingredientId, quantity, row);};
                         actions.appendChild(removeButton);
                     }
                 })
@@ -157,8 +157,8 @@ let singleRecipeObj = {
 
     //Delete ingredient from table
     //Delete ingredient from database
-    deleteIngredient: function(recipeId, ingredientId, row){
-        axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId})
+    deleteIngredient: function(recipeId, ingredientId, quantity, row){
+        axios.post("/merchant/recipes/ingredients/remove", {ingredientId: ingredientId, recipeId: recipeId, quantity: quantity})
             .then((result)=>{
                 if(typeof(result.data) === "string"){
                     banner.createError(result.data);
@@ -252,7 +252,6 @@ let singleRecipeObj = {
                     }
                 })
                 .catch((err)=>{
-                    console.log(err);
                     td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
                     banner.createError("There was an error and the ingredient could not be updated");
                 });