thread.js 2.5 KB

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