diff --git a/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt b/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt index 3abed19e..c05ff442 100644 --- a/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt +++ b/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt @@ -31,8 +31,12 @@ class CommentService( ): CommentUseCase { override fun getComments(command: GetCommentsCommand): GetCommentsDto.Response { - val comments = commentReader.findAllByPostId(command.postId) - .map { + + + val comments = when (command.postType) { + PostType.COMMUNITY -> commentReader.findAllByCommunityPostId(command.postId) + PostType.LOST -> commentReader.findAllByLostPostId(command.postId) + }.map { GetCommentsDto.Comment( it.id, it.upperComment?.id, diff --git a/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt b/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt index ca8d1fd4..5441bd6d 100644 --- a/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt +++ b/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt @@ -16,7 +16,7 @@ class CommunityPostCommentController( @GetMapping("/v1/community-posts/{postId}/comments") fun getCommunityPostComments(@PathVariable postId: Long): CommonResponse { - return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(postId))) + return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(postId, PostType.COMMUNITY))) } @Authentication diff --git a/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt b/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt index f5e68cde..d8118f7a 100644 --- a/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt +++ b/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt @@ -16,7 +16,7 @@ class LostPostCommentController( @GetMapping("/v1/lost-posts/{lostId}/comments") fun getLostPostComments(@PathVariable lostId: Long): CommonResponse { - return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(lostId))) + return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(lostId, PostType.LOST))) } @Authentication diff --git a/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt b/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt index 7856da38..d9170803 100644 --- a/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt +++ b/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt @@ -200,8 +200,8 @@ class CommentServiceTest( } @Test - @DisplayName("코멘트 조회") - fun 코멘트_조회() { + @DisplayName("커뮤니티 코멘트 조회") + fun 커뮤니티_코멘트_조회() { // given for (i in 1..10) { val createCommentCommand = CreateCommentCommand( @@ -215,7 +215,39 @@ class CommentServiceTest( } val getCommentsCommand = GetCommentsCommand( - postId = communityPost.id + postId = communityPost.id, + PostType.COMMUNITY + ) + + // when + val result = commentUseCase.getComments(getCommentsCommand) + + // then + assertThat(result.comments).hasSize(10) + for (i: Int in 0..9) { + assertThat(result.comments[i].parentComment).isNotNull + assertThat(result.comments[i].childComments).isEmpty() + } + } + + @Test + @DisplayName("유실물 코멘트 조회") + fun 유실물_코멘트_조회() { + // given + for (i in 1..10) { + val createCommentCommand = CreateCommentCommand( + postId = lostPost.id, + postType = PostType.LOST, + upperCommentId = null, + content = "내용${i}", + visibility = CommentVisibility.PUBLIC + ) + commentUseCase.createComment(createCommentCommand) + } + + val getCommentsCommand = GetCommentsCommand( + postId = lostPost.id, + PostType.LOST ) // when @@ -254,7 +286,8 @@ class CommentServiceTest( } val getCommentsCommand = GetCommentsCommand( - postId = communityPost.id + postId = communityPost.id, + PostType.COMMUNITY ) // when diff --git a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentPersistence.kt b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentPersistence.kt index 66407d52..f86b7048 100644 --- a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentPersistence.kt +++ b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentPersistence.kt @@ -25,10 +25,14 @@ class CommentPersistence( return repository.findById(id).orElse(null) } - override fun findAllByPostId(postId: Long): List { + override fun findAllByCommunityPostId(postId: Long): List { return repository.findAllByCommunityPostId(postId) } + override fun findAllByLostPostId(postId: Long): List { + return repository.findAllByLostPostId(postId) + } + override fun count(postId: Long): Int { return repository.countByCommunityPostId(postId) } diff --git a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentRepository.kt b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentRepository.kt index 06deab46..c5ae1f86 100644 --- a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentRepository.kt +++ b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/adapter/web/out/CommentRepository.kt @@ -13,5 +13,12 @@ interface CommentRepository: JpaRepository { "ORDER BY cc.createdAt ASC") fun findAllByCommunityPostId(postId: Long): List + @Query("SELECT cc " + + "FROM CommentEntity cc " + + "JOIN FETCH cc.member m " + + "WHERE cc.lostPost.id = :postId " + + "ORDER BY cc.createdAt ASC") + fun findAllByLostPostId(postId: Long): List + fun countByCommunityPostId(postId: Long): Int } \ No newline at end of file diff --git a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/command/GetCommentsCommand.kt b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/command/GetCommentsCommand.kt index e7feeb5b..fc6c563e 100644 --- a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/command/GetCommentsCommand.kt +++ b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/command/GetCommentsCommand.kt @@ -1,6 +1,9 @@ package backend.team.ahachul_backend.api.comment.application.command +import backend.team.ahachul_backend.api.comment.domain.model.PostType + class GetCommentsCommand( - val postId: Long + val postId: Long, + val postType: PostType ) { } \ No newline at end of file diff --git a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/port/out/CommentReader.kt b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/port/out/CommentReader.kt index 946d59a1..79a91313 100644 --- a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/port/out/CommentReader.kt +++ b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/port/out/CommentReader.kt @@ -8,7 +8,8 @@ interface CommentReader { fun findById(id: Long): CommentEntity? - fun findAllByPostId(postId: Long): List + fun findAllByCommunityPostId(postId: Long): List + fun findAllByLostPostId(postId: Long): List fun count(postId: Long): Int } \ No newline at end of file