ingredientData.js 2.5 KB

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