thread.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. res.json(resThread)
  34. } catch (e) {
  35. if(e.name === 'invalidParameter') {
  36. res.status(400)
  37. res.json({
  38. errors: [e]
  39. })
  40. } else {
  41. console.log(e)
  42. res.status(500)
  43. res.json({
  44. errors: [Errors.unknown]
  45. })
  46. }
  47. }
  48. })
  49. router.all('*', (req, res, next) => {
  50. if(req.session.loggedIn) {
  51. next()
  52. } else {
  53. res.status(401)
  54. res.json({
  55. errors: [Errors.requestNotAuthorized]
  56. })
  57. }
  58. })
  59. router.post('/', async (req, res) => {
  60. let validationErrors = []
  61. try {
  62. if(req.body.name === undefined) {
  63. validationErrors.push(Errors.missingParameter('name'))
  64. } else if(typeof req.body.name !== 'string') {
  65. validationErrors.push(Errors.invalidParameterType('name', 'string'))
  66. }
  67. if(req.body.category === undefined) {
  68. validationErrors.push(Errors.missingParameter('category'))
  69. } else if(typeof req.body.category !== 'string') {
  70. validationErrors.push(Errors.invalidParameterType('category', 'string'))
  71. }
  72. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  73. let category = await Category.findOne({ where: {
  74. name: req.body.category
  75. }})
  76. if(!category) throw Errors.invalidCategory
  77. let user = await User.findOne({ where: {
  78. username: req.session.username
  79. }})
  80. let thread = await Thread.create({
  81. name: req.body.name
  82. })
  83. await thread.setCategory(category)
  84. await thread.setUser(user)
  85. res.json(await thread.reload({
  86. include: [
  87. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  88. Category
  89. ]
  90. }))
  91. } catch (e) {
  92. if(e === Errors.VALIDATION_ERROR) {
  93. res.status(400)
  94. res.json({
  95. errors: validationErrors
  96. })
  97. } else if(e === Errors.invalidCategory) {
  98. res.status(400)
  99. res.json({
  100. errors: [Errors.invalidCategory]
  101. })
  102. } else {
  103. console.log(e)
  104. res.status(500)
  105. res.json({
  106. errors: [Errors.unknown]
  107. })
  108. }
  109. }
  110. })
  111. module.exports = router