Преглед изворни кода

Add ability to update recipes

Lee Morgan пре 6 година
родитељ
комит
37387045a8

+ 63 - 2
controllers/home.js

@@ -179,7 +179,7 @@ module.exports = {
                     if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
                         recipe.ingredients.splice(i, 1);
                         break;
-                    }
+                    }   
                 }
 
                 recipe.save()
@@ -189,7 +189,7 @@ module.exports = {
                     .catch((err)=>{
                         console.log(err);
                         return res.render("error");
-                    })
+                    });
             })
             .catch((err)=>{
                 console.log(err);
@@ -206,5 +206,66 @@ module.exports = {
                 console.log(err);
                 return res.render("error");
             });
+    },
+
+    updateRecipes: function(req, res){
+        Merchant.findOne({posId: merchantId})
+            .then((merchant)=>{
+                axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
+                    .then((result)=>{
+                        let deletedRecipes = merchant.recipes.slice();
+                        for(let i = 0; i < result.data.elements.length; i++){
+                            for(let j = 0; j < deletedRecipes.length; j++){
+                                if(result.data.elements[i].id === deletedRecipes[j].posId){
+                                    result.data.elements.splice(i, 1);
+                                    deletedRecipes.splice(j, 1);
+                                    i--;
+                                    break;
+                                }
+                            }
+                        }
+
+                        for(let recipe of deletedRecipes){
+                            for(let i = 0; i < merchant.recipes.length; i++){
+                                if(recipe._id === merchant.recipes[i]._id){
+                                    merchant.recipes.splice(i, 1);
+                                    break;
+                                }
+                            }
+                        }
+
+                        for(let recipe of result.data.elements){
+                            merchant.recipes.push({
+                                posId: recipe.id,
+                                name: recipe.name,
+                                ingredients: []
+                            });
+                        }
+                        
+                        merchant.save()
+                            .then((newMerchant)=>{
+                                newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
+                                    .then((newestMerchant)=>{
+                                        return res.json({merchant: newestMerchant, count: result.data.elements.length});
+                                    })
+                                    .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");
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
     }
 }

+ 1 - 1
models/ingredient.js

@@ -14,6 +14,6 @@ const IngredientSchema = new mongoose.Schema({
         type: String,
         required: [true, "You must provide the measurement unit for this item"]
     }
-})
+});
 
 module.exports = mongoose.model("Ingredient", IngredientSchema);

+ 1 - 0
routes.js

@@ -10,4 +10,5 @@ module.exports = function(app){
     app.post("/ingredients/createone", home.createIngredient);
     app.get("/recipes", home.displayRecipes);
     app.post("/recipes/ingredients/remove", home.deleteRecipeIngredient);
+    app.get("/recipes/update", home.updateRecipes);
 }

+ 0 - 1
views/merchantSetupPage/recipeSetup.js

@@ -123,7 +123,6 @@ let recipeSetup = {
             
             this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
             if(!validator.ingredient.quantity(newIngredient.quantity)){
-                
                 isValid = false;
                 break;
             }

+ 2 - 0
views/recipesPage/recipes.ejs

@@ -11,6 +11,8 @@
 
         <h1 id="title">Recipes</h1>
 
+        <button id="recipeUpdate" onclick="recipesPage.updateRecipes()">Update Recipes</button>
+
         <div id="recipes" class="container"></div>
 
         <div id="ingredient" class="container">

+ 17 - 1
views/recipesPage/recipes.js

@@ -82,6 +82,7 @@ let recipesPage = {
         
         axios.post("/merchant/update", merchant)
             .then((result)=>{
+                merchant = result;
                 banner.createNotification("Ingredient has been removed from recipe");
             })
             .catch((err)=>{
@@ -90,7 +91,6 @@ let recipesPage = {
             });
     },
 
-
     //Change quantity field to input
     //Change edit button
     editIngredient: function(row, ingredient){
@@ -132,6 +132,22 @@ let recipesPage = {
                 console.log(err);
                 banner.createError("There was an error and the ingredient could not be updated");
             });
+    },
+
+    updateRecipes: function(){
+        axios.get("/recipes/update")
+            .then((result)=>{
+                merchant = result.data.merchant;
+                this.displayRecipes();
+                banner.createNotification("Your recipes have been updated successfully");
+                if(result.data.count > 0){
+                    banner.createError(`You have ${result.data.count} recipes with no ingredients.  Please update them.`);
+                }
+            })
+            .catch((err)=>{
+                console.log(err);
+                banner.createError("There was an error and your recipes could not be updated");
+            });
     }
 }