thread.js 2.7 KB

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