category.js 3.1 KB

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