Skip to content

Commit

Permalink
✨ 유실물 댓글 조회 구현 (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwgyun authored Dec 9, 2024
1 parent 33dcfb2 commit 801b23e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CommunityPostCommentController(

@GetMapping("/v1/community-posts/{postId}/comments")
fun getCommunityPostComments(@PathVariable postId: Long): CommonResponse<GetCommentsDto.Response> {
return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(postId)))
return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(postId, PostType.COMMUNITY)))
}

@Authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LostPostCommentController(

@GetMapping("/v1/lost-posts/{lostId}/comments")
fun getLostPostComments(@PathVariable lostId: Long): CommonResponse<GetCommentsDto.Response> {
return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(lostId)))
return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(lostId, PostType.LOST)))
}

@Authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ class CommentServiceTest(
}

@Test
@DisplayName("코멘트 조회")
fun 코멘트_조회() {
@DisplayName("커뮤니티 코멘트 조회")
fun 커뮤니티_코멘트_조회() {
// given
for (i in 1..10) {
val createCommentCommand = CreateCommentCommand(
Expand All @@ -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
Expand Down Expand Up @@ -254,7 +286,8 @@ class CommentServiceTest(
}

val getCommentsCommand = GetCommentsCommand(
postId = communityPost.id
postId = communityPost.id,
PostType.COMMUNITY
)

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ class CommentPersistence(
return repository.findById(id).orElse(null)
}

override fun findAllByPostId(postId: Long): List<CommentEntity> {
override fun findAllByCommunityPostId(postId: Long): List<CommentEntity> {
return repository.findAllByCommunityPostId(postId)
}

override fun findAllByLostPostId(postId: Long): List<CommentEntity> {
return repository.findAllByLostPostId(postId)
}

override fun count(postId: Long): Int {
return repository.countByCommunityPostId(postId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ interface CommentRepository: JpaRepository<CommentEntity, Long> {
"ORDER BY cc.createdAt ASC")
fun findAllByCommunityPostId(postId: Long): List<CommentEntity>

@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<CommentEntity>

fun countByCommunityPostId(postId: Long): Int
}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ interface CommentReader {

fun findById(id: Long): CommentEntity?

fun findAllByPostId(postId: Long): List<CommentEntity>
fun findAllByCommunityPostId(postId: Long): List<CommentEntity>

fun findAllByLostPostId(postId: Long): List<CommentEntity>
fun count(postId: Long): Int
}

0 comments on commit 801b23e

Please sign in to comment.