settings.controller.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // logic for settings routes
  2. import Settings from "../models/settings.model.js";
  3. export const getSingleSetting = async (req, res) => {
  4. let id = req.params.id;
  5. try {
  6. const setting = await Settings.findById(id);
  7. res.status(200).json(setting);
  8. } catch (error) {
  9. res.status(404).json({ message: error.message });
  10. }
  11. }
  12. export const getSettingsOfUser = async (req, res) => {
  13. let id = req.params.id;
  14. try {
  15. const setting = await Settings.findOne({ userId: id });
  16. res.status(200).json(setting);
  17. } catch (error) {
  18. res.status(404).json({ message: error.message });
  19. }
  20. }
  21. export const addSettings = async (req, res) => {
  22. const givenSettings = req.body;
  23. const { userId } = givenSettings;
  24. if (userId) {
  25. const newSettings = new Settings(givenSettings);
  26. try {
  27. await newSettings.save();
  28. res.status(201).json({ 'message': 'successfully added new Settings', 'Settings': newSettings });
  29. } catch (error) {
  30. res.status(409).json({ message: error.message });
  31. }
  32. } else {
  33. res.status(409).json({ 'message': 'cannot add settings for undefined userId' });
  34. }
  35. }
  36. export const updateSettings = async (req, res) => {
  37. const newSettings = req.body;
  38. const id = req.params.id;
  39. try {
  40. Settings.findById(id, function (err, settingFound) {
  41. settingFound.set('userId', newSettings.userId);
  42. settingFound.set('contacts', newSettings.contacts);
  43. settingFound.set('language', newSettings.language);
  44. settingFound.set('prefersDarkMode', newSettings.prefersDarkMode);
  45. settingFound.set('contactStartPage', newSettings.contactStartPage);
  46. settingFound.set('mealCategories', newSettings.mealCategories);
  47. settingFound.set('mealTags', newSettings.mealTags);
  48. settingFound.save().then(settingUpdated => {
  49. res.status(201).json({ 'info': 'Settings updated', settingUpdated })
  50. });
  51. });
  52. } catch (error) {
  53. res.status(400).json({ 'info': `Update of setting ${id} failed`, 'message': error.message });
  54. }
  55. }
  56. export const deleteSettings = async (req, res) => {
  57. const id = req.params.id;
  58. Settings.findByIdAndDelete(id, {}, function (err, settings) {
  59. if (err) {
  60. res.status(400).json({ 'info': `Deletion of Settings ${id} failed`, 'message': err.message });
  61. } else {
  62. res.status(201).json({ 'info': 'Settings deleted, id: ', id, settings })
  63. }
  64. });
  65. }
  66. export const deleteSettingOfUser = async (userId) => {
  67. Settings.deleteOne({ userId: userId }, {}, function (err) {
  68. if (err) {
  69. console.log('error on delete settings for user ' + userId, err);
  70. } else {
  71. console.log('settings for user ' + userId + ' deleted');
  72. }
  73. });
  74. }
  75. export const updateUserContacts = async (req, res) => {
  76. const newContacts = req.body;
  77. const id = req.params.id;
  78. try {
  79. const setting = await Settings.findOne({ userId: id });
  80. setting.set('contacts', newContacts);
  81. setting.save().then(settingSaved => {
  82. res.status(201).json({ 'info': 'Updated user contacts', settingSaved })
  83. });
  84. } catch (error) {
  85. res.status(400).json({ 'info': `Update of contacts for user ${id} failed`, 'message': error.message });
  86. }
  87. }
  88. export const updateUserDarkModePreference = async (req, res) => {
  89. const newDarkModePreference = req.body;
  90. let id = req.params.id;
  91. try {
  92. const setting = await Settings.findOne({ userId: id });
  93. setting.set('prefersDarkMode', newDarkModePreference);
  94. setting.save().then(settingSaved => {
  95. res.status(201).json({ 'info': 'Updated user\'s dark mode preference', settingSaved })
  96. });
  97. } catch (error) {
  98. res.status(400).json({ 'info': `Update of dark mode preference for user ${id} failed`, 'message': error.message });
  99. }
  100. }
  101. export const updateUserLanguage = async (req, res) => {
  102. let newLanguage = req.body;
  103. let id = req.params.id;
  104. try {
  105. const setting = await Settings.findOne({ userId: id });
  106. setting.set('language', newLanguage);
  107. setting.save().then(settingSaved => {
  108. res.status(201).json({ 'info': 'Updated user language', settingSaved })
  109. });
  110. } catch (error) {
  111. res.status(400).json({ 'info': `Update of language for user ${id} failed`, 'message': error.message });
  112. }
  113. }
  114. export const updateSingleUserSetting = async (req, res) => {
  115. let newSetting = req.body;
  116. let id = req.params.id;
  117. try {
  118. const setting = await Settings.findOne({ userId: id });
  119. setting.set(newSetting.key, newSetting.value);
  120. setting.save().then(settingSaved => {
  121. res.status(201).json({ 'info': `Updated user ${newSetting.key}`, settingSaved });
  122. });
  123. } catch (error) {
  124. res.status(400).json({ 'info': `Update of ${newSetting.key} for user ${id} failed`, 'message': error.message });
  125. }
  126. }