Recipe.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, price, ingredients, parent, hidden){
  54. this._id = id;
  55. this._name = name;
  56. this._price = price;
  57. this._parent = parent;
  58. this._hidden = hidden;
  59. this._ingredients = [];
  60. for(let i = 0; i < ingredients.length; i++){
  61. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  62. const recipeIngredient = new RecipeIngredient(
  63. ingredient.ingredient,
  64. ingredients[i].quantity
  65. );
  66. this._ingredients.push(recipeIngredient);
  67. }
  68. }
  69. get id(){
  70. return this._id;
  71. }
  72. get name(){
  73. return this._name;
  74. }
  75. set name(name){
  76. this._name = name;
  77. }
  78. get price(){
  79. return this._price / 100;
  80. }
  81. set price(price){
  82. this._price = price;
  83. }
  84. get parent(){
  85. return this._parent;
  86. }
  87. get hidden(){
  88. return this._hidden;
  89. }
  90. set hidden(hidden){
  91. this._hidden = hidden;
  92. }
  93. get ingredients(){
  94. return this._ingredients;
  95. }
  96. addIngredient(ingredient, quantity){
  97. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  98. this._ingredients.push(recipeIngredient);
  99. recipeBook.isPopulated = false;
  100. analytics.isPopulated = false;
  101. }
  102. removeIngredients(){
  103. this._ingredients = [];
  104. }
  105. }
  106. module.exports = Recipe;