Recipe.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = this.convertToBase(quantity);
  36. }
  37. getQuantityDisplay(){
  38. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  39. }
  40. convertToBase(quantity){
  41. switch(this._ingredient.unit){
  42. case "g": return quantity;
  43. case "kg": return quantity * 1000;
  44. case "oz": return quantity * 28.3495;
  45. case "lb": return quantity * 453.5924;
  46. case "ml": return quantity / 1000;
  47. case "l": return quantity;
  48. case "tsp": return quantity / 202.8842;
  49. case "tbsp": return quantity / 67.6278;
  50. case "ozfl": return quantity / 33.8141;
  51. case "cup": return quantity / 4.1667;
  52. case "pt": return quantity / 2.1134;
  53. case "qt": return quantity / 1.0567;
  54. case "gal": return quantity * 3.7854;
  55. case "mm": return quantity / 1000;
  56. case "cm": return quantity / 100;
  57. case "m": return quantity;
  58. case "in": return quantity / 39.3701;
  59. case "ft": return quantity / 3.2808;
  60. default: return quantity;
  61. }
  62. }
  63. }
  64. /*
  65. Recipe Object
  66. id = database id of recipe
  67. name = name of recipe
  68. price = price of recipe in cents
  69. ingredients = [{
  70. ingredient: Ingredient Object,
  71. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  72. }]
  73. parent = merchant that it belongs to
  74. */
  75. class Recipe{
  76. constructor(id, name, price, ingredients, parent){
  77. this._id = id;
  78. this._name = name;
  79. this._price = price;
  80. this._parent = parent;
  81. this._ingredients = [];
  82. for(let i = 0; i < ingredients.length; i++){
  83. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  84. const recipeIngredient = new RecipeIngredient(
  85. ingredient.ingredient,
  86. ingredients[i].quantity
  87. );
  88. this._ingredients.push(recipeIngredient);
  89. }
  90. }
  91. get id(){
  92. return this._id;
  93. }
  94. get name(){
  95. return this._name;
  96. }
  97. set name(name){
  98. this._name = name;
  99. }
  100. get price(){
  101. return this._price / 100;
  102. }
  103. set price(price){
  104. this._price = price;
  105. }
  106. get parent(){
  107. return this._parent;
  108. }
  109. get ingredients(){
  110. return this._ingredients;
  111. }
  112. addIngredient(ingredient, quantity){
  113. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  114. this._ingredients.push(recipeIngredient);
  115. recipeBook.isPopulated = false;
  116. analytics.isPopulated = false;
  117. }
  118. removeIngredients(){
  119. this._ingredients = [];
  120. }
  121. }
  122. module.exports = Recipe;