123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- let express = require('express')
- let router = express.Router()
- const Errors = require('../lib/errors')
- let { Category } = require('../models')
- router.get('/', async (req, res) => {
- try {
- let categories = await Category.findAll({
- attributes: { exclude: ['id'] }
- })
- res.json(categories)
- } catch (e) {
- res.status(500)
- res.json({
- errors: [Errors.unknown]
- })
- }
-
- })
- router.get('/:category', async (req, res) => {
- try {
- let threads = await Category.findOne({
- where: { name: req.params.category },
- include: Category.includeOptions()
- })
- if(!threads) throw Errors.invalidParameter('id', 'thread does not exist')
-
- res.json(threads.toJSON())
- } 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
|