settings.controller.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // logic for 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. console.log(setting);
  8. res.status(200).json(setting);
  9. } catch (error) {
  10. res.status(404).json({ message: error.message });
  11. }
  12. }
  13. export const getSettingsOfUser = async (req, res) => {
  14. let id = req.params.id;
  15. try {
  16. const setting = await Settings.findOne({ userId: id });
  17. console.log('delivering', setting);
  18. res.status(200).json(setting);
  19. } catch (error) {
  20. res.status(404).json({ message: error.message });
  21. }
  22. }
  23. export const addSettings = async (req, res) => {
  24. console.log('trying to add the following Settings', req.body);
  25. const givenSettings = req.body;
  26. const newSettings = new Settings(givenSettings);
  27. console.log(newSettings);
  28. try {
  29. console.log('starting await');
  30. await newSettings.save();
  31. console.log('after await')
  32. res.status(201).json({ 'message': 'successfully added new Settings', 'Settings': newSettings });
  33. } catch (error) {
  34. res.status(409).json({ message: error.message });
  35. }
  36. }
  37. export const updateSettings = async (req, res) => {
  38. let newSettings = req.body;
  39. let id = req.params.id;
  40. try {
  41. Settings.findById(id, function (err, Settings) {
  42. Settings.set('userId', newSettings.userId);
  43. Settings.set('language', newSettings.language);
  44. Settings.save().then(Settings => {
  45. res.status(201).json({ 'info': 'Settings updated', Settings })
  46. });
  47. });
  48. } catch (error) {
  49. res.status(400).json({ 'info': `Update of Settings ${id} failed`, 'message': error.message });
  50. }
  51. }
  52. export const deleteSettings = async (req, res) => {
  53. let id = req.params.id;
  54. Settings.findByIdAndDelete(id, {}, function (err, Settings) {
  55. if (err) {
  56. res.status(400).json({ 'info': `Deletion of Settings ${id} failed`, 'message': err.message });
  57. } else {
  58. res.status(201).json({ 'info': 'Settings deleted, id: ', id, Settings })
  59. }
  60. });
  61. }