category.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors')
  4. let { Category } = require('../models')
  5. router.get('/', async (req, res) => {
  6. try {
  7. let categories = await Category.findAll({
  8. attributes: { exclude: ['id'] },
  9. include: Category.includeOptions(1),
  10. order: ['updatedAt', 'DESC']
  11. })
  12. res.json(categories)
  13. } catch (e) {
  14. res.status(500)
  15. res.json({
  16. errors: [Errors.unknown]
  17. })
  18. }
  19. })
  20. router.get('/:category', async (req, res) => {
  21. try {
  22. let threads
  23. if(req.params.category === 'ALL') {
  24. threads = await Category.findAll({ include: Category.includeOptions() })
  25. } else {
  26. threads = await Category.findOne({
  27. where: { name: req.params.category },
  28. include: Category.includeOptions()
  29. })
  30. }
  31. if(!threads) throw Errors.invalidParameter('id', 'thread does not exist')
  32. if(Array.isArray(threads)) {
  33. let processedThreads = []
  34. threads.forEach(category => {
  35. let jsonCategory = category.toJSON()
  36. processedThreads.push(...jsonCategory.Threads)
  37. })
  38. res.json({
  39. name: 'All',
  40. value: 'ALL',
  41. Threads: processedThreads
  42. })
  43. } else {
  44. res.json(threads.toJSON())
  45. }
  46. } catch (e) {
  47. if(e.name === 'invalidParameter') {
  48. res.status(400)
  49. res.json({
  50. errors: [e]
  51. })
  52. } else {
  53. console.log(e)
  54. res.status(500)
  55. res.json({
  56. errors: [Errors.unknown]
  57. })
  58. }
  59. }
  60. })
  61. router.all('*', (req, res, next) => {
  62. if(!req.session.loggedIn || !req.session.admin) {
  63. res.status(401)
  64. res.json({
  65. errors: [Errors.requestNotAuthorized]
  66. })
  67. } else {
  68. next()
  69. }
  70. })
  71. router.post('/', async (req, res) => {
  72. let validationErrors = []
  73. try {
  74. if(req.body.name === undefined) {
  75. validationErrors.push(Errors.missingParameter('name'))
  76. } else if(typeof req.body.name !== 'string') {
  77. validationErrors.push(Errors.invalidParameterType('name', 'string'))
  78. } else if(!req.body.name.length) {
  79. validationErrors.push(Errors.parameterLengthTooSmall('name', '0'))
  80. }
  81. if(validationErrors.length) throw Errors.VALIDAITON_ERROR
  82. let category = await Category.create({
  83. name: req.body.name
  84. })
  85. res.json(category.toJSON())
  86. } catch (e) {
  87. if(e === Errors.VALIDAITON_ERROR) {
  88. res.status(400)
  89. res.json({
  90. errors: validationErrors
  91. })
  92. } else if(e.name === 'SequelizeUniqueConstraintError') {
  93. res.status(400)
  94. res.json({
  95. errors: [Errors.categoryAlreadyExists]
  96. })
  97. } else {
  98. res.status(500)
  99. res.json({
  100. errors: [Errors.unknown]
  101. })
  102. }
  103. }
  104. })
  105. module.exports = router