소스 검색

Add category route

sbkwgh 8 년 전
부모
커밋
6f9bb726b4
1개의 변경된 파일71개의 추가작업 그리고 0개의 파일을 삭제
  1. 71 0
      routes/category.js

+ 71 - 0
routes/category.js

@@ -0,0 +1,71 @@
+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.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'))
+		}
+
+		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