This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
274 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import models from '../models'; | ||
|
||
const { Article, User, Comment } = models; | ||
|
||
const validateResourceId = (req, res, next) => { | ||
const { | ||
articleId, | ||
userId, | ||
commentId, | ||
} = req.params; | ||
|
||
const validate = async (type, id, model) => { | ||
if (Number.isNaN(Number(id))) { | ||
// articleId passed in is not a number | ||
const nanError = new Error(`invalid id, ${type} id must be a number`); | ||
nanError.status = 400; | ||
return next(nanError); | ||
} | ||
|
||
let resource; | ||
try { | ||
resource = await model.findByPk(id); | ||
} catch (err) { | ||
next(err); | ||
} | ||
|
||
if (!resource) { | ||
// checks if resource exists | ||
const notFoundError = new Error(`Sorry, that ${type} was not found`); | ||
notFoundError.status = 404; | ||
return next(notFoundError); | ||
} | ||
}; | ||
|
||
if (articleId) { | ||
// if article Id param exists | ||
validate('article', articleId, Article); | ||
} | ||
if (userId) { | ||
// if user Id param exists | ||
validate('user', userId, User); | ||
} | ||
if (commentId) { | ||
// if comment Id param exists | ||
validate('comment', commentId, Comment); | ||
} | ||
next(); | ||
}; | ||
export default validateResourceId; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters