123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- let express = require('express')
- let router = express.Router()
- const Errors = require('../lib/errors')
- let pagination = require('../lib/pagination')
- let { Category, Post, Thread, User } = require('../models')
- router.get('/', async (req, res) => {
- try {
- let categories = await Category.findAll()
- res.json(categories)
- } catch (e) {
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
-
- })
- router.get('/:category', async (req, res) => {
- function concatenateThreads(threads) {
- let processedThreads = []
-
- threads.forEach(category => {
- let jsonCategory = category.toJSON()
- processedThreads.push(...jsonCategory.Threads)
- })
- return processedThreads
- }
- try {
- let threads, threadsLatestPost, resThreads, user
- let { from, limit } = pagination.getPaginationProps(req.query, true)
- if(req.query.username) {
- user = await User.findOne({ where: { username: req.query.username }})
- }
- function threadInclude(order) {
- let options = {
- model: Thread,
- order: [['id', 'DESC']],
- limit,
- where: {},
- include: [
- Category,
- { model: User, attributes: ['username', 'createdAt', 'id', 'color'] },
- {
- model: Post, limit: 1, order: [['id', order]], include:
- [{ model: User, attributes: ['username', 'id'] }]
- }
- ]
- }
- if(user) {
- options.where.userId = user.id
- }
- if(from !== null) {
- options.where.id = { $lte: from }
- }
- return [options]
- }
- if(req.params.category === 'ALL') {
- threads = await Category.findAll({ include: threadInclude('ASC') })
- threadsLatestPost = await Category.findAll({ include: threadInclude('DESC') })
- } else {
- threads = await Category.findOne({
- where: { name: req.params.category },
- include: threadInclude('ASC')
- })
- threadsLatestPost = await Category.findOne({
- where: { name: req.params.category },
- include: threadInclude('DESC')
- })
- }
- if(!threads) throw Errors.invalidParameter('id', 'thread does not exist')
-
- if(Array.isArray(threads)) {
- resThreads = {
- name: 'All',
- value: 'ALL',
- Threads: concatenateThreads(threads),
- meta: {}
- }
- threadsLatestPost = { Threads: concatenateThreads(threadsLatestPost) }
- } else {
- resThreads = threads.toJSON()
- resThreads.meta = {}
- }
- threadsLatestPost.Threads.forEach((thread, i) => {
- let first = resThreads.Threads[i].Posts[0]
- let latest = thread.Posts[0]
- if(first.id === latest.id) return
- resThreads.Threads[i].Posts.push(latest)
- })
- let nextId = await pagination.getNextIdDesc(Thread, user ? { userId: user.id } : {}, resThreads.Threads)
- if(nextId) {
- resThreads.meta.nextURL =
- `/api/v1/category/${req.params.category}?&limit=${limit}&from=${nextId - 1}`
- if(user) {
- resThreads.meta.nextURL += '&username=' + user.username
- }
- resThreads.meta.nextThreadsCount = await pagination.getNextCount(
- Thread, resThreads.Threads, limit,
- user ? { userId: user.id } : {}
- )
- } else {
- resThreads.meta.nextURL = null
- resThreads.meta.nextThreadsCount = 0
- }
- res.json(resThreads)
- } 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 || !req.session.admin) {
- res.status(401)
- res.json({
- errors: [Errors.requestNotAuthorized]
- })
- } else {
- next()
- }
- })
- 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'))
- } else if(!req.body.name.length) {
- validationErrors.push(Errors.parameterLengthTooSmall('name', '0'))
- }
- if(validationErrors.length) throw Errors.VALIDAITON_ERROR
- let category = await Category.create({
- name: req.body.name
- })
- res.json(category.toJSON())
- } catch (e) {
- if(e === Errors.VALIDAITON_ERROR) {
- res.status(400)
- res.json({
- errors: validationErrors
- })
- } else if(e.name === 'SequelizeUniqueConstraintError') {
- res.status(400)
- res.json({
- errors: [Errors.categoryAlreadyExists]
- })
- } else {
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
- }
- })
- module.exports = router
|