Browse Source

Complete creation process for new ingredients

Lee Morgan 6 years ago
parent
commit
f3891f6990

+ 31 - 29
controllers/ingredientData.js

@@ -15,43 +15,45 @@ module.exports = {
             });
     },
 
-    //TODO - Redirect to merchantData.js rather than adding here
-    //POST - create a single ingredient and then add to the merchant
-    //Inputs: 
-    //  req.body.ingredient: full ingredient to create (name, category, unit)
-    //  req.body.quantity: quantity of ingredient for merchant
-    //Returns:
-    //  item: ingredient and quantity
+    /*
+    POST - create a single ingredient and then add to the merchant
+    req.body = {
+        ingredient: {
+            name: name of ingredient,
+            category: category of ingredient,
+            unit: unit measurement of ingredient
+        },
+        quantity: quantity of ingredient for current merchant
+    }
+    Returns:
+        Same as above, with the _id
+    */
     createIngredient: function(req, res){
         if(!req.session.user){
             req.session.error = "Must be logged in to do that";
             return res.redirect("/");
         }
-        
-        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)=>{
-                                return res.json(item);
-                            })
-                            .catch((err)=>{
-                                return res.json("Error: ingredient could not be saved");
-                            });
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: could not retrieve user data");
-                    });
+        let ingredientPromise = Ingredient.create((req.body.ingredient));
+        let merchantPromise = Merchant.findOne({_id: req.session.user});
+        let newIngredient;
+
+        Promise.all([ingredientPromise, merchantPromise])
+            .then((response)=>{
+                newIngredient = {
+                    ingredient: response[0],
+                    quantity: req.body.quantity
+                }
+
+                response[1].inventory.push(newIngredient);
+
+                return response[1].save();
+            })
+            .then((response)=>{
+                return res.json(newIngredient);
             })
             .catch((err)=>{
-                return res.json("Error: could not create new ingredient");
+                return res.json("Error: unable to create new ingredient");
             });
     }
 }

+ 1 - 1
routes.js

@@ -29,7 +29,7 @@ module.exports = function(app){
 
     //Ingredients
     app.get("/ingredients", ingredientData.getIngredients);
-    // app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
+    app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
 
     //Recipes
     app.post("/recipe/create", recipeData.createRecipe);

+ 1 - 1
views/dashboardPage/components/addIngredients.ejs

@@ -133,7 +133,7 @@
                     let ingredient = this.addIngredientsDiv[i];
 
                     if(ingredient.children[0].checked){
-                        if(!validator.ingredient.quantity(ingredient.children[2].value)){
+                        if(!validator.ingredientQuantity(ingredient.children[2].value)){
                             return;
                         }
 

+ 19 - 1
views/dashboardPage/components/components.js

@@ -272,7 +272,25 @@ let newIngredientComp = {
         }
 
         if(validator.ingredient(newIngredient)){
-            console.log("all good");
+            fetch("/ingredients/create", {
+                method: "POST",
+                headers: {
+                    "Content-Type": "application/json;charset=utf-8"
+                },
+                body: JSON.stringify(newIngredient)
+            })
+                .then((response) => response.json())
+                .then((response)=>{
+                    if(typeof(response) === "string"){
+                        banner.createError(response);
+                    }else{
+                        updateInventory([response]);
+                        banner.createNotification("Ingredient successfully created");
+                    }
+                })
+                .catch((err)=>{
+                    banner.createError("Something went wrong.  Try refreshing the page");
+                });
         }
     }
 }

+ 1 - 1
views/dashboardPage/components/ingredientDetails.ejs

@@ -107,7 +107,7 @@
                     quantity: Number(document.querySelector("#ingredientInput").value)
                 }];
 
-                if(validator.ingredient.quantity(data[0].quantity)){
+                if(validator.ingredientQuantity(data[0].quantity)){
                     fetch("/merchant/ingredients/update", {
                         method: "PUT",
                         headers: {

+ 23 - 90
views/shared/validation.js

@@ -38,96 +38,29 @@ let validator = {
         return true;
     },
 
-    // ingredient: {
-    //     name: function(ingName, createBanner = true){
-    //         //Check for special chars
-    //         if(!validator.isSanitary(ingName)){
-    //             if(createBanner){
-    //                 banner.createError("Your inputs contain illegal characters");
-    //             }
-    //             return false;
-    //         }
-
-    //         //Check for length
-    //         if(ingName.length < 2){
-    //             if(createBanner){
-    //                 banner.createError("Ingredient name must contain at least 2 characters");
-    //             }
-    //             return false;
-    //         }
-
-    //         return true;
-    //     },
-
-    //     category: function(ingCategory, createBanner = true){
-    //         //Check for special chars
-    //         if(!validator.isSanitary(ingCategory)){
-    //             if(createBanner){
-    //                 banner.createError("Your inputs contain illegal characters");
-    //             }
-
-    //             return false;
-    //         }
-
-    //         //Check for length
-    //         if(ingCategory.length < 3){
-    //             if(createBanner){
-    //                 banner.createError("Category name must contain at least 3 characters");
-    //             }
-    //             return false;
-    //         }
-
-    //         return true;
-    //     },
-
-    //     quantity: function(num, createBanner = true){
-    //         if(isNaN(num) || num === ""){
-    //             if(createBanner){
-    //                 banner.createError("Must enter a valid number");
-    //             }
-
-    //             return false;
-    //         }
-
-    //         if(num < 0){
-    //             if(createBanner){
-    //                 banner.createError("Quantity cannot be a negative number");
-    //             }
-
-    //             return false;
-    //         }
-
-    //         return true;
-    //     },
-
-    //     unit: function(ingUnit, createBanner = true){
-    //         //Check for special chars
-    //         if(!validator.isSanitary(ingUnit)){
-    //             if(createBanner){
-    //                 banner.createError("Your inputs contain illegal characters");
-    //             }
-
-    //             return false;
-    //         }
-
-    //         return true;
-    //     },
-
-    //     //Check all parts of ingredient, return true if all pass
-    //     //Quantity passed seperately and optional
-    //     all: function(ingObject, quantity = 0, createBanner = true){
-    //         let nameCheck = this.name(ingObject.name, createBanner);
-    //         let categoryCheck = this.category(ingObject.category, createBanner);
-    //         let unitCheck = this.unit(ingObject.unit, createBanner);
-    //         let quantityCheck = this.quantity(quantity, createBanner);
-
-    //         if(!nameCheck || !categoryCheck || !quantityCheck || !unitCheck){
-    //             return false;
-    //         }
-
-    //         return true;
-    //     }
-    // },
+    ingredientQuantity: function(quantity, createBanner = true){
+        let errors = [];
+
+        if(isNaN(quantity) || quantity === ""){
+            errors.push("Must enter a valid number");
+        }
+
+        if(quantity < 0){
+            banner.createError("Quantity cannot be a negative number");
+        }
+
+        if(errors.length > 0){
+            if(createBanner){
+                for(let i = 0; i < errors.length; i++){
+                    banner.createError(errors[i]);
+                }
+            }
+
+            return false;
+        }
+
+        return true;
+    },
 
     merchant: {
         password: function(pass, confirmPass, createBanner = true){