Browse Source

Finish backend adding of ingredients

Lee Morgan 6 năm trước cách đây
mục cha
commit
25983788f2
2 tập tin đã thay đổi với 30 bổ sung16 xóa
  1. 13 9
      controllers/merchantData.js
  2. 17 7
      views/inventoryPage/addIngredient.js

+ 13 - 9
controllers/merchantData.js

@@ -165,10 +165,9 @@ module.exports = {
 
     //POST - Adds an ingredient to merchant's inventory
     //Inputs:
-    //  req.body.ingredient: ingredient id
-    //  req.body.quantity: quantity for the ingredient
+    //  req.body: array of objects containing ingredient id and quantity
     //Returns:
-    //  ingredient: Newly added ingredient
+    //  entire inventory of merchant
     addMerchantIngredient: function(req, res){
         if(!req.session.user){
             req.session.error = "Must be logged in to do that";
@@ -177,21 +176,26 @@ module.exports = {
 
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
-                for(let item of merchant.inventory){
-                    if(item.ingredient.toString() === req.body.ingredient){
-                        return res.json("Ingredient is already in your inventory");
+                for(let ingredient of req.body){
+                    for(let item of merchant.inventory){
+                        if(item.ingredient.toString() === ingredient.id){
+                            return res.json("Error: Duplicate ingredient detected");
+                        }
                     }
+                    
+                    merchant.inventory.push({
+                        ingredient: ingredient.id,
+                        quantity: ingredient.quantity
+                    });
                 }
 
-                merchant.inventory.push(req.body);
                 merchant.save()
                     .then((newMerchant)=>{
                         newMerchant.populate("inventory.ingredient", (err)=>{
                             if(err){
                                 return res.json("Warning: refresh page to view updates");
                             }else{
-                                let newIngredient = newMerchant.inventory.find(i => i.ingredient._id.toString() === req.body.ingredient);
-                                return res.json(newIngredient);
+                                return res.json(newMerchant.inventory);
                             }
                         });
                     })

+ 17 - 7
views/inventoryPage/addIngredient.js

@@ -13,13 +13,17 @@ window.addIngredientObj = {
     },
 
     populateIngredients: function(){
+        let tbody = document.querySelector("#addIngredientAction tbody");
+
+        while(tbody.children.length > 0){
+            tbody.removeChild(tbody.firstChild);
+        }
+
         axios.get("/ingredients")
             .then((response)=>{
                 if(typeof(response.data) === "string"){
                     banner.createError(response.data);
                 }else{
-                    let tbody = document.querySelector("#addIngredientAction tbody");
-
                     for(let ingredient of response.data){
                         let exists = false;
                         for(let merchIngredient of merchant.inventory){
@@ -81,8 +85,8 @@ window.addIngredientObj = {
             if(row.children[0].children[0].checked){
                 let quantity = row.children[4].children[0].value;
                 if(validator.ingredient.quantity(quantity)){
-                    addList.append({
-                        ingredient: row._id,
+                    addList.push({
+                        id: row._id,
                         quantity: quantity
                     });
                 }else{
@@ -93,9 +97,15 @@ window.addIngredientObj = {
 
         axios.post("/merchant/ingredients/create", addList)
             .then((response)=>{
-                banner.createNotification("All ingredients successfully added");
-                this.isPopulated = false;
-                window.inventoryObj.display();
+                if(typeof(response.data) === "string"){
+                    banner.createError(response.data);
+                }else{
+                    banner.createNotification("All ingredients successfully added");
+                    merchant.inventory = response.data;
+                    this.isPopulated = false;
+                    window.inventoryObj.isPopulated = false;
+                    window.inventoryObj.display();
+                }
             })
             .catch((err)=>{
                 banner.createError("Error: Something went wrong.  Try refreshing the page");