ingredientData.js 4.1 KB

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