Forráskód Böngészése

Update the ingredient class.
Update the merchant class.

Lee Morgan 5 éve
szülő
commit
785043dda0

+ 6 - 8
controllers/ingredientData.js

@@ -18,9 +18,9 @@ module.exports = {
                 toVolume: Number
                 toLength: Number
             }
+            unit: String
         }
             quantity: quantity of ingredient for current merchant,
-            unit: String
     }
     Returns:
         Same as above, with the _id
@@ -29,6 +29,7 @@ module.exports = {
         let newIngredient = new Ingredient({
             name: req.body.ingredient.name,
             category: req.body.ingredient.category,
+            unit: req.body.ingredient.unit,
             convert: req.body.ingredient.category,
             ingredients: []
         })
@@ -38,7 +39,6 @@ module.exports = {
                 newIngredient = {
                     ingredient: ingredient._id,
                     quantity: req.body.quantity,
-                    unit: req.body.unit
                 }
 
                 res.locals.merchant.inventory.push(newIngredient);
@@ -64,6 +64,7 @@ module.exports = {
             id: String (id of Ingredient)
             name: String
             category: String
+            unit: String
             convert: {
                 toMass: Number
                 toVolume: Number
@@ -71,7 +72,6 @@ module.exports = {
             }
         }
         quantity: Number
-        unit: String
     }
     response = Ingredient
     */
@@ -80,14 +80,13 @@ module.exports = {
             .then((ingredient)=>{
                 ingredient.name = req.body.ingredient.name;
                 ingredient.category = req.body.ingredient.category;
-                ingredient.convert = req.body.convert;
+                ingredient.convert = req.body.ingredient.convert;
+                ingredient.unit = req.body.ingredient.unit;
                 
                 //find and update ingredient on merchant
                 for(let i = 0; i < res.locals.merchant.inventory.length; i++){
                     if(res.locals.merchant.inventory[i].ingredient.toString() === req.body.id){
-                        res.locals.merchant.inventory[i].unit = req.body.unit;
-                        res.locals.merchant.inventory[i].quantity = req.body.quantity;
-                        
+                        res.locals.merchant.inventory[i].quantity = req.body.quantity;                        
                         break;
                     }
                 }
@@ -97,7 +96,6 @@ module.exports = {
                 return res.json({
                     ingredient: response[0],
                     quantity: req.body.quantity,
-                    defaultUnit: req.body.unit
                 });
             })
             .catch((err)=>{

+ 4 - 0
models/ingredient.js

@@ -21,6 +21,10 @@ const IngredientSchema = new mongoose.Schema({
             message: "INGREDIENT CATEGORY CONTAINS ILLEGAL CHARACTERS"
         }
     },
+    unit: {
+        type: String,
+        required: true
+    },
     convert: {
         toMass: Number,
         toVolume: Number,

+ 0 - 4
models/merchant.js

@@ -45,10 +45,6 @@ const MerchantSchema = new mongoose.Schema({
         quantity: {
             type: Number,
             required: [true, "INGREDIENT QUANTITY IS REQUIRED"]
-        },
-        unit: {
-            type: String,
-            required: [true, "INGREDIENT UNIT IS REQUIRED"]
         }
     }],
     recipes: [{

+ 17 - 0
views/dashboardPage/js/classes/Merchant.js

@@ -42,6 +42,23 @@ class MerchantIngredient{
         this._quantity = quantity;
     }
 
+    quantity(unit = this._ingredient.unit){
+        let convertMultiplier = 1;
+        switch(controller.getBaseUnit(unit)){
+            case "g":
+                convertMultiplier = this._ingredient.convert.toMass;
+                break;
+            case "l":
+                convertMultiplier = this._ingredient.convert.toVolume;
+                break;
+            case "m":
+                convertMultiplier = this._ingredient.convert.toLength;
+                break;
+        }
+
+        return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this._ingredient.unit), unit) * convertMultiplier;
+    }
+
     updateQuantity(quantity){
         this._quantity += controller.baseUnit(quantity, this._ingredient.unit);
     }

+ 2 - 2
views/dashboardPage/js/sidebars/editIngredient.js

@@ -133,14 +133,14 @@ let editIngredient = {
                 id: ingredient.ingredient.id,
                 name: document.getElementById("editIngName").value,
                 category: document.getElementById("editIngCategory").value,
+                unit: unit,
                 convert: {
                     toMass: rightMass / leftMass,
                     toVolume: rigthVolume / leftVolume,
                     toLength: rightLength / leftLength
                 }
             },
-            quantity: quantity * controller.unitMultiplier(unit, controller.getBaseUnit(unit)),
-            unit: unit
+            quantity: quantity * controller.unitMultiplier(unit, controller.getBaseUnit(unit))
         }
 
         let loader = document.getElementById("loaderContainer");

+ 2 - 2
views/dashboardPage/js/sidebars/newIngredient.js

@@ -68,14 +68,14 @@ let newIngredient = {
             ingredient: {
                 name: document.getElementById("newIngName").value,
                 category: document.getElementById("newIngCategory").value,
+                unit: unit,
                 convert: {
                     toMass: controller.baseUnit(massConvertRight, massConvertUnit) / controller.baseUnit(massConvertLeft, unit),
                     toVolume: controller.baseUnit(volumeConvertRight, volumeConvertUnit) / controller.baseUnit(volumeConvertLeft, unit),
                     toLength: controller.baseUnit(lengthConvertRight, lengthConvertUnit) / controller.baseUnit(lengthConvertLeft, unit)
                 }
             },
-            quantity: controller.baseUnit(quantityValue, unit),
-            unit: unit,
+            quantity: controller.baseUnit(quantityValue, unit)
         }
 
         if(controller.unitType(unit) === "mass") newIngredient.convert.toMass = 1;