Recipe.js 3.8 KB

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