ingredientData.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const Merchant = require("../models/merchant");
  2. const Ingredient = require("../models/ingredient");
  3. module.exports = {
  4. //GET - gets a list of all database ingredients
  5. //Returns:
  6. // ingredients: list containing all ingredients
  7. getIngredients: function(req, res){
  8. Ingredient.find()
  9. .then((ingredients)=>{
  10. return res.json(ingredients);
  11. })
  12. .catch((err)=>{
  13. let errorMessage = "Unable to retrieve ingredients";
  14. let error = new Error({
  15. code: 626,
  16. displayMessage: errorMessage,
  17. error: err
  18. });
  19. error.save();
  20. return res.json(errorMessage);
  21. });
  22. },
  23. //POST - creates new ingredients from a list
  24. //Inputs:
  25. // req.body: list of ingredients (name, category, unit)
  26. //Returns:
  27. // ingredients: list containing the newly created ingredients
  28. createNewIngredients: function(req, res){
  29. if(!req.session.user){
  30. req.session.error = "Must be logged in to do that";
  31. return res.redirect("/");
  32. }
  33. Ingredient.create(req.body)
  34. .then((ingredients)=>{
  35. return res.json(ingredients);
  36. })
  37. .catch((err)=>{
  38. let errorMessage = "There was an error and the ingredients could not be created";
  39. let error = new Error({
  40. code: 547,
  41. displayMessage: errorMessage,
  42. error: err
  43. });
  44. error.save();
  45. return res.json(errorMessage);
  46. });
  47. },
  48. //TODO - Redirect to merchantData.js rather than adding here
  49. //POST - create a single ingredient and then add to the merchant
  50. //Inputs:
  51. // req.body.ingredient: full ingredient to create (name, category, unit)
  52. // req.body.quantity: quantity of ingredient for merchant
  53. //Returns:
  54. // item: ingredient and quantity
  55. createIngredient: function(req, res){
  56. if(!req.session.user){
  57. req.session.error = "Must be logged in to do that";
  58. return res.redirect("/");
  59. }
  60. Ingredient.create(req.body.ingredient)
  61. .then((ingredient)=>{
  62. Merchant.findOne({_id: req.session.user})
  63. .then((merchant)=>{
  64. let item = {
  65. ingredient: ingredient,
  66. quantity: req.body.quantity
  67. }
  68. merchant.inventory.push(item);
  69. merchant.save()
  70. .then((merchant)=>{
  71. return res.json(item);
  72. })
  73. .catch((err)=>{
  74. let errorMessage = "There was an error and the data could not be saved";
  75. let error = new Error({
  76. code: 547,
  77. displayMessage: errorMessage,
  78. error: err
  79. });
  80. error.save();
  81. return res.json(errorMessage);
  82. });
  83. })
  84. .catch((err)=>{
  85. let errorMessage = "There was an error and your data could not be retrieved";
  86. let error = new Error({
  87. code: 626,
  88. displayMessage: errorMessage,
  89. error: err
  90. });
  91. error.save();
  92. return res.json(errorMessage);
  93. });
  94. })
  95. .catch((err)=>{
  96. let errorMessage = "There was an error and the ingredient could not be created";
  97. let error = new Error({
  98. code: 547,
  99. displayMessage: errorMessage,
  100. error: err
  101. });
  102. error.save();
  103. return res.json(errorMessage);
  104. });
  105. }
  106. }