Skip to content

Commit

Permalink
refactor: 댓글 & 대댓글 관련 코드를 리팩토링한다
Browse files Browse the repository at this point in the history
  • Loading branch information
zkxmdkdltm authored and devbelly committed Jul 6, 2024
1 parent b9bb5f4 commit 1269c06
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dclass.backend.application

import com.dclass.backend.application.dto.CommentReplyResponse
import com.dclass.backend.application.dto.CommentReplyWithUserResponse
import com.dclass.backend.application.dto.CommentResponse
import com.dclass.backend.application.dto.CommentScrollPageRequest
Expand All @@ -8,15 +9,18 @@ import com.dclass.backend.application.dto.CreateCommentRequest
import com.dclass.backend.application.dto.DeleteCommentRequest
import com.dclass.backend.application.dto.LikeCommentRequest
import com.dclass.backend.application.dto.UpdateCommentRequest
import com.dclass.backend.domain.anonymous.Anonymous
import com.dclass.backend.domain.anonymous.AnonymousRepository
import com.dclass.backend.domain.comment.CommentRepository
import com.dclass.backend.domain.comment.getByIdOrThrow
import com.dclass.backend.domain.community.CommunityRepository
import com.dclass.backend.domain.community.findByIdOrThrow
import com.dclass.backend.domain.notification.NotificationEvent
import com.dclass.backend.domain.post.Post
import com.dclass.backend.domain.post.PostRepository
import com.dclass.backend.domain.post.findByIdOrThrow
import com.dclass.backend.domain.reply.ReplyRepository
import com.dclass.backend.domain.userblock.UserBlock
import com.dclass.backend.domain.userblock.UserBlockRepository
import com.dclass.backend.exception.comment.CommentException
import com.dclass.backend.exception.comment.CommentExceptionType
Expand Down Expand Up @@ -49,7 +53,7 @@ class CommentService(
val post = postRepository.findByIdOrThrow(request.postId)
val community = communityRepository.findByIdOrThrow(post.communityId)
commentValidator.validate(userId, community)
val comment = commentRepository.save(request.toEntity(userId, post.userId == userId))
val comment = commentRepository.save(request.toEntity(userId))

if (request.isAnonymous && !anonymousRepository.existsByUserIdAndPostId(userId, post.id)) {
anonymousRepository.save(request.toAnonymousEntity(userId, post.id))
Expand Down Expand Up @@ -101,23 +105,19 @@ class CommentService(
val blockedUserIds =
userBlockRepository.findByBlockerUserId(userId).associateBy { it.blockedUserId }
val anonymousList = anonymousRepository.findByPostId(request.postId)
val post = postRepository.findByIdOrThrow(request.postId)

val commentIds = comments.map { it.id }

val anonymousIndexMap = anonymousList.mapIndexed { index, anon -> anon.userId to index }.toMap()

comments.forEach {
it.isLiked = it.likeCount.findUserById(userId)
it.isBlockedUser = blockedUserIds.contains(it.userId)
val index = anonymousIndexMap[it.userId] ?: -1
it.userInformation.nickname = determineNickname(it.isOwner, it.isAnonymous, it.userInformation.nickname, index)
updateUserInfo(it, blockedUserIds, anonymousList, post)
}

val replies = replyRepository.findRepliesWithUserByCommentIdIn(commentIds)
.onEach {
it.isBlockedUser = blockedUserIds.contains(it.userId)
val index = anonymousIndexMap[it.userId] ?: -1
it.userInformation.nickname = determineNickname(it.isOwner, it.isAnonymous, it.userInformation.nickname, index)
updateUserInfo(it, blockedUserIds, anonymousList, post)
}.groupBy { it.commentId }


Expand All @@ -130,6 +130,18 @@ class CommentService(
return CommentsResponse.of(data, request.size)
}

private fun updateUserInfo(
it: CommentReplyResponse,
blockedUserIds: Map<Long, UserBlock>,
anonymousList: List<Anonymous>,
post: Post
) {
it.isBlockedUser = blockedUserIds.contains(it.userId)
val index = anonymousList.indexOfFirst { anon -> anon.userId == it.userId }
val isOwner = it.userId == post.userId
it.userInformation.nickname = determineNickname(isOwner, it.isAnonymous, it.userInformation.nickname, index)
}

private fun determineNickname(
isOwner: Boolean,
isAnonymous: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ReplyService(

replyValidator.validate(userId, community.departmentId)

val reply = replyRepository.save(request.toEntity(userId, post.userId == userId))
val reply = replyRepository.save(request.toEntity(userId))

if (request.isAnonymous && !anonymousRepository.existsByUserIdAndPostId(userId, post.id)) {
anonymousRepository.save(request.toAnonymousEntity(userId, post.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ data class CreateCommentRequest(
)
val isAnonymous: Boolean = false,
) {
fun toEntity(userId: Long, isOwner: Boolean): Comment {
return Comment(userId, postId, content, isAnonymous, isOwner)
fun toEntity(userId: Long): Comment {
return Comment(userId, postId, content, isAnonymous)
}

fun toAnonymousEntity(userId: Long, postId: Long): Anonymous {
Expand Down Expand Up @@ -78,6 +78,14 @@ data class LikeCommentRequest(
val commentId: Long,
)

interface CommentReplyResponse {
val userId: Long
var isBlockedUser: Boolean
val isAnonymous: Boolean
val userInformation: UserInformation
}


data class CommentResponse(
@Schema(
description = "댓글의 고유 식별자",
Expand Down Expand Up @@ -109,11 +117,6 @@ data class CommentResponse(
)
val isAnonymous: Boolean = false,

@Schema(
description = "댓글 작성자와 글 작성자가 같은지 여부",
example = "false",
)
val isOwner: Boolean = false,

@Schema(
description = "댓글이 작성된 시각",
Expand All @@ -133,7 +136,6 @@ data class CommentResponse(
comment.postId,
comment.content,
comment.isAnonymous,
comment.isOwner,
comment.createdDateTime,
comment.modifiedDateTime,
)
Expand All @@ -150,13 +152,13 @@ data class CommentWithUserResponse(
description = "댓글을 작성한 유저의 정보",
example = "1",
)
val userInformation: UserInformation,
override val userInformation: UserInformation,

@Schema(
description = "댓글을 작성한 유저의 고유 id",
example = "1",
)
val userId: Long,
override val userId: Long,

@Schema(
description = "댓글이 달린 게시글의 고유 식별자",
Expand Down Expand Up @@ -192,26 +194,20 @@ data class CommentWithUserResponse(
description = "차단된 사용자 여부",
example = "true",
)
var isBlockedUser: Boolean,
override var isBlockedUser: Boolean,

@Schema(
description = "익명 여부",
example = "false",
)
val isAnonymous: Boolean = false,

@Schema(
description = "댓글 작성자와 글 작성자가 같은지 여부",
example = "false",
)
val isOwner: Boolean = false,
override val isAnonymous: Boolean = false,

@Schema(
description = "댓글이 작성된 시각",
example = "2021-08-01T00:00:00",
)
val createdAt: LocalDateTime,
) {
) : CommentReplyResponse {
constructor(comment: Comment, user: User) : this(
id = comment.id,
userInformation = UserInformation(user.name, user.email, user.nickname),
Expand All @@ -222,7 +218,6 @@ data class CommentWithUserResponse(
deleted = comment.isDeleted(),
isLiked = false,
isBlockedUser = false,
isOwner = comment.isOwner,
isAnonymous = comment.isAnonymous,
createdAt = comment.createdDateTime,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ data class CreateReplyRequest(
)
val isAnonymous: Boolean = false,
) {
fun toEntity(userId: Long, isOwner: Boolean): Reply {
return Reply(userId, commentId, content, isAnonymous, isOwner)
fun toEntity(userId: Long): Reply {
return Reply(userId, commentId, content, isAnonymous)
}

fun toAnonymousEntity(userId: Long, postId: Long): Anonymous {
Expand Down Expand Up @@ -145,12 +145,12 @@ data class ReplyWithUserResponse(
description = "대댓글을 작성한 유저의 고유 식별자",
example = "1",
)
val userId: Long,
override val userId: Long,

@Schema(
description = "대댓글을 작성한 유저의 정보",
)
val userInformation: UserInformation,
override val userInformation: UserInformation,

@Schema(
description = "대댓글이 달린 댓글의 고유 식별자",
Expand All @@ -168,7 +168,7 @@ data class ReplyWithUserResponse(
description = "익명 여부",
example = "false",
)
val isAnonymous: Boolean,
override val isAnonymous: Boolean,

@Schema(
description = "대댓글의 좋아요 수",
Expand All @@ -180,28 +180,21 @@ data class ReplyWithUserResponse(
description = "차단한 사용자 여부",
example = "false",
)
var isBlockedUser: Boolean = false,

@Schema(
description = "대댓글 작성자와 글 작성자가 같은지 여부",
example = "false",
)
val isOwner: Boolean = false,
override var isBlockedUser: Boolean = false,

@Schema(
description = "대댓글이 작성된 시각",
example = "2021-08-01T00:00:00",
)
val createdAt: LocalDateTime,
) {
) : CommentReplyResponse {
constructor(reply: Reply, user: User) : this(
id = reply.id,
userId = reply.userId,
userInformation = UserInformation(user.name, user.email, user.nickname),
commentId = reply.commentId,
content = reply.content,
isAnonymous = reply.isAnonymous,
isOwner = reply.isOwner,
likeCount = reply.replyLikes,
createdAt = reply.createdDateTime,
)
Expand Down

0 comments on commit 1269c06

Please sign in to comment.