Recipe.js 4.4 KB

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