ソースを参照

Move backend recipe deletion to recipeData.js. Update function to delete recipe from the database completely.

Lee Morgan 5 年 前
コミット
cddf62612c

+ 0 - 33
controllers/merchantData.js

@@ -204,39 +204,6 @@ module.exports = {
             });
     },
 
-    //DELETE - removes a single recipe from the merchant
-    removeRecipe: 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)=>{
-                if(merchant.pos === "clover"){
-                    return res.json("YOU MUST EDIT YOUR RECIPES INSIDE CLOVER");
-                }
-                
-                for(let i = 0; i < merchant.recipes.length; i++){
-                    if(merchant.recipes[i].toString() === req.params.id){
-                        merchant.recipes.splice(i, 1);
-                        break;
-                    }
-                }
-
-                merchant.save()
-                    .then((updatedMerchant)=>{
-                        return res.json({});
-                    })
-                    .catch((err)=>{
-                        return res.json("ERROR: UNABLE TO SAVE DATA");
-                    })
-            })
-            .catch((err)=>{
-                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
-            });
-    },
-
     //PUT - Update the default unit for a single ingredient
     ingredientDefaultUnit: function(req, res){
         if(!req.session.user){

+ 38 - 0
controllers/recipeData.js

@@ -105,6 +105,44 @@ module.exports = {
             });
     },
 
+    //DELETE - removes a single recipe from the merchant and the database
+    removeRecipe: 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)=>{
+                if(merchant.pos === "clover"){
+                    return res.json("YOU MUST EDIT YOUR RECIPES INSIDE CLOVER");
+                }
+                
+                for(let i = 0; i < merchant.recipes.length; i++){
+                    if(merchant.recipes[i].toString() === req.params.id){
+                        merchant.recipes.splice(i, 1);
+                        break;
+                    }
+                }
+
+                merchant.save()
+                    .then((updatedMerchant)=>{
+                        return res.json({});
+                    })
+                    .catch((err)=>{
+                        return res.json("ERROR: UNABLE TO SAVE DATA");
+                    })
+
+                return Recipe.deleteOne({_id: req.params.id});
+            })
+            .then((response)=>{
+                return res.json({});
+            })
+            .catch((err)=>{
+                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
+            });
+    },
+
     //GET - Checks clover for new or deleted recipes
     //Returns: 
     //  merchant: Full merchant (recipe ingredients populated)

+ 1 - 1
routes.js

@@ -18,7 +18,6 @@ module.exports = function(app){
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.get("/merchant/create/square", merchantData.createMerchantSquare);
-    app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
     app.put("/merchant/ingredients/update/:id/:unit", merchantData.ingredientDefaultUnit);
     app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient); //also updates some data in ingredients
     app.post("/merchant/password", merchantData.updatePassword);
@@ -32,6 +31,7 @@ module.exports = function(app){
     //Recipes
     app.post("/recipe/create", recipeData.createRecipe);
     app.put("/recipe/update", recipeData.updateRecipe);
+    app.delete("/recipe/remove/:id", recipeData.removeRecipe);
     app.get("/recipe/update/clover", recipeData.updateRecipesClover);
     app.get("/recipe/update/square", recipeData.updateRecipesSquare);
 

+ 2 - 2
views/dashboardPage/bundle.js

@@ -3514,8 +3514,8 @@ let recipeDetails = {
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
-        fetch(`/merchant/recipes/remove/${recipe.id}`, {
-            method: "DELETE"
+        fetch(`/recipe/remove/${recipe.id}`, {
+            method: "delete"
         })
             .then((response) => response.json())
             .then((response)=>{

+ 2 - 2
views/dashboardPage/js/recipeDetails.js

@@ -26,8 +26,8 @@ let recipeDetails = {
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
-        fetch(`/merchant/recipes/remove/${recipe.id}`, {
-            method: "DELETE"
+        fetch(`/recipe/remove/${recipe.id}`, {
+            method: "delete"
         })
             .then((response) => response.json())
             .then((response)=>{