From 4dfcbc43e16a5274b71b63ccbc9232994ee42e64 Mon Sep 17 00:00:00 2001 From: hwgyun Date: Sat, 21 Dec 2024 17:30:56 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=EB=B9=84=EB=B0=80=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=EC=9D=84=20=EB=B3=BC=20=EC=88=98=20=EC=97=86=EB=8A=94?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=97=90=EA=B2=8C=EB=8A=94=20?= =?UTF-8?q?=EB=82=B4=EC=9A=A9=EC=9D=84=20=EB=B9=88=20=EA=B0=92=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=91=EB=8B=B5=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20(#304)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ๐Ÿ› ๋น„๋ฐ€ ๋Œ“๊ธ€์„ ๋ณผ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž ์—๊ฒŒ๋Š” ๋‚ด์šฉ์„ ๋นˆ ๊ฐ’์œผ๋กœ ์‘๋‹ตํ•˜๋„๋ก ์ˆ˜์ • * ๐Ÿ› ๋น„๋ฐ€๋Œ“๊ธ€ ๋‚ด์šฉ ์‚ญ์ œ ๋กœ์ง ๋ฆฌํŒฉํ† ๋ง * Revert "๐Ÿ› ๋น„๋ฐ€๋Œ“๊ธ€ ๋‚ด์šฉ ์‚ญ์ œ ๋กœ์ง ๋ฆฌํŒฉํ† ๋ง" This reverts commit 600885cde83e1a56f4a70ad1fdee493108c2e486. * โ™ป๏ธ ๋น„๋ฐ€๋Œ“๊ธ€ ๋‚ด์šฉ ์‚ญ์ œ ๋กœ์ง ๋ฆฌํŒฉํ† ๋ง --- .../application/service/CommentService.kt | 11 +- .../web/in/CommunityPostCommentController.kt | 1 + .../web/in/LostPostCommentController.kt | 1 + .../application/service/CommentServiceTest.kt | 127 ++++++++++++++++++ .../comment/domain/entity/CommentEntity.kt | 12 ++ 5 files changed, 150 insertions(+), 2 deletions(-) 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 84461e0d..c21cfbea 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 @@ -12,6 +12,7 @@ import backend.team.ahachul_backend.api.comment.application.port.`in`.CommentUse import backend.team.ahachul_backend.api.comment.application.port.out.CommentReader import backend.team.ahachul_backend.api.comment.application.port.out.CommentWriter import backend.team.ahachul_backend.api.comment.domain.entity.CommentEntity +import backend.team.ahachul_backend.api.comment.domain.model.CommentVisibility import backend.team.ahachul_backend.api.comment.domain.model.PostType import backend.team.ahachul_backend.api.community.application.port.out.CommunityPostReader import backend.team.ahachul_backend.api.lost.application.port.out.LostPostReader @@ -29,9 +30,14 @@ class CommentService( private val lostPostReader: LostPostReader, private val memberReader: MemberReader, ): CommentUseCase { - override fun getComments(command: GetCommentsCommand): GetCommentsDto.Response { + val postWriterId = when (command.postType) { + PostType.COMMUNITY -> communityPostReader.getCommunityPost(command.postId).createdBy + PostType.LOST -> lostPostReader.getLostPost(command.postId).createdBy + }.toLongOrNull() + val loginMemberId = RequestUtils.getAttribute("memberId")?.toLong() + val isPostWriterEqualToLoginMember = postWriterId != null && loginMemberId == postWriterId val comments = when (command.postType) { PostType.COMMUNITY -> commentReader.findAllByCommunityPostId(command.postId) @@ -40,7 +46,8 @@ class CommentService( GetCommentsDto.Comment( it.id, it.upperComment?.id, - it.content, + if(it.validateReadPermission(loginMemberId) + || isPostWriterEqualToLoginMember) it.content else "", it.status, it.createdAt, it.createdBy, 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 5441bd6d..88ee56f6 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 @@ -14,6 +14,7 @@ class CommunityPostCommentController( private val commentUseCase: CommentUseCase ) { + @Authentication(required = false) @GetMapping("/v1/community-posts/{postId}/comments") fun getCommunityPostComments(@PathVariable postId: Long): CommonResponse { return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(postId, PostType.COMMUNITY))) 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 d8118f7a..3c270c8d 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 @@ -14,6 +14,7 @@ class LostPostCommentController( private val commentUseCase: CommentUseCase ) { + @Authentication(required = false) @GetMapping("/v1/lost-posts/{lostId}/comments") fun getLostPostComments(@PathVariable lostId: Long): CommonResponse { return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(lostId, PostType.LOST))) 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 d9170803..8cc9fd98 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 @@ -51,7 +51,12 @@ class CommentServiceTest( private lateinit var category: CategoryEntity private lateinit var communityPost: CommunityPostEntity private lateinit var lostPost: LostPostEntity + private val memberIds: MutableList = ArrayList() + private val membersCount: Int = 5 + private fun loginWithMemberId(memberId: Long) { + RequestUtils.setAttribute("memberId", memberId) + } @BeforeEach fun setup() { val member = memberRepository.save( @@ -66,12 +71,36 @@ class CommentServiceTest( ) ) member.id.let { RequestUtils.setAttribute("memberId", it) } + lateinit var postWriter: MemberEntity + + for (i in 1..membersCount) { + val member = memberRepository.save( + MemberEntity( + nickname = "nickname${i}", + provider = ProviderType.KAKAO, + providerUserId = "providerUserId${i}", + email = "email${i}", + gender = GenderType.MALE, + ageRange = "20", + status = MemberStatusType.ACTIVE + ) + ) + + if (i == 1) { + postWriter = member + } + + memberIds.add(member.id) + } + loginWithMemberId(memberIds[0]) + subwayLine = subwayLineRepository.save(SubwayLineEntity(name = "1ํ˜ธ์„ ", regionType = RegionType.METROPOLITAN)) category = categoryRepository.save(CategoryEntity(name = "ํœด๋Œ€ํฐ")) communityPost = communityPostRepository.save( CommunityPostEntity( title = "์ œ๋ชฉ", content = "๋‚ด์šฉ", + member = postWriter, categoryType = CommunityCategoryType.FREE, subwayLineEntity = subwayLine ) @@ -80,6 +109,7 @@ class CommentServiceTest( LostPostEntity( title = "์ œ๋ชฉ", content = "๋‚ด์šฉ", + member = postWriter, subwayLine = subwayLine, lostType = LostType.LOST, category = category @@ -261,6 +291,103 @@ class CommentServiceTest( } } + @Test + @DisplayName("์ปค๋ฎค๋‹ˆํ‹ฐ ์ฝ”๋ฉ˜ํŠธ ์กฐํšŒ - ๋น„๋ฐ€๋Œ“๊ธ€") + fun ์ปค๋ฎค๋‹ˆํ‹ฐ_์ฝ”๋ฉ˜ํŠธ_์กฐํšŒ_๋น„๋ฐ€๋Œ“๊ธ€() { + // given + val content = "content" + + val createParentCommentCommand = CreateCommentCommand( + postId = communityPost.id, + postType = PostType.COMMUNITY, + upperCommentId = null, + content = content, + visibility = CommentVisibility.PRIVATE + ) + val createChildCommentCommand = CreateCommentCommand( + postId = communityPost.id, + postType = PostType.COMMUNITY, + upperCommentId = null, + content = content, + visibility = CommentVisibility.PRIVATE + ) + + loginWithMemberId(memberIds[1]) + commentUseCase.createComment(createParentCommentCommand) + loginWithMemberId(memberIds[0]) + commentUseCase.createComment(createChildCommentCommand) + + val getCommentsCommand = GetCommentsCommand( + postId = communityPost.id, + PostType.COMMUNITY + ) + + // when + loginWithMemberId(memberIds[0]) + val postWriterResult = commentUseCase.getComments(getCommentsCommand) + loginWithMemberId(memberIds[1]) + val commentWriterResult = commentUseCase.getComments(getCommentsCommand) + loginWithMemberId(memberIds[2]) + val anotherMemberResult = commentUseCase.getComments(getCommentsCommand) + + // then + assertThat(postWriterResult.comments[0].parentComment.isPrivate).isTrue() + assertThat(postWriterResult.comments[0].parentComment.content).isEqualTo(content) + assertThat(commentWriterResult.comments[0].parentComment.isPrivate).isTrue() + assertThat(commentWriterResult.comments[0].parentComment.content).isEqualTo(content) + assertThat(anotherMemberResult.comments[0].parentComment.isPrivate).isTrue() + assertThat(anotherMemberResult.comments[0].parentComment.content).isEmpty() + } + + @Test + @DisplayName("์œ ์‹ค๋ฌผ ์ฝ”๋ฉ˜ํŠธ ์กฐํšŒ - ๋น„๋ฐ€๋Œ“๊ธ€") + fun ์œ ์‹ค๋ฌผ_์ฝ”๋ฉ˜ํŠธ_์กฐํšŒ_๋น„๋ฐ€๋Œ“๊ธ€() { + // given + val content = "content" + + val createParentCommentCommand = CreateCommentCommand( + postId = lostPost.id, + postType = PostType.LOST, + upperCommentId = null, + content = content, + visibility = CommentVisibility.PRIVATE + ) + val createChildCommentCommand = CreateCommentCommand( + postId = lostPost.id, + postType = PostType.LOST, + upperCommentId = null, + content = content, + visibility = CommentVisibility.PRIVATE + ) + + loginWithMemberId(memberIds[1]) + commentUseCase.createComment(createParentCommentCommand) + loginWithMemberId(memberIds[0]) + commentUseCase.createComment(createChildCommentCommand) + + val getCommentsCommand = GetCommentsCommand( + postId = lostPost.id, + PostType.LOST + ) + + // when + loginWithMemberId(memberIds[0]) + val postWriterResult = commentUseCase.getComments(getCommentsCommand) + loginWithMemberId(memberIds[1]) + val commentWriterResult = commentUseCase.getComments(getCommentsCommand) + loginWithMemberId(memberIds[2]) + val anotherMemberResult = commentUseCase.getComments(getCommentsCommand) + + // then + assertThat(postWriterResult.comments[0].parentComment.isPrivate).isTrue() + assertThat(postWriterResult.comments[0].parentComment.content).isEqualTo(content) + assertThat(commentWriterResult.comments[0].parentComment.isPrivate).isTrue() + assertThat(commentWriterResult.comments[0].parentComment.content).isEqualTo(content) + assertThat(anotherMemberResult.comments[0].parentComment.isPrivate).isTrue() + assertThat(anotherMemberResult.comments[0].parentComment.content).isEmpty() + } + + @Test @DisplayName("์ž์‹ ์ฝ”๋ฉ˜ํŠธ ์กฐํšŒ") fun ์ž์‹_์ฝ”๋ฉ˜ํŠธ_์กฐํšŒ() { diff --git a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt index 0776febb..34449823 100644 --- a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt +++ b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt @@ -56,6 +56,18 @@ class CommentEntity( } } + fun validateReadPermission(loginMemberId: Long?) : Boolean { + if (visibility == CommentVisibility.PUBLIC) { + return true + } + + return if (upperComment != null) { + upperComment!!.id == loginMemberId + } else { + member.id == loginMemberId + } + } + fun update(content: String) { this.content = content }