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

Update removeRecipe to work with new session id.

Lee Morgan 5 лет назад
Родитель
Сommit
aaba75f95e
2 измененных файлов с 12 добавлено и 25 удалено
  1. 10 23
      controllers/recipeData.js
  2. 2 2
      routes.js

+ 10 - 23
controllers/recipeData.js

@@ -92,31 +92,18 @@ 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("/");
+        if(res.locals.merchant.pos === "clover"){
+            return res.json("YOU MUST EDIT YOUR RECIPES INSIDE CLOVER");
+        }
+        
+        for(let i = 0; i < res.locals.merchant.recipes.length; i++){
+            if(res.locals.merchant.recipes[i].toString() === req.params.id){
+                res.locals.merchant.recipes.splice(i, 1);
+                break;
+            }
         }
 
-        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()
-                    .catch((err)=>{
-                        return res.json("ERROR: UNABLE TO SAVE DATA");
-                    })
-
-                return Recipe.deleteOne({_id: req.params.id});
-            })
+        Promise.all([Recipe.deleteOne({_id: req.params.id}), res.locals.merchant.save()])
             .then((response)=>{
                 return res.json({});
             })

+ 2 - 2
routes.js

@@ -24,7 +24,7 @@ module.exports = function(app){
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.get("/merchant/create/square", merchantData.createMerchantSquare);
     app.put("/merchant/ingredients/update", session, merchantData.updateMerchantIngredient); //also updates some data in ingredients
-    app.post("/merchant/password", merchantData.updatePassword);
+    app.post("/merchant/password", merchantData.updatePassword); //TODO: change to work with session
 
     //Ingredients
     app.post("/ingredients/create", session, ingredientData.createIngredient);  //also adds to merchant
@@ -36,7 +36,7 @@ module.exports = function(app){
     //Recipes
     app.post("/recipe/create", session, recipeData.createRecipe);
     app.put("/recipe/update", session, recipeData.updateRecipe);
-    app.delete("/recipe/remove/:id", recipeData.removeRecipe);
+    app.delete("/recipe/remove/:id", session, recipeData.removeRecipe);
     app.get("/recipe/update/clover", recipeData.updateRecipesClover);
     app.get("/recipe/update/square", recipeData.updateRecipesSquare);
     app.post("/recipes/create/spreadsheet", upload.single("recipes"), recipeData.createFromSpreadsheet);