ingredientData.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. Ingredient.create(req.body)
  30. .then((ingredients)=>{
  31. return res.json(ingredients);
  32. })
  33. .catch((err)=>{
  34. let errorMessage = "There was an error and the ingredients could not be created";
  35. let error = new Error({
  36. code: 547,
  37. displayMessage: errorMessage,
  38. error: err
  39. });
  40. error.save();
  41. return res.json(errorMessage);
  42. });
  43. },
  44. //TODO - Redirect to merchantData.js rather than adding here
  45. //POST - create a single ingredient and then add to the merchant
  46. //Inputs:
  47. // req.body.ingredient: full ingredient to create (name, category, unit)
  48. // req.body.quantity: quantity of ingredient for merchant
  49. //Returns:
  50. // item: ingredient and quantity
  51. createIngredient: 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. Ingredient.create(req.body.ingredient)
  57. .then((ingredient)=>{
  58. Merchant.findOne({_id: req.session.user})
  59. .then((merchant)=>{
  60. let item = {
  61. ingredient: ingredient,
  62. quantity: req.body.quantity
  63. }
  64. merchant.inventory.push(item);
  65. merchant.save()
  66. .then((merchant)=>{
  67. return res.json(item);
  68. })
  69. .catch((err)=>{
  70. let errorMessage = "There was an error and the data could not be saved";
  71. let error = new Error({
  72. code: 547,
  73. displayMessage: errorMessage,
  74. error: err
  75. });
  76. error.save();
  77. return res.json(errorMessage);
  78. });
  79. })
  80. .catch((err)=>{
  81. let errorMessage = "There was an error and your data could not be retrieved";
  82. let error = new Error({
  83. code: 626,
  84. displayMessage: errorMessage,
  85. error: err
  86. });
  87. error.save();
  88. return res.json(errorMessage);
  89. });
  90. })
  91. .catch((err)=>{
  92. let errorMessage = "There was an error and the ingredient could not be created";
  93. let error = new Error({
  94. code: 547,
  95. displayMessage: errorMessage,
  96. error: err
  97. });
  98. error.save();
  99. return res.json(errorMessage);
  100. });
  101. }
  102. }