recipeData.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const Recipe = require("../models/recipe");
  2. const Merchant = require("../models/merchant");
  3. module.exports = {
  4. //POST - creates a single new recipe
  5. //Inputs:
  6. // req.body.name: name of recipes
  7. // req.body.price: price of the recipe
  8. // req.body.ingredients: array of ingredients (object) in recipe
  9. // id: id of ingredient
  10. // quantity: quantity of ingredient in recipe
  11. createRecipe: function(req, res){
  12. if(!req.session.user){
  13. req.session.error = "Must be logged in to do that";
  14. return res.redirect("/");
  15. }
  16. let recipe = new Recipe({
  17. merchant: req.session.user,
  18. name: req.body.name,
  19. price: Math.round(req.body.price * 100),
  20. ingredients: req.body.ingredients
  21. });
  22. Merchant.findOne({_id: req.session.user})
  23. .then((merchant)=>{
  24. merchant.recipes.push(recipe);
  25. merchant.save()
  26. .catch((err)=>{
  27. return res.json("Error: unable to save recipe");
  28. });
  29. })
  30. .catch((err)=>{
  31. return res.json("Error: unable to retrieve user data");
  32. })
  33. recipe.save()
  34. .then((newRecipe)=>{
  35. return res.json({});
  36. })
  37. .catch((err)=>{
  38. console.log(err);
  39. return res.json("Error: unable to save new ingredient");
  40. });
  41. }
  42. }