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