Recipe.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. return this._quantity;
  15. }
  16. set quantity(quantity){
  17. this_quantity = controller.baseUnit(quantity, this._ingredient.unit);
  18. }
  19. getQuantityDisplay(){
  20. return `${this._quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
  21. }
  22. getQuantityAsBase(){
  23. return this._quantity * this._baseUnitMultiplier;
  24. }
  25. }
  26. /*
  27. Recipe Object
  28. id = database id of recipe
  29. name = name of recipe
  30. price = price of recipe in cents
  31. ingredients = [{
  32. ingredient: Ingredient Object,
  33. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  34. unit: String
  35. baseUnitmultiplier: Number
  36. }]
  37. parent = merchant that it belongs to
  38. */
  39. class Recipe{
  40. constructor(id, name, category, price, ingredients, parent, hidden){
  41. this._id = id;
  42. this._name = name;
  43. this._category = category;
  44. this._price = price;
  45. this._parent = parent;
  46. this._hidden = hidden;
  47. this._ingredients = [];
  48. this._ingredientTotals = {};
  49. this._ingredientTotalsBase = {};
  50. for(let i = 0; i < ingredients.length; i++){
  51. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  52. const recipeIngredient = new RecipeIngredient(
  53. ingredient.ingredient,
  54. ingredients[i].quantity,
  55. ingredients[i].unit,
  56. ingredients[i].baseUnitMultiplier
  57. );
  58. this._ingredients.push(recipeIngredient);
  59. }
  60. }
  61. get id(){
  62. return this._id;
  63. }
  64. get name(){
  65. return this._name;
  66. }
  67. set name(name){
  68. this._name = name;
  69. }
  70. get category(){
  71. return this._category;
  72. }
  73. set category(category){
  74. this._category = category;
  75. }
  76. get price(){
  77. return this._price / 100;
  78. }
  79. set price(price){
  80. this._price = price;
  81. }
  82. get parent(){
  83. return this._parent;
  84. }
  85. get hidden(){
  86. return this._hidden;
  87. }
  88. set hidden(hidden){
  89. this._hidden = hidden;
  90. }
  91. get ingredients(){
  92. return this._ingredients;
  93. }
  94. clearIngredients(){
  95. this._ingredients = [];
  96. }
  97. get ingredientTotals(){
  98. return this._ingredientTotals;
  99. }
  100. getIngredientTotal(id){
  101. return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id];
  102. }
  103. getIngredientTotalBase(id){
  104. return (this._ingredientTotalsBase[i] === undefined) ? 0 : this._ingredientTotals[id];
  105. }
  106. addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
  107. let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
  108. this._ingredients.push(recipeIngredient);
  109. recipeBook.isPopulated = false;
  110. analytics.isPopulated = false;
  111. }
  112. removeIngredients(){
  113. this._ingredients = [];
  114. }
  115. calculateIngredientTotals(){
  116. this._ingredientTotals = {};
  117. let traverseIngredient = (ingredient, multiplier)=>{
  118. for(let i = 0; i < ingredient.subIngredients.length; i++){
  119. traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);
  120. }
  121. if(this._ingredientTotals[ingredient.id] === undefined){
  122. this._ingredientTotals[ingredient.id] = multiplier;
  123. }else{
  124. this._ingredientTotals[ingredient.id] += multiplier;
  125. }
  126. if(this._ingredientTotalsBase[ingredient.id] === undefined){
  127. this._ingredientTotalsBase[ingredient.id] = multiplier * this._baseUnitMultiplier;
  128. }else{
  129. this._ingredientTotalsBase[ingredient.id] += multiplier * this._baseUnitMultiplier;
  130. }
  131. }
  132. for(let i = 0; i < this._ingredients.length; i++){
  133. traverseIngredient(this._ingredients[i]._ingredient, this._ingredients[i].quantity);
  134. }
  135. }
  136. }
  137. module.exports = Recipe;