apps.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const asyncWrapper = require('../middleware/asyncWrapper');
  2. const ErrorResponse = require('../utils/ErrorResponse');
  3. const App = require('../models/App');
  4. const Config = require('../models/Config');
  5. // @desc Create new app
  6. // @route POST /api/apps
  7. // @access Public
  8. exports.createApp = asyncWrapper(async (req, res, next) => {
  9. // Get config from database
  10. const pinApps = await Config.findOne({
  11. where: { key: 'pinAppsByDefault' }
  12. });
  13. let app;
  14. if (pinApps) {
  15. if (parseInt(pinApps.value)) {
  16. app = await App.create({
  17. ...req.body,
  18. isPinned: true
  19. })
  20. } else {
  21. app = await App.create(req.body);
  22. }
  23. }
  24. res.status(201).json({
  25. success: true,
  26. data: app
  27. })
  28. })
  29. // @desc Get all apps
  30. // @route GET /api/apps
  31. // @access Public
  32. exports.getApps = asyncWrapper(async (req, res, next) => {
  33. const apps = await App.findAll({
  34. order: [['name', 'ASC']]
  35. });
  36. res.status(200).json({
  37. success: true,
  38. data: apps
  39. })
  40. })
  41. // @desc Get single app
  42. // @route GET /api/apps/:id
  43. // @access Public
  44. exports.getApp = asyncWrapper(async (req, res, next) => {
  45. const app = await App.findOne({
  46. where: { id: req.params.id }
  47. });
  48. if (!app) {
  49. return next(new ErrorResponse(`App with id of ${req.params.id} was not found`, 404));
  50. }
  51. res.status(200).json({
  52. success: true,
  53. data: app
  54. })
  55. })
  56. // @desc Update app
  57. // @route PUT /api/apps/:id
  58. // @access Public
  59. exports.updateApp = asyncWrapper(async (req, res, next) => {
  60. let app = await App.findOne({
  61. where: { id: req.params.id }
  62. });
  63. if (!app) {
  64. return next(new ErrorResponse(`App with id of ${req.params.id} was not found`, 404));
  65. }
  66. app = await app.update({ ...req.body });
  67. res.status(200).json({
  68. success: true,
  69. data: app
  70. })
  71. })
  72. // @desc Delete app
  73. // @route DELETE /api/apps/:id
  74. // @access Public
  75. exports.deleteApp = asyncWrapper(async (req, res, next) => {
  76. await App.destroy({
  77. where: { id: req.params.id }
  78. })
  79. res.status(200).json({
  80. success: true,
  81. data: {}
  82. })
  83. })