Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[πŸ› οΈ Refactor] Goal 주인이 νƒ€μΈμ˜ λŒ“κΈ€μ„ μ§€μš°μ§€ λͺ»ν•˜λŠ” 문제 μˆ˜μ • (#251) #252

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading