thread.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors.js')
  4. let { User, Thread, Category, Post } = require('../models')
  5. let pagination = require('../lib/pagination.js')
  6. router.get('/:thread_id', async (req, res) => {
  7. try {
  8. let { lastId, limit } = pagination.getPaginationProps(req.query)
  9. let thread = await Thread.findById(req.params.thread_id, {
  10. include: Thread.includeOptions(lastId, limit)
  11. })
  12. if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
  13. let resThread = thread.toJSON()
  14. resThread.meta = {}
  15. let nextId = await pagination.getNextId(
  16. Post,
  17. { threadId: +req.params.thread_id },
  18. resThread.Posts
  19. )
  20. let previousId = await pagination.getPreviousId(
  21. Post,
  22. { threadId: +req.params.thread_id },
  23. resThread.Posts,
  24. limit
  25. )
  26. if(nextId) {
  27. resThread.meta.nextURL =
  28. `/api/v1/thread/${thread.id}?limit=${limit}&lastId=${nextId}`
  29. } else {
  30. resThread.meta.nextURL = null
  31. }
  32. if(previousId) {
  33. resThread.meta.previousURL =
  34. `/api/v1/thread/${thread.id}?limit=${limit}&lastId=${previousId}`
  35. } else {
  36. resThread.meta.previousURL = null
  37. }
  38. res.json(resThread)
  39. } catch (e) {
  40. if(e.name === 'invalidParameter') {
  41. res.status(400)
  42. res.json({
  43. errors: [e]
  44. })
  45. } else {
  46. console.log(e)
  47. res.status(500)
  48. res.json({
  49. errors: [Errors.unknown]
  50. })
  51. }
  52. }
  53. })
  54. router.all('*', (req, res, next) => {
  55. if(req.session.loggedIn) {
  56. next()
  57. } else {
  58. res.status(401)
  59. res.json({
  60. errors: [Errors.requestNotAuthorized]
  61. })
  62. }
  63. })
  64. router.post('/', async (req, res) => {
  65. let validationErrors = []
  66. try {
  67. if(req.body.name === undefined) {
  68. validationErrors.push(Errors.missingParameter('name'))
  69. } else if(typeof req.body.name !== 'string') {
  70. validationErrors.push(Errors.invalidParameterType('name', 'string'))
  71. }
  72. if(req.body.category === undefined) {
  73. validationErrors.push(Errors.missingParameter('category'))
  74. } else if(typeof req.body.category !== 'string') {
  75. validationErrors.push(Errors.invalidParameterType('category', 'string'))
  76. }
  77. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  78. let category = await Category.findOne({ where: {
  79. name: req.body.category
  80. }})
  81. if(!category) throw Errors.invalidCategory
  82. let user = await User.findOne({ where: {
  83. username: req.session.username
  84. }})
  85. let thread = await Thread.create({
  86. name: req.body.name
  87. })
  88. await thread.setCategory(category)
  89. await thread.setUser(user)
  90. res.json(await thread.reload({
  91. include: [
  92. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  93. Category
  94. ]
  95. }))
  96. } catch (e) {
  97. if(e === Errors.VALIDATION_ERROR) {
  98. res.status(400)
  99. res.json({
  100. errors: validationErrors
  101. })
  102. } else if(e === Errors.invalidCategory) {
  103. res.status(400)
  104. res.json({
  105. errors: [Errors.invalidCategory]
  106. })
  107. } else {
  108. console.log(e)
  109. res.status(500)
  110. res.json({
  111. errors: [Errors.unknown]
  112. })
  113. }
  114. }
  115. })
  116. module.exports = router