recipeData.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const Recipe = require("../models/recipe");
  2. const Merchant = require("../models/merchant");
  3. module.exports = {
  4. //POST - creates a single new recipe
  5. //Inputs:
  6. // req.body.name: name of recipes
  7. // req.body.price: price of the recipe
  8. // req.body.ingredients: array of ingredients (object) in recipe
  9. // id: id of ingredient
  10. // quantity: quantity of ingredient in recipe
  11. //Returns newly created ingredient
  12. createRecipe: function(req, res){
  13. if(!req.session.user){
  14. req.session.error = "Must be logged in to do that";
  15. return res.redirect("/");
  16. }
  17. let recipe = new Recipe({
  18. merchant: req.session.user,
  19. name: req.body.name,
  20. price: Math.round(req.body.price * 100),
  21. ingredients: req.body.ingredients
  22. });
  23. Merchant.findOne({_id: req.session.user})
  24. .then((merchant)=>{
  25. merchant.recipes.push(recipe);
  26. merchant.save()
  27. .catch((err)=>{
  28. return res.json("Error: unable to save recipe");
  29. });
  30. })
  31. .catch((err)=>{
  32. return res.json("Error: unable to retrieve user data");
  33. })
  34. recipe.save()
  35. .then((newRecipe)=>{
  36. return res.json(newRecipe);
  37. })
  38. .catch((err)=>{
  39. return res.json("Error: unable to save new ingredient");
  40. });
  41. },
  42. //PUT - Update a single recipe
  43. //Inputs:
  44. // req.body: An object representing a single recipe
  45. // _id: id of recipe
  46. // name: name of recipe
  47. // price: price of recipe
  48. // ingredients: list of objects representing ingredients
  49. // ingredient: id of ingredient
  50. // quantity: quantity of ingredient
  51. updateRecipe: function(req, res){
  52. if(!req.session.user){
  53. req.session.error = "Must be logged in to do that";
  54. return res.redirect("/");
  55. }
  56. Recipe.findOne({_id: req.body._id})
  57. .then((recipe)=>{
  58. recipe.name = req.body.name;
  59. recipe.price = req.body.price;
  60. for(let i = 0; i < req.body.ingredients.length; i++){
  61. let isNew = true;
  62. for(let j = 0; j < recipe.ingredients.length; j++){
  63. if(req.body.ingredients[i].ingredient === recipe.ingredients[j].ingredient._id.toString()){
  64. isNew = false;
  65. recipe.ingredients[j].quantity = req.body.ingredients[i].quantity;
  66. break;
  67. }
  68. }
  69. if(isNew){
  70. recipe.ingredients.push(req.body.ingredients[i]);
  71. }
  72. }
  73. for(let i = 0; i < recipe.ingredients.length; i++){
  74. let doesntExist = true;
  75. for(let j = 0; j < req.body.ingredients.length; j++){
  76. if(recipe.ingredients[i].ingredient._id.toString() === req.body.ingredients[j].ingredient){
  77. doesntExist = false;
  78. break;
  79. }
  80. }
  81. if(doesntExist){
  82. recipe.ingredients.splice(i, 1);
  83. }
  84. }
  85. return recipe.save()
  86. })
  87. .then((response)=>{
  88. return res.json({});
  89. })
  90. .catch((err)=>{
  91. return res.json("Error: unable to update your recipe");
  92. })
  93. }
  94. }