post.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors')
  4. let { User, Thread, Post } = require('../models')
  5. router.all('*', (req, res, next) => {
  6. if(req.session.loggedIn) {
  7. next()
  8. } else {
  9. res.status(401)
  10. res.json({
  11. errors: [Errors.requestNotAuthorized]
  12. })
  13. }
  14. })
  15. router.post('/', async (req, res) => {
  16. let validationErrors = []
  17. let thread, replyingToPost, post
  18. try {
  19. if(req.body.content === undefined) {
  20. validationErrors.push(Errors.missingParameter('content'))
  21. } else if(typeof req.body.content !== 'string') {
  22. validationErrors.push(Errors.invalidParamterType('content', 'string'))
  23. } if (req.body.threadId === undefined) {
  24. validationErrors.push(Errors.missingParameter('threadId'))
  25. } else if(!Object.isInteger(req.body.threadId)) {
  26. validationErrors.push(Errors.invalidParamterType('threadId', 'integer'))
  27. } if(req.body.replyingToId !== undefined && !Object.isInteger(req.body.replyingToId)) {
  28. validationErrors.push(Errors.invalidParamterType('replyingToId', 'integer'))
  29. }
  30. if(validationErrors) throw Errors.VALIDATION_ERROR
  31. post = await Post.create({
  32. content: req.body.content
  33. })
  34. thread = await Thread.findOne({ where: {
  35. id: req.body.threadId
  36. }})
  37. user = await User.findOne({ where: {
  38. username: req.session.username
  39. }})
  40. if(!thread) throw Errors.invalidParamter('threadId', 'thread does not exist')
  41. await post.setUser(user)
  42. await post.setThread(thread)
  43. await thread.addPost(post)
  44. if(req.body.replyingToId) {
  45. replyingToPost = await Post.findOne({ where: {
  46. id: req.body.replyingToId
  47. }, include: [Thread] })
  48. if(!replyingToPost) {
  49. throw Errors.invalidParamter('replyingToId', 'post does not exist')
  50. } else if(replyingToPost.Thread.id !== thread.id) {
  51. throw Errors.invalidParamter('replyingToId', 'replies must be in same thread')
  52. } else {
  53. await post.setReplyingTo(replyingToPost)
  54. await replyingToPost.addReplies(post)
  55. }
  56. }
  57. res.json(await post.reload({
  58. include: [
  59. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  60. Thread,
  61. { model: Post, as: 'ReplyingTo', include:
  62. [{ model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] }]
  63. }
  64. ]
  65. }))
  66. } catch (e) {
  67. if(e === Errors.VALIDATION_ERROR) {
  68. res.status(400)
  69. res.json({
  70. errors: validationErrors
  71. })
  72. } else if(e.name === 'invalidParamter') {
  73. res.status(400)
  74. res.json({
  75. errors: [e]
  76. })
  77. } else {
  78. res.status(500)
  79. res.json({
  80. errors: [Errors.unknown]
  81. })
  82. }
  83. }
  84. })
  85. module.exports router