recipeData.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const Recipe = require("../models/recipe.js");
  2. const Merchant = require("../models/merchant.js");
  3. const RecipeChange = require("../models/recipeChange.js");
  4. const Validator = require("./validator.js");
  5. module.exports = {
  6. /*
  7. POST - creates a single new recipe
  8. req.body = {
  9. name: name of recipe,
  10. price: price of the recipe,
  11. ingredients: [{
  12. id: id of ingredient,
  13. quantity: quantity of ingredient in recipe
  14. }]
  15. }
  16. Return = newly created recipe in same form as above, with _id
  17. */
  18. createRecipe: function(req, res){
  19. if(!req.session.user){
  20. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  21. return res.redirect("/");
  22. }
  23. let validation = Validator.recipe(req.body);
  24. if(validation !== true){
  25. return res.json(validation);
  26. }
  27. let recipe = new Recipe({
  28. merchant: req.session.user,
  29. name: req.body.name,
  30. price: Math.round(req.body.price * 100),
  31. ingredients: req.body.ingredients
  32. });
  33. Merchant.findOne({_id: req.session.user})
  34. .then((merchant)=>{
  35. merchant.recipes.push(recipe);
  36. merchant.save()
  37. .catch((err)=>{
  38. return res.json("ERROR: UNABLE TO SAVE RECIPE");
  39. });
  40. })
  41. .catch((err)=>{
  42. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  43. });
  44. recipe.save()
  45. .then((newRecipe)=>{
  46. return res.json(newRecipe);
  47. })
  48. .catch((err)=>{
  49. return res.json("ERROR: UNABLE TO SAVE INGREDIENT");
  50. });
  51. },
  52. /*
  53. PUT - Update a single recipe
  54. req.body = {
  55. id: id of recipe,
  56. name: name of recipe,
  57. price: price of recipe,
  58. ingredients: [{
  59. ingredient: id of ingredient,
  60. quantity: quantity of ingredient in recipe
  61. }]
  62. }
  63. */
  64. updateRecipe: function(req, res){
  65. if(!req.session.user){
  66. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  67. return res.redirect("/");
  68. }
  69. let validation = Validator.recipe(req.body);
  70. if(validation !== true){
  71. return res.json(validation);
  72. }
  73. let changes = [];
  74. Recipe.findOne({_id: req.body.id})
  75. .then((recipe)=>{
  76. for(let i = 0; i < req.body.ingredients.length; i++){
  77. let isMatch = false;
  78. for(let j = 0; j < recipe.ingredients.length; j++){
  79. if(req.body.ingredients[i].ingredient === recipe.ingredients[j].ingredient.toString()){
  80. let difference = parseFloat((req.body.ingredients[i].quantity - recipe.ingredients[j].quantity).toFixed(2));
  81. if(difference !== 0){
  82. changes.push({
  83. ingredient: recipe.ingredients[j].ingredient,
  84. change: difference
  85. });
  86. }
  87. isMatch = true;
  88. break;
  89. }
  90. }
  91. if(!isMatch){
  92. changes.push({
  93. ingredient: req.body.ingredients[i].ingredient,
  94. change: req.body.ingredients[i].quantity
  95. });
  96. }
  97. }
  98. for(let i = 0; i < recipe.ingredients.length; i++){
  99. let isMatch = false;
  100. for(let j = 0; j < req.body.ingredients.length; j++){
  101. if(recipe.ingredients[i].ingredient.toString() === req.body.ingredients[i].ingredient){
  102. isMatch = true;
  103. }
  104. }
  105. if(!isMatch){
  106. changes.push({
  107. ingredient: recipe.ingredients[i].ingredient,
  108. change: -recipe.ingredients[i].quantity
  109. });
  110. }
  111. }
  112. console.log(changes);
  113. recipe.name = req.body.name;
  114. recipe.price = req.body.price;
  115. recipe.ingredients = req.body.ingredients;
  116. return recipe.save()
  117. })
  118. .then((response)=>{
  119. res.json({});
  120. let recipeChange = new RecipeChange({
  121. recipe: response._id,
  122. date: new Date(),
  123. changes: changes
  124. });
  125. return recipeChange.save()
  126. })
  127. .catch((err)=>{
  128. return res.json("ERROR: UNABLE TO UPDATE RECIPE");
  129. });
  130. }
  131. }