You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
const router = module.exports = require('express-promise-router')()
|
|
const parseJSON = require('body-parser').json()
|
|
const db = require('../db')
|
|
|
|
const {validationResult} = require('express-validator')
|
|
const validate = require('./middleware/validators')
|
|
|
|
router.get('/', async (req, res) => {
|
|
const categories = await db.category.findAll()
|
|
|
|
res.json(categories)
|
|
})
|
|
|
|
const newCategoryValidators = [
|
|
validate.urlSlugRestrictions,
|
|
validate.nameRestrictions,
|
|
validate.descriptionRestrictions
|
|
]
|
|
router.post('/', parseJSON, newCategoryValidators, async (req, res) => {
|
|
const errors = validationResult(req)
|
|
|
|
if(!errors.isEmpty())
|
|
return res.status(422).json({errors: errors.array()})
|
|
|
|
const category = await db.category.create(
|
|
req.body.name,
|
|
req.body.urlslug,
|
|
req.body.description
|
|
)
|
|
|
|
res.json(category)
|
|
})
|
|
|
|
router.put('/:category_uuid/items/:item_uuid', async (req, res) => {
|
|
const category = await db.category.addItem(req.params.category_uuid, req.params.item_uuid);
|
|
res.json(category)
|
|
})
|
|
|
|
router.delete('/:category_uuid/items/:item_uuid', async (req, res) => {
|
|
const category = await db.category.removeItem(req.params.category_uuid, req.params.item_uuid);
|
|
res.json(category)
|
|
}) |