thread.js 2.6 KB

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