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

Update classes and basics of ingredients to make it work with the new unit system

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

+ 6 - 3
controllers/ingredientData.js

@@ -22,9 +22,10 @@ module.exports = {
         ingredient: {
             name: name of ingredient,
             category: category of ingredient,
-            unit: unit measurement of ingredient
+            unitType: category for the unit (mass, volume, length)
         },
-        quantity: quantity of ingredient for current merchant
+        quantity: quantity of ingredient for current merchant,
+        defaultUnit: default unit of measurement to display
     }
     Returns:
         Same as above, with the _id
@@ -52,7 +53,8 @@ module.exports = {
             .then((response)=>{
                 newIngredient = {
                     ingredient: response[0],
-                    quantity: req.body.quantity
+                    quantity: req.body.quantity,
+                    defaultUnit: req.body.defaultUnit
                 }
 
                 response[1].inventory.push(newIngredient);
@@ -63,6 +65,7 @@ module.exports = {
                 return res.json(newIngredient);
             })
             .catch((err)=>{
+                console.log(err);
                 return res.json("ERROR: UNABLE TO CREATE NEW INGREDIENT");
             });
     }

+ 1 - 1
controllers/validator.js

@@ -61,7 +61,7 @@ module.exports = {
     },
 
     ingredient: function(ingredient){
-        if(!this.isSanitary([ingredient.name, ingredient.category, ingredient.unit])){
+        if(!this.isSanitary([ingredient.name, ingredient.category])){
             return "Ingredient contains illegal characters";
         }
 

+ 1 - 1
models/ingredient.js

@@ -10,7 +10,7 @@ const IngredientSchema = new mongoose.Schema({
         type: String,
         minlength: 3
     },
-    unit: {
+    unitType: {
         type: String,
         required: true
     }

+ 1 - 1
models/merchant.js

@@ -39,7 +39,7 @@ const MerchantSchema = new mongoose.Schema({
             required: true,
             min: 0
         },
-        displayUnit: {
+        defaultUnit: {
             type: String,
             required: true
         }

+ 12 - 25
views/dashboardPage/Merchant.js

@@ -1,8 +1,9 @@
 class Ingredient{
-    constructor(id, name, category, unit){
+    constructor(id, name, category, unitType, unit){
         this.id = id;
         this.name = name;
         this.category = category;
+        this.unitType = unitType;
         this.unit = unit;
     }
 }
@@ -87,7 +88,8 @@ class Merchant{
                     oldMerchant.inventory[i].ingredient._id,
                     oldMerchant.inventory[i].ingredient.name,
                     oldMerchant.inventory[i].ingredient.category,
-                    oldMerchant.inventory[i].ingredient.unit,
+                    oldMerchant.inventory[i].ingredient.unitType,
+                    oldMerchant.inventory[i].defaultUnit,
                 ),
                 quantity: oldMerchant.inventory[i].quantity
             });
@@ -491,33 +493,18 @@ let convertMass = (quantity, from, to)=>{
     //change to g
     let converted = 0;
     switch(from){
-        case "g":
-            converted = quantity;
-            break;
-        case "kg":
-            converted = quantity * 1000;
-            break;
-        case "oz":
-            converted = quantity * 28.3495;
-            break;
-        case "lb":
-            converted = quantity * 453.5924;
-            break;
+        case "g": converted = quantity; break;
+        case "kg": converted = quantity * 1000; break;
+        case "oz": converted = quantity * 28.3495; break;
+        case "lb": converted = quantity * 453.5924; break;
     }
 
     //change to end
     switch(to){
-        case "g":
-            break;
-        case "kg":
-            converted = converted / 1000;
-            break;
-        case "oz":
-            converted = converted / 28.3495;
-            break;
-        case "lb":
-            converted = converted / 453.5924;
-            break;
+        case "g": break;
+        case "kg": converted = converted / 1000; break;
+        case "oz": converted = converted / 28.3495; break;
+        case "lb": converted = converted / 453.5924; break;
     }
 
     return converted;

+ 18 - 18
views/dashboardPage/sidebars/newIngredient.ejs

@@ -25,30 +25,30 @@
     <label>UNIT:
         <select id="unitSelector">
             <optgroup label="MASS/WEIGHT">
-                <option value="g">g</option>
-                <option value="kg">kg</option>
-                <option value="oz">oz</option>
-                <option value="lb">lb</option>
+                <option type="mass" value="g">G</option>
+                <option type="mass" value="kg">KG</option>
+                <option type="mass" value="oz">OZ</option>
+                <option type="mass" value="lb">LB</option>
             </optgroup>
 
             <optgroup label="VOLUME">
-                <option value="ml">ml</option>
-                <option value="l">l</option>
-                <option value="tsp">tsp</option>
-                <option value="tbsp">tbsp</option>
-                <option value="ozfl">oz. fl</option>
-                <option value="cup">cup</option>
-                <option value="pt">pt</option>
-                <option value="qt">qt</option>
-                <option value="gal">gal</option>
+                <option type="volume" value="ml">ML</option>
+                <option type="volume" value="l">L</option>
+                <option type="volume" value="tsp">TSP</option>
+                <option type="volume" value="tbsp">TBSP</option>
+                <option type="volume" value="ozfl">OZ. FL</option>
+                <option type="volume" value="cup">CUP</option>
+                <option type="volume" value="pt">PT</option>
+                <option type="volume" value="qt">QT</option>
+                <option type="volume" value="gal">GAL</option>
             </optgroup>
 
             <optgroup label="LENGTH">
-                <option value="m">m</option>
-                <option value="cm">cm</option>
-                <option value="mm">mm</option>
-                <option value="in">in</option>
-                <option value="ft">ft</option>
+                <option type="length" value="mm">MM</option>
+                <option type="length" value="cm">CM</option>
+                <option type="length" value="m">M</option>
+                <option type="length" value="in">IN</option>
+                <option type="length" value="ft">FT</option>
             </optgroup>
 
             <optgroup label="OTHER">

+ 9 - 3
views/dashboardPage/sidebars/sidebars.js

@@ -321,13 +321,17 @@ let newIngredientComp = {
     },
 
     submit: function(){
+        let unitSelector = document.getElementById("unitSelector");
+        let options = document.querySelectorAll("#unitSelector option");
+
         let newIngredient = {
             ingredient: {
                 name: document.getElementById("newIngName").value,
                 category: document.getElementById("newIngCategory").value,
-                unit: document.getElementById("unitSelector").value
+                unitType: options[unitSelector.selectedIndex].getAttribute("type"),
             },
-            quantity: document.querySelector("#newIngQuantity").value
+            quantity: document.querySelector("#newIngQuantity").value,
+            defaultUnit: unitSelector.value
         }
 
         let loader = document.getElementById("loaderContainer");
@@ -350,7 +354,8 @@ let newIngredientComp = {
                             response.ingredient._id,
                             response.ingredient.name,
                             response.ingredient.category,
-                            response.ingredient.unit
+                            response.ingredient.unitType,
+                            response.defaultUnit
                         ),
                         quantity: response.quantity
                     }]);
@@ -359,6 +364,7 @@ let newIngredientComp = {
                 }
             })
             .catch((err)=>{
+                console.log(err);
                 banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
             })
             .finally(()=>{