123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- let express = require('express')
- let router = express.Router()
- const Errors = require('../lib/errors.js')
- let { User, Thread, Category, Post } = require('../models')
- let pagination = require('../lib/pagination.js')
- router.get('/:thread_id', async (req, res) => {
- try {
- let { lastId, limit, previousId } = pagination.getPaginationProps(req.query)
- let thread, resThread
- if(+req.query.postId) {
- let findObj = {
- limit: Math.floor(limit / 2),
- order: [['id', 'ASC']],
- include: Post.includeOptions()
- }
- thread = await Thread.findById(req.params.thread_id, {
- include: [
- { model: User, attributes: ['username', 'createdAt', 'color', 'updatedAt', 'id'] },
- Category,
- ]
- })
- if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
- resThread = thread.toJSON()
- let postsAfter = await Post.findAll(Object.assign({}, findObj, {
- where: {
- id: { $gt: +req.query.postId },
- threadId: req.params.thread_id,
- },
- }))
- let postsBefore = await Post.findAll(Object.assign({}, findObj, {
- where: {
- id: { $lte: +req.query.postId },
- threadId: req.params.thread_id,
- },
- order: [['id', 'DESC']],
- }))
- resThread.Posts = postsBefore
- .concat(postsAfter)
- .map(p => p.toJSON())
- .sort((a, b) => a.id - b.id)
- } else {
- thread = await Thread.findById(req.params.thread_id, {
- include: Thread.includeOptions(lastId, limit, previousId)
- })
- if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
- resThread = thread.toJSON()
- if(previousId) {
- resThread.Posts = resThread.Posts.sort((a, b) => a.id - b.id)
- }
- }
- resThread.meta = {}
-
- let nextId = await pagination.getNextId(
- Post,
- { threadId: +req.params.thread_id },
- resThread.Posts
- )
- let beforeId = await pagination.getPreviousId(
- Post,
- { threadId: +req.params.thread_id },
- resThread.Posts,
- limit
- )
- if(nextId) {
- resThread.meta.nextURL =
- `/api/v1/thread/${thread.id}?limit=${limit}&lastId=${nextId}`
- } else {
- resThread.meta.nextURL = null
- }
- if(beforeId) {
- resThread.meta.previousURL =
- `/api/v1/thread/${thread.id}?limit=${limit}&previousId=${beforeId}`
- } else {
- resThread.meta.previousURL = null
- }
- res.json(resThread)
-
- } catch (e) {
- if(e.name === 'invalidParameter') {
- res.status(400)
- res.json({
- errors: [e]
- })
- } else {
- console.log(e)
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
- }
- })
- router.all('*', (req, res, next) => {
- if(req.session.loggedIn) {
- next()
- } else {
- res.status(401)
- res.json({
- errors: [Errors.requestNotAuthorized]
- })
- }
- })
- router.post('/', async (req, res) => {
- let validationErrors = []
- try {
- if(req.body.name === undefined) {
- validationErrors.push(Errors.missingParameter('name'))
- } else if(typeof req.body.name !== 'string') {
- validationErrors.push(Errors.invalidParameterType('name', 'string'))
- }
- if(req.body.category === undefined) {
- validationErrors.push(Errors.missingParameter('category'))
- } else if(typeof req.body.category !== 'string') {
- validationErrors.push(Errors.invalidParameterType('category', 'string'))
- }
- if(validationErrors.length) throw Errors.VALIDATION_ERROR
- let category = await Category.findOne({ where: {
- name: req.body.category
- }})
- if(!category) throw Errors.invalidCategory
- let user = await User.findOne({ where: {
- username: req.session.username
- }})
- let thread = await Thread.create({
- name: req.body.name
- })
- await thread.setCategory(category)
- await thread.setUser(user)
- res.json(await thread.reload({
- include: [
- { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
- Category
- ]
- }))
- } catch (e) {
- if(e === Errors.VALIDATION_ERROR) {
- res.status(400)
- res.json({
- errors: validationErrors
- })
- } else if(e === Errors.invalidCategory) {
- res.status(400)
- res.json({
- errors: [Errors.invalidCategory]
- })
- } else {
- console.log(e)
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
- }
- })
- module.exports = router
|