ingredientData.js 4.1 KB

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