meals.controllers.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /** logic for meal routes */
  2. import Meal from "../models/meal.model.js";
  3. import { deleteAllImagesFromCategoryInternally } from "./images.controllers.js";
  4. export const getMeals = async (req, res) => {
  5. Meal.find().sort({ title: 1 }).exec((err, meals) => {
  6. if (err) {
  7. res.status(404).json({ message: err.message });
  8. } else {
  9. res.status(200).json(meals);
  10. }
  11. });
  12. }
  13. export const getMealsOfUser = async (req, res) => {
  14. let userId = req.params.userId;
  15. Meal.find({ userId: userId }).sort({ title: 1 }).exec((err, meals) => {
  16. if (err) {
  17. res.status(404).json({ message: err.message });
  18. } else {
  19. res.status(200).json(meals);
  20. }
  21. });
  22. }
  23. export const addMeal = async (req, res) => {
  24. const meal = req.body;
  25. const newMeal = new Meal(meal);
  26. try {
  27. await newMeal.save();
  28. res.status(201).json({ 'message': 'successfully added new Meal', 'meal': newMeal });
  29. } catch (error) {
  30. res.status(409).json({ message: error.message });
  31. }
  32. }
  33. export const getSingleMeal = async (req, res) => {
  34. let id = req.params.id;
  35. try {
  36. const singleMeal = await Meal.findById(id);
  37. res.status(200).json(singleMeal);
  38. } catch (error) {
  39. res.status(404).json({ message: error.message });
  40. }
  41. }
  42. export const updateMeal = async (req, res) => {
  43. let newMeal = req.body;
  44. let id = req.params.id;
  45. try {
  46. Meal.findById(id, function (err, meal) {
  47. meal.set('userId', newMeal.userId);
  48. meal.set('title', newMeal.title);
  49. meal.set('recipeLink', newMeal.recipeLink);
  50. meal.set('images', newMeal.images);
  51. meal.set('comment', newMeal.comment);
  52. meal.set('category', newMeal.category);
  53. meal.set('tags', newMeal.tags);
  54. meal.save().then(meal => {
  55. res.status(201).json({ 'info': 'meal updated', meal })
  56. });
  57. });
  58. } catch (error) {
  59. res.status(400).json({ 'info': `Update of meal ${id} failed`, 'message': error.message });
  60. }
  61. }
  62. export const setImageAsMain = async (req, res) => {
  63. const { mealId, imageId } = req.params;
  64. try {
  65. Meal.findById(mealId, function (err, meal) {
  66. const newOrderImages = [];
  67. const revisedImages = meal.images.map(i => {
  68. i.isMain = i._id === imageId;
  69. if (i._id === imageId) newOrderImages.push(i);
  70. return i;
  71. });
  72. revisedImages.forEach(i => {
  73. if (i._id !== imageId) newOrderImages.push(i);
  74. })
  75. meal.set('images', newOrderImages);
  76. meal.save().then(meal => {
  77. res.status(201).json({ 'info': 'updated main image of meal ', meal })
  78. });
  79. });
  80. } catch (error) {
  81. res.status(400).json({ 'info': `Setting Image ${imageId} as main for meal ${mealId} failed`, 'message': error.message });
  82. }
  83. }
  84. export const deleteMeal = async (req, res) => {
  85. let id = req.params.id;
  86. Meal.findByIdAndDelete(id, {}, function (err, meal) {
  87. if (err) {
  88. res.status(400).json({ 'info': `Deletion of meal ${id} failed`, 'message': err.message });
  89. } else {
  90. res.status(201).json({ 'info': 'meal deleted, id: ', id, meal })
  91. }
  92. });
  93. }
  94. export const deleteAllMealsOfUser = async (userId) => {
  95. await Meal.find({ userId: userId }).sort({ title: 1 }).exec(async (err, meals) => {
  96. if (err) {
  97. console.log('error on finding meals to delete images for user ' + userId);
  98. } else {
  99. await Promise.all(meals.map(async (meal) => {
  100. await deleteAllImagesFromCategoryInternally('mealImages', meal._id);
  101. }));
  102. }
  103. });
  104. Meal.deleteMany({ userId: userId }, {}, function (err) {
  105. if (err) {
  106. console.log('error on delete meals for user ' + userId, err);
  107. } else {
  108. console.log('meals for user ' + userId + ' deleted');
  109. }
  110. });
  111. }