Skip to content

Commit

Permalink
Add: comments API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun4928 committed Nov 12, 2020
1 parent 4967acd commit 3a79a0a
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 425 deletions.
2 changes: 2 additions & 0 deletions controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const getOneArticle = async (req, res, next) => {
const { articleId } = req.params
const article = await ArticleService.findArticle({ id: articleId })

if (article.deleted_at) return res.status(200).json({ message: 'deleted ' })

res.status(200).json({ article })
} catch (err) {
next(err)
Expand Down
97 changes: 97 additions & 0 deletions controllers/CommentController.js
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,
}
2 changes: 1 addition & 1 deletion controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { AUTH_TOKEN_SALT } = process.env
const bcrypt = require('bcrypt')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const { UserService } = require('../services')
const { errorGenerator } = require('../utils')
Expand Down
2 changes: 2 additions & 0 deletions controllers/index.js
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,
}
Loading

0 comments on commit 3a79a0a

Please sign in to comment.