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

Refactor and reorder routes and homeController

Lee Morgan 6 лет назад
Родитель
Сommit
94390f052c
3 измененных файлов с 125 добавлено и 134 удалено
  1. 119 130
      controllers/home.js
  2. 1 1
      routes.js
  3. 5 3
      views/recipesPage/recipes.js

+ 119 - 130
controllers/home.js

@@ -76,108 +76,6 @@ module.exports = {
             })
     },
 
-    updateMerchant: function(req, res){
-        Merchant.updateOne({_id: req.session.user}, req.body)
-            .then((merchant)=>{
-                return res.json(merchant);
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    getCloverRecipes: function(req, res){
-        axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
-            .then((recipes)=>{
-                return res.json(recipes);
-            })
-            .catch((err)=>{
-                return res.json(err);
-            });
-    },
-
-    createMerchant: function(req, res){
-        let data = JSON.parse(req.body.data);
-
-        axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
-            .then((merchant)=>{
-                let newMerchant = new Merchant({
-                    name: merchant.data.name,
-                    posId: merchant.data.id,
-                    lastUpdatedTime: Date.now(),
-                    inventory: [],
-                    recipes: []
-                });
-
-                for(let ingredient of data.ingredients){
-                    let newIngredient = {
-                        ingredient: ingredient.id,
-                        quantity: parseInt(ingredient.quantity)
-                    }
-                    newMerchant.inventory.push(newIngredient);
-                }
-
-                for(let recipe of data.recipes){
-                    newMerchant.recipes.push(recipe);
-                }
-
-                newMerchant.save()
-                    .then((newMerchant)=>{
-                        return res.redirect("/");
-                    })
-                    .catch((err)=>{
-                        console.log(err);
-                        return res.render("error");
-                    })
-            })
-            .catch((err)=>{
-                console.log(err);
-            });
-    },
-
-    createNewIngredients: function(req, res){
-        Ingredient.create(req.body)
-            .then((ingredients)=>{
-                return res.json(ingredients);
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    createIngredient: function(req, res){
-        Ingredient.create(req.body.ingredient)
-            .then((ingredient)=>{
-                Merchant.findOne({_id: req.session.user})
-                    .then((merchant)=>{
-                        let item = {
-                            ingredient: ingredient,
-                            quantity: req.body.quantity
-                        }
-                        merchant.inventory.push(item);
-                        merchant.save()
-                            .then((merchant)=>{
-                                console.log("something");
-                                return res.json(merchant);
-                            })
-                            .catch((err)=>{
-                                console.log(err);
-                                return res.render("error");
-                            });
-                    })
-                    .catch((err)=>{
-                        console.log(err);
-                        return res.render("error");
-                    });
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });  
-    },
-
     displayRecipes: function(req, res){
         Merchant.findOne({_id: req.session.user})
             .populate("recipes.ingredients.ingredient")
@@ -252,14 +150,42 @@ module.exports = {
             });
     },
 
-    getIngredients: function(req, res){
-        Ingredient.find()
-            .then((ingredients)=>{
-                return res.json(ingredients);
+    createMerchant: function(req, res){
+        let data = JSON.parse(req.body.data);
+
+        axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
+            .then((merchant)=>{
+                let newMerchant = new Merchant({
+                    name: merchant.data.name,
+                    posId: merchant.data.id,
+                    lastUpdatedTime: Date.now(),
+                    inventory: [],
+                    recipes: []
+                });
+
+                for(let ingredient of data.ingredients){
+                    let newIngredient = {
+                        ingredient: ingredient.id,
+                        quantity: parseInt(ingredient.quantity)
+                    }
+                    newMerchant.inventory.push(newIngredient);
+                }
+
+                for(let recipe of data.recipes){
+                    newMerchant.recipes.push(recipe);
+                }
+
+                newMerchant.save()
+                    .then((newMerchant)=>{
+                        return res.redirect("/");
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    })
             })
             .catch((err)=>{
                 console.log(err);
-                return res.render("error");
             });
     },
 
@@ -282,6 +208,31 @@ module.exports = {
             });
     },
 
+    removeMerchantIngredient: function(req, res){
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                for(let i = 0; i < merchant.inventory.length; i++){
+                    if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
+                        merchant.inventory.splice(i, 1);
+                        break;
+                    }
+                }
+
+                merchant.save()
+                    .then(()=>{
+                        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)=>{
@@ -302,19 +253,20 @@ module.exports = {
             });
     },
 
-    removeRecipeIngredient: function(req, res){
+    updateRecipeIngredient: function(req, res){
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
                 let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
+
                 for(let i = 0; i < recipe.ingredients.length; i++){
-                    if(req.body.ingredientId === recipe.ingredients[i]._id.toString()){
-                        recipe.ingredients.splice(i, 1);
+                    if(recipe.ingredients[i]._id.toString() === req.body.ingredient._id){
+                        recipe.ingredients[i].quantity = req.body.ingredient.quantity;
                         break;
                     }
                 }
 
                 merchant.save()
-                    .then((result)=>{
+                    .then(()=>{
                         return res.json();
                     })
                     .catch((err)=>{
@@ -328,18 +280,19 @@ module.exports = {
             });
     },
 
-    removeMerchantIngredient: function(req, res){
+    removeRecipeIngredient: function(req, res){
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
-                for(let i = 0; i < merchant.inventory.length; i++){
-                    if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
-                        merchant.inventory.splice(i, 1);
+                let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
+                for(let i = 0; i < recipe.ingredients.length; i++){
+                    if(req.body.ingredientId === recipe.ingredients[i]._id.toString()){
+                        recipe.ingredients.splice(i, 1);
                         break;
                     }
                 }
 
                 merchant.save()
-                    .then(()=>{
+                    .then((result)=>{
                         return res.json();
                     })
                     .catch((err)=>{
@@ -353,21 +306,47 @@ module.exports = {
             });
     },
 
-    updateRecipeIngredient: function(req, res){
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
-                
-                for(let i = 0; i < recipe.ingredients.length; i++){
-                    if(recipe.ingredients[i]._id.toString() === req.body.ingredient._id){
-                        recipe.ingredients[i].quantity = req.body.ingredient.quantity;
-                        break;
-                    }
-                }
+    getIngredients: function(req, res){
+        Ingredient.find()
+            .then((ingredients)=>{
+                return res.json(ingredients);
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
 
-                merchant.save()
-                    .then(()=>{
-                        return res.json();
+    createNewIngredients: function(req, res){
+        Ingredient.create(req.body)
+            .then((ingredients)=>{
+                return res.json(ingredients);
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
+
+    createIngredient: function(req, res){
+        Ingredient.create(req.body.ingredient)
+            .then((ingredient)=>{
+                Merchant.findOne({_id: req.session.user})
+                    .then((merchant)=>{
+                        let item = {
+                            ingredient: ingredient,
+                            quantity: req.body.quantity
+                        }
+                        merchant.inventory.push(item);
+                        merchant.save()
+                            .then((merchant)=>{
+                                console.log("something");
+                                return res.json(merchant);
+                            })
+                            .catch((err)=>{
+                                console.log(err);
+                                return res.render("error");
+                            });
                     })
                     .catch((err)=>{
                         console.log(err);
@@ -377,6 +356,16 @@ module.exports = {
             .catch((err)=>{
                 console.log(err);
                 return res.render("error");
+            });  
+    },
+
+    getCloverRecipes: function(req, res){
+        axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
+            .then((recipes)=>{
+                return res.json(recipes);
+            })
+            .catch((err)=>{
+                return res.json(err);
             });
     }
 }

+ 1 - 1
routes.js

@@ -7,7 +7,7 @@ module.exports = function(app){
     app.get("/recipes", home.displayRecipes);
 
     //Merchant
-    app.get("/merchant/recipes/update", home.updateRecipes); //this is fucked
+    app.get("/merchant/recipes/update", home.updateRecipes);
     app.post("/merchant/create", home.createMerchant);
     app.post("/merchant/ingredients/create", home.addMerchantIngredient);
     app.post("/merchant/ingredients/remove", home.removeMerchantIngredient);

+ 5 - 3
views/recipesPage/recipes.js

@@ -209,23 +209,25 @@ let recipesPage = {
     },
 
     updateIngredient: function(row, ingredient){
+        let originalQuantity = ingredient.quantity;
         ingredient.quantity = row.children[1].children[0].value;
+
         let td = row.children[1];
         while(td.children.length > 0){
             td.removeChild(td.firstChild);
         }
-        td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
 
         let button = row.children[2].children[0];
         button.innerText = "Edit";
         button.onclick = ()=>{this.editIngredient(row, ingredient);};
-        console.log(this.currentRecipe._id);
 
         axios.post("/merchant/recipes/ingredients/update", {recipeId: this.currentRecipe._id, ingredient: ingredient})
-            .then((recipe)=>{
+            .then(()=>{
+                td.innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
                 banner.createNotification("Ingredient successfully updated");
             })
             .catch((err)=>{
+                td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
                 console.log(err);
                 banner.createError("There was an error and the ingredient could not be updated");
             });