thread.js 3.2 KB

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