fix(character-comment): 캐릭터 상세 댓글 집계 및 최신 댓글 조회 기준 수정

- 최신 댓글 조회 시 원댓글(Parent=null)만 대상으로 조회하도록 Repository 메서드 및 Service 로직 변경

- 총 댓글 수를 "활성 원댓글 + 활성 부모를 가진 활성 답글"로 계산하여, 삭제된 원댓글의 답글은 집계에서 제외되도록 수정
This commit is contained in:
Klaus 2025-08-20 15:39:52 +09:00
parent aeab6eddc2
commit ca27903e45
2 changed files with 19 additions and 6 deletions

View File

@ -25,8 +25,14 @@ interface CharacterCommentRepository : JpaRepository<CharacterComment, Long> {
pageable: Pageable pageable: Pageable
): List<CharacterComment> ): List<CharacterComment>
fun findFirstByChatCharacter_IdAndIsActiveTrueOrderByCreatedAtDesc(chatCharacterId: Long): CharacterComment? // 최신 원댓글만 조회
fun findFirstByChatCharacter_IdAndIsActiveTrueAndParentIsNullOrderByCreatedAtDesc(
chatCharacterId: Long
): CharacterComment?
// 전체(상위+답글) 활성 댓글 총 개수 // 활성 원댓글 수
fun countByChatCharacter_IdAndIsActiveTrue(chatCharacterId: Long): Int fun countByChatCharacter_IdAndIsActiveTrueAndParentIsNull(chatCharacterId: Long): Int
// 활성 부모를 가진 활성 답글 수 (부모가 null인 경우 제외됨)
fun countByChatCharacter_IdAndIsActiveTrueAndParent_IsActiveTrue(chatCharacterId: Long): Int
} }

View File

@ -153,13 +153,20 @@ class CharacterCommentService(
@Transactional(readOnly = true) @Transactional(readOnly = true)
fun getLatestComment(imageHost: String, characterId: Long): CharacterCommentResponse? { fun getLatestComment(imageHost: String, characterId: Long): CharacterCommentResponse? {
val last = commentRepository.findFirstByChatCharacter_IdAndIsActiveTrueOrderByCreatedAtDesc(characterId) return commentRepository
return last?.let { toCommentResponse(imageHost, it) } .findFirstByChatCharacter_IdAndIsActiveTrueAndParentIsNullOrderByCreatedAtDesc(characterId)
?.let { toCommentResponse(imageHost, it) }
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
fun getTotalCommentCount(characterId: Long): Int { fun getTotalCommentCount(characterId: Long): Int {
return commentRepository.countByChatCharacter_IdAndIsActiveTrue(characterId) // 활성 원댓글 수 + 활성 부모를 가진 활성 답글 수
val originalCount = commentRepository
.countByChatCharacter_IdAndIsActiveTrueAndParentIsNull(characterId)
val replyWithActiveParentCount = commentRepository
.countByChatCharacter_IdAndIsActiveTrueAndParent_IsActiveTrue(characterId)
return originalCount + replyWithActiveParentCount
} }
@Transactional @Transactional