apps.js 2.1 KB

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