category.js 2.8 KB

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