ingredientData.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. console.log(err);
  14. return res.render("error");
  15. });
  16. },
  17. //POST - creates new ingredients from a list
  18. //Inputs:
  19. // req.body: list of ingredients (name, category, unit)
  20. //Returns:
  21. // ingredients: list containing the newly created ingredients
  22. createNewIngredients: function(req, res){
  23. if(!req.session.user){
  24. return res.render("error");
  25. }
  26. Ingredient.create(req.body)
  27. .then((ingredients)=>{
  28. return res.json(ingredients);
  29. })
  30. .catch((err)=>{
  31. console.log(err);
  32. return res.render("error");
  33. });
  34. },
  35. //TODO - Redirect to merchantData.js rather than adding here
  36. //POST - create a single ingredient and then add to the merchant
  37. //Inputs:
  38. // req.body.ingredient: full ingredient to create (name, category, unit)
  39. // req.body.quantity: quantity of ingredient for merchant
  40. //Returns:
  41. // item: ingredient and quantity
  42. createIngredient: function(req, res){
  43. if(!req.session.user){
  44. return res.render("error");
  45. }
  46. Ingredient.create(req.body.ingredient)
  47. .then((ingredient)=>{
  48. Merchant.findOne({_id: req.session.user})
  49. .then((merchant)=>{
  50. let item = {
  51. ingredient: ingredient,
  52. quantity: req.body.quantity
  53. }
  54. merchant.inventory.push(item);
  55. merchant.save()
  56. .then((merchant)=>{
  57. return res.json(item);
  58. })
  59. .catch((err)=>{
  60. console.log(err);
  61. return res.render("error");
  62. });
  63. })
  64. .catch((err)=>{
  65. console.log(err);
  66. return res.render("error");
  67. });
  68. })
  69. .catch((err)=>{
  70. console.log(err);
  71. return res.render("error");
  72. });
  73. }
  74. }