-
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
10 changed files
with
227 additions
and
425 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 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,97 @@ | ||
const { CommentService, ArticleService } = require('../services') | ||
const { errorGenerator } = require('../utils') | ||
|
||
const getComments = async (req, res, next) => { | ||
try { | ||
const { articleId } = req.params | ||
|
||
const comments = await CommentService.findCommentsOfArticle({ articleId }) | ||
|
||
res.status(200).json({ comments }) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
const postOneComment = async (req, res, next) => { | ||
try { | ||
const { articleId } = req.params | ||
const { body } = req.body | ||
const { id: userId } = req.foundUser | ||
|
||
if (!body) errorGenerator({ message: 'invalud input', statusCode: 400 }) | ||
|
||
const createdComment = await CommentService.createCommentOfArticle({ | ||
articleId, | ||
userId, | ||
body, | ||
}) | ||
|
||
res.status(201).json({ createdComment }) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
const updateOneComment = async (req, res, next) => { | ||
try { | ||
const { commentId, articleId } = req.params | ||
const { id: userIdFromToken } = req.foundUser | ||
const { body } = req.body | ||
|
||
const foundArticle = await ArticleService.findArticle({ id: articleId }) | ||
|
||
if (userIdFromToken !== foundArticle.user_id) | ||
errorGenerator({ message: 'unauthorized', statusCode: 403 }) | ||
|
||
const isCommentIncluded = foundArticle.comments.some( | ||
({ id }) => id === Number(commentId) | ||
) | ||
|
||
if (!isCommentIncluded) | ||
errorGenerator({ message: 'comment not in the article', statusCode: 400 }) | ||
|
||
const updatedComment = await CommentService.updateCommentOfArticle({ | ||
commentId, | ||
body, | ||
}) | ||
|
||
res.status(201).json({ updatedComment }) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
const deleteOneComment = async (req, res, next) => { | ||
try { | ||
const { commentId, articleId: id } = req.params | ||
const { id: userIdFromToken } = req.foundUser | ||
|
||
const foundArticle = await ArticleService.findArticle({ id }) | ||
|
||
if (userIdFromToken !== foundArticle.user_id) | ||
errorGenerator({ message: 'unauthorized', statusCode: 403 }) | ||
|
||
const isCommentIncluded = foundArticle.comments.some( | ||
({ id }) => id === Number(commentId) | ||
) | ||
|
||
if (!isCommentIncluded) | ||
errorGenerator({ message: 'comment not in the article', statusCode: 400 }) | ||
|
||
const deletedComment = await CommentService.deleteCommentOfArticle({ | ||
commentId, | ||
}) | ||
|
||
res.status(201).json({ deletedComment }) | ||
} catch (err) { | ||
next(err) | ||
} | ||
} | ||
|
||
module.exports = { | ||
getComments, | ||
postOneComment, | ||
updateOneComment, | ||
deleteOneComment, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const UserController = require('./UserController') | ||
const ArticleController = require('./ArticleController') | ||
const CommentController = require('./CommentController') | ||
|
||
module.exports = { | ||
UserController, | ||
ArticleController, | ||
CommentController, | ||
} |
Oops, something went wrong.