Skip to content

Commit

Permalink
refactor : 자신의 Goal에 달린 Comment 삭제를 허용하도록 변경 (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-ho committed Apr 8, 2024
1 parent 78b7956 commit 70b35ac
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class CommentService(
@Transactional
fun isNewComment(goalId: Long): Boolean {
val goal = goalRepository.getById(goalId)
return commentRepository.existsByGoalIdAndCreatedAtGreaterThan(goal.id!!, goal.lastCommentReadAt)
return commentRepository.existsByGoalIdAndCreatedAtGreaterThan(
goalId = goal.id!!,
createdAt = goal.lastCommentReadAt,
)
}

@Transactional
Expand All @@ -67,12 +70,12 @@ class CommentService(
@Transactional
fun delete(commentId: Long, currentUserId: Long) {
val comment = commentRepository.getById(commentId)
if (currentUserId != comment.commenter.id) {

if (isNotMyComment(comment, currentUserId) && isNotMyGoal(comment.goal, currentUserId)) {
throw CoreApiException(ErrorInfo.RESOURCE_DELETE_FORBIDDEN)
}

commentRepository.delete(comment)

applicationEventPublisher.publishEvent(DeletedCommentEvent(comment.goal.id!!))
}

Expand All @@ -88,6 +91,11 @@ class CommentService(
}
}

private fun isNotMyComment(
comment: Comment,
currentUserId: Long,
) = currentUserId != comment.commenter.id

private fun isMyGoal(goalId: Long, userId: Long): Boolean {
val goal = goalRepository.getById(goalId)
return isMyGoal(goal, userId)
Expand All @@ -96,6 +104,8 @@ class CommentService(
private fun isMyGoal(goal: Goal, userId: Long): Boolean =
userId == goal.lifeMap.user.id

private fun isNotMyGoal(goal: Goal, userId: Long): Boolean = !isMyGoal(goal, userId)

private fun publishUpdateCommentReadAtEvent(goalId: Long) {
val event = CommentReadEvent(goalId, LocalDateTime.now())
applicationEventPublisher.publishEvent(event)
Expand Down

0 comments on commit 70b35ac

Please sign in to comment.