category.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const asyncWrapper = require('../middleware/asyncWrapper');
  2. const ErrorResponse = require('../utils/ErrorResponse');
  3. const Category = require('../models/Category');
  4. const Bookmark = require('../models/Bookmark');
  5. const Config = require('../models/Config');
  6. const { Sequelize } = require('sequelize')
  7. // @desc Create new category
  8. // @route POST /api/categories
  9. // @access Public
  10. exports.createCategory = asyncWrapper(async (req, res, next) => {
  11. // Get config from database
  12. const pinCategories = await Config.findOne({
  13. where: { key: 'pinCategoriesByDefault' }
  14. });
  15. let category;
  16. if (pinCategories) {
  17. if (parseInt(pinCategories.value)) {
  18. category = await Category.create({
  19. ...req.body,
  20. isPinned: true
  21. })
  22. } else {
  23. category = await Category.create(req.body);
  24. }
  25. }
  26. res.status(201).json({
  27. success: true,
  28. data: category
  29. })
  30. })
  31. // @desc Get all categories
  32. // @route GET /api/categories
  33. // @access Public
  34. exports.getCategories = asyncWrapper(async (req, res, next) => {
  35. const categories = await Category.findAll({
  36. include: [{
  37. model: Bookmark,
  38. as: 'bookmarks'
  39. }],
  40. order: [[ Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC' ]]
  41. });
  42. res.status(200).json({
  43. success: true,
  44. data: categories
  45. })
  46. })
  47. // @desc Get single category
  48. // @route GET /api/categories/:id
  49. // @access Public
  50. exports.getCategory = asyncWrapper(async (req, res, next) => {
  51. const category = await Category.findOne({
  52. where: { id: req.params.id },
  53. include: [{
  54. model: Bookmark,
  55. as: 'bookmarks'
  56. }]
  57. });
  58. if (!category) {
  59. return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
  60. }
  61. res.status(200).json({
  62. success: true,
  63. data: category
  64. })
  65. })
  66. // @desc Update category
  67. // @route PUT /api/categories/:id
  68. // @access Public
  69. exports.updateCategory = asyncWrapper(async (req, res, next) => {
  70. let category = await Category.findOne({
  71. where: { id: req.params.id }
  72. });
  73. if (!category) {
  74. return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
  75. }
  76. category = await category.update({ ...req.body });
  77. res.status(200).json({
  78. success: true,
  79. data: category
  80. })
  81. })
  82. // @desc Delete category
  83. // @route DELETE /api/categories/:id
  84. // @access Public
  85. exports.deleteCategory = asyncWrapper(async (req, res, next) => {
  86. const category = await Category.findOne({
  87. where: { id: req.params.id },
  88. include: [{
  89. model: Bookmark,
  90. as: 'bookmarks'
  91. }]
  92. });
  93. if (!category) {
  94. return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
  95. }
  96. category.bookmarks.forEach(async (bookmark) => {
  97. await Bookmark.destroy({
  98. where: { id: bookmark.id }
  99. })
  100. })
  101. await Category.destroy({
  102. where: { id: req.params.id }
  103. })
  104. res.status(200).json({
  105. success: true,
  106. data: {}
  107. })
  108. })