Browse Source

Move post id error to class method on model

sbkwgh 8 years ago
parent
commit
51a31918ca
2 changed files with 11 additions and 10 deletions
  1. 10 0
      models/report.js
  2. 1 10
      routes/report.js

+ 10 - 0
models/report.js

@@ -15,6 +15,16 @@ module.exports = (sequelize, DataTypes) => {
 			associate (models) {
 				Report.hasOne(models.User, { as: 'FlaggedByUser' })
 				Report.hasOne(models.Post)
+			},
+			InvalidPostId (value) {
+				return new sequelize.ValidationError('Post id is not valid', [
+					new sequelize.ValidationErrorItem(
+						'Post id is not valid',
+						'Validation error',
+						'postId',
+						value
+					)
+				])
 			}
 		}
 	})

+ 1 - 10
routes/report.js

@@ -18,16 +18,7 @@ router.post('/', async (req, res) => {
 	try {
 		let post = await Post.findById(req.body.postId)
 
-		if(!post) {
-			throw new Sequelize.ValidationError('Post id is not valid', [
-				new Sequelize.ValidationErrorItem(
-					'Post id is not valid',
-					'Validation error',
-					'postId',
-					req.body.postId
-				)
-			])
-		}
+		if(!post) throw Report.InvalidPostId(req.body.postId)
 
 		let user = await User.findOne({
 			where: { username: req.session.username }