Recipe.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const recipeBook = require("../strands/recipeBook.js");
  2. const analytics = require("../strands/analytics.js");
  3. class RecipeIngredient{
  4. constructor(ingredient, quantity, unit, baseUnitMultiplier){
  5. this._ingredient = ingredient;
  6. this._quantity = quantity;
  7. this._unit = unit;
  8. this._baseUnitMultiplier = baseUnitMultiplier;
  9. }
  10. get ingredient(){
  11. return this._ingredient;
  12. }
  13. get quantity(){
  14. switch(this._ingredient.unit){
  15. case "g":return this._quantity;
  16. case "kg": return this._quantity / 1000;
  17. case "oz": return this._quantity / 28.3495;
  18. case "lb": return this._quantity / 453.5924;
  19. case "ml": return this._quantity * 1000;
  20. case "l": return this._quantity;
  21. case "tsp": return this._quantity * 202.8842;
  22. case "tbsp": return this._quantity * 67.6278;
  23. case "ozfl": return this._quantity * 33.8141;
  24. case "cup": return this._quantity * 4.1667;
  25. case "pt": return this._quantity * 2.1134;
  26. case "qt": return this._quantity * 1.0567;
  27. case "gal": return this._quantity / 3.7854;
  28. case "mm": return this._quantity * 1000;
  29. case "cm": return this._quantity * 100;
  30. case "m": return this._quantity;
  31. case "in": return this._quantity * 39.3701;
  32. case "ft": return this._quantity * 3.2808;
  33. default: return this._quantity;
  34. }
  35. }
  36. set quantity(quantity){
  37. this_quantity = controller.baseUnit(quantity, this._ingredient.unit);
  38. }
  39. getQuantityDisplay(){
  40. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  41. }
  42. }
  43. /*
  44. Recipe Object
  45. id = database id of recipe
  46. name = name of recipe
  47. price = price of recipe in cents
  48. ingredients = [{
  49. ingredient: Ingredient Object,
  50. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  51. }]
  52. parent = merchant that it belongs to
  53. */
  54. class Recipe{
  55. constructor(id, name, category, price, ingredients, parent, hidden){
  56. this._id = id;
  57. this._name = name;
  58. this._category = category;
  59. this._price = price;
  60. this._parent = parent;
  61. this._hidden = hidden;
  62. this._ingredients = [];
  63. this._ingredientTotals = {};
  64. for(let i = 0; i < ingredients.length; i++){
  65. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  66. const recipeIngredient = new RecipeIngredient(
  67. ingredient.ingredient,
  68. ingredients[i].quantity,
  69. ingredients[i].unit,
  70. ingredients[i].baseUnitMultiplier
  71. );
  72. this._ingredients.push(recipeIngredient);
  73. }
  74. }
  75. get id(){
  76. return this._id;
  77. }
  78. get name(){
  79. return this._name;
  80. }
  81. set name(name){
  82. this._name = name;
  83. }
  84. get category(){
  85. return this._category;
  86. }
  87. set category(category){
  88. this._category = category;
  89. }
  90. get price(){
  91. return this._price / 100;
  92. }
  93. set price(price){
  94. this._price = price;
  95. }
  96. get parent(){
  97. return this._parent;
  98. }
  99. get hidden(){
  100. return this._hidden;
  101. }
  102. set hidden(hidden){
  103. this._hidden = hidden;
  104. }
  105. get ingredients(){
  106. return this._ingredients;
  107. }
  108. clearIngredients(){
  109. this._ingredients = [];
  110. }
  111. get ingredientTotals(){
  112. return this._ingredientTotals;
  113. }
  114. getIngredientTotal(id){
  115. return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id];
  116. }
  117. addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
  118. let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
  119. this._ingredients.push(recipeIngredient);
  120. recipeBook.isPopulated = false;
  121. analytics.isPopulated = false;
  122. }
  123. removeIngredients(){
  124. this._ingredients = [];
  125. }
  126. calculateIngredientTotals(){
  127. this._ingredientTotals = {};
  128. let traverseIngredient = (ingredient, multiplier)=>{
  129. for(let i = 0; i < ingredient.subIngredients.length; i++){
  130. traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);
  131. }
  132. if(this._ingredientTotals[ingredient.id] === undefined){
  133. this._ingredientTotals[ingredient.id] = multiplier;
  134. }else{
  135. this._ingredientTotals[ingredient.id] += multiplier;
  136. }
  137. }
  138. for(let i = 0; i < this._ingredients.length; i++){
  139. traverseIngredient(this._ingredients[i]._ingredient, this._ingredients[i].quantity);
  140. }
  141. }
  142. }
  143. module.exports = Recipe;