From ce6f4435d1c0138088ef6b4785964a10c548630f Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Fri, 29 Nov 2024 15:17:52 +0900 Subject: [PATCH 1/9] =?UTF-8?q?refactor(community):=20communityBoardView?= =?UTF-8?q?=20=ED=8F=B4=EB=8D=94=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../community/dto/response/CommunityBoardGetResponseDto.java | 2 +- .../community/repository/board/CommunityBoardRepository.java | 2 +- .../repository/board/CommunityBoardRepositoryImpl.java | 2 +- .../{domain => repository/mapper}/CommunityBoardView.java | 4 +++- .../community/service/board/CommunityBoardQueryService.java | 2 +- .../community/repository/CommunityBoardRepositoryTest.java | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) rename src/main/java/com/somemore/community/{domain => repository/mapper}/CommunityBoardView.java (51%) diff --git a/src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java b/src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java index 30f5400c8..ba15f67ee 100644 --- a/src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java +++ b/src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; -import com.somemore.community.domain.CommunityBoardView; +import com.somemore.community.repository.mapper.CommunityBoardView; import java.time.LocalDateTime; diff --git a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java index 44b34f517..64fb8ea0e 100644 --- a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java +++ b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java @@ -1,7 +1,7 @@ package com.somemore.community.repository.board; import com.somemore.community.domain.CommunityBoard; -import com.somemore.community.domain.CommunityBoardView; +import com.somemore.community.repository.mapper.CommunityBoardView; import java.util.List; import java.util.Optional; diff --git a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java index 72bed7542..62dc924ba 100644 --- a/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java +++ b/src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java @@ -4,7 +4,7 @@ import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import com.somemore.community.domain.CommunityBoard; -import com.somemore.community.domain.CommunityBoardView; +import com.somemore.community.repository.mapper.CommunityBoardView; import com.somemore.community.domain.QCommunityBoard; import com.somemore.volunteer.domain.QVolunteer; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/somemore/community/domain/CommunityBoardView.java b/src/main/java/com/somemore/community/repository/mapper/CommunityBoardView.java similarity index 51% rename from src/main/java/com/somemore/community/domain/CommunityBoardView.java rename to src/main/java/com/somemore/community/repository/mapper/CommunityBoardView.java index 01cbd31a9..a03f271aa 100644 --- a/src/main/java/com/somemore/community/domain/CommunityBoardView.java +++ b/src/main/java/com/somemore/community/repository/mapper/CommunityBoardView.java @@ -1,4 +1,6 @@ -package com.somemore.community.domain; +package com.somemore.community.repository.mapper; + +import com.somemore.community.domain.CommunityBoard; public record CommunityBoardView( CommunityBoard communityBoard, diff --git a/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java b/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java index 98d45143f..d9be49dda 100644 --- a/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java +++ b/src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java @@ -1,7 +1,7 @@ package com.somemore.community.service.board; import com.somemore.community.domain.CommunityBoard; -import com.somemore.community.domain.CommunityBoardView; +import com.somemore.community.repository.mapper.CommunityBoardView; import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto; import com.somemore.community.dto.response.CommunityBoardGetResponseDto; import com.somemore.community.repository.board.CommunityBoardRepository; diff --git a/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java b/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java index acb29075f..bd524f811 100644 --- a/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java +++ b/src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java @@ -3,7 +3,7 @@ import com.somemore.IntegrationTestSupport; import com.somemore.auth.oauth.OAuthProvider; import com.somemore.community.domain.CommunityBoard; -import com.somemore.community.domain.CommunityBoardView; +import com.somemore.community.repository.mapper.CommunityBoardView; import com.somemore.community.repository.board.CommunityBoardRepository; import com.somemore.volunteer.domain.Volunteer; import com.somemore.volunteer.repository.VolunteerRepository; From 9d6cbc8b311afe72bd0980373db7233cd342a2cc Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 02:15:25 +0900 Subject: [PATCH 2/9] =?UTF-8?q?test(community):=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CommunityCommentRepositoryTest.java | 44 +++++- .../CommunityCommentQueryServiceTest.java | 138 ++++++++++++++++++ 2 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java diff --git a/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java b/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java index 8903033ce..0a84492a6 100644 --- a/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java +++ b/src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java @@ -1,16 +1,21 @@ package com.somemore.community.repository; import com.somemore.IntegrationTestSupport; +import com.somemore.auth.oauth.OAuthProvider; import com.somemore.community.domain.CommunityBoard; import com.somemore.community.domain.CommunityComment; import com.somemore.community.repository.board.CommunityBoardRepository; import com.somemore.community.repository.comment.CommunityCommentRepository; +import com.somemore.community.repository.mapper.CommunityCommentView; +import com.somemore.volunteer.domain.Volunteer; +import com.somemore.volunteer.repository.VolunteerRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -23,6 +28,8 @@ class CommunityCommentRepositoryTest extends IntegrationTestSupport { CommunityCommentRepository communityCommentRepository; @Autowired CommunityBoardRepository communityBoardRepository; + @Autowired + VolunteerRepository volunteerRepository; private Long boardId; private UUID writerId; @@ -30,19 +37,23 @@ class CommunityCommentRepositoryTest extends IntegrationTestSupport { @BeforeEach void setUp() { + String oAuthId = "example-oauth-id"; + Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId); + volunteerRepository.save(volunteer); + + writerId = volunteer.getId(); + CommunityBoard communityBoard = CommunityBoard.builder() .title("테스트 커뮤니티 게시글 제목") .content("테스트 커뮤니티 게시글 내용") .imgUrl("http://community.example.com/123") - .writerId(UUID.randomUUID()) + .writerId(writerId) .build(); communityBoardRepository.save(communityBoard); boardId = communityBoard.getId(); - writerId = UUID.randomUUID(); - CommunityComment communityComment = CommunityComment.builder() .communityBoardId(boardId) .writerId(writerId) @@ -74,7 +85,7 @@ void createCommunityCommentReply() { .communityBoardId(boardId) .writerId(writerId) .content("커뮤니티 댓글 테스트 내용") - .parentCommentId(1L) + .parentCommentId(savedComment.getId()) .build(); //when @@ -83,7 +94,7 @@ void createCommunityCommentReply() { //then assertThat(savedCommentReply.getWriterId()).isEqualTo(writerId); assertThat(savedCommentReply.getContent()).isEqualTo("커뮤니티 댓글 테스트 내용"); - assertThat(savedCommentReply.getParentCommentId()).isEqualTo(1L); + assertThat(savedCommentReply.getParentCommentId()).isEqualTo(savedComment.getId()); } @DisplayName("댓글을 id로 조회할 수 있다. (Repository)") @@ -112,4 +123,27 @@ void existsById() { //then assertThat(isExist).isTrue(); } + + @DisplayName("게시글 id로 게시글에 달린 댓글을 조회할 수 있다. (Repository)") + @Test + void findCommunityCommentByBoardId() { + + //given + CommunityComment communityCommentReply = CommunityComment.builder() + .communityBoardId(boardId) + .writerId(writerId) + .content("커뮤니티 댓글 테스트 내용") + .parentCommentId(savedComment.getId()) + .build(); + + CommunityComment savedCommentReply = communityCommentRepository.save(communityCommentReply); + + //when + List comments = communityCommentRepository.findCommentsByBoardId(boardId); + + //then + assertThat(comments).hasSize(2); + assertThat(comments.getFirst().communityComment().getId()).isEqualTo(savedComment.getId()); + assertThat(comments.getLast().communityComment().getId()).isEqualTo(savedCommentReply.getId()); + } } diff --git a/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java new file mode 100644 index 000000000..86fb8d32a --- /dev/null +++ b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java @@ -0,0 +1,138 @@ +package com.somemore.community.service.comment; + +import com.somemore.IntegrationTestSupport; +import com.somemore.auth.oauth.OAuthProvider; +import com.somemore.community.domain.CommunityBoard; +import com.somemore.community.domain.CommunityComment; +import com.somemore.community.dto.request.CommunityBoardCreateRequestDto; +import com.somemore.community.dto.request.CommunityCommentCreateRequestDto; +import com.somemore.community.dto.response.CommunityCommentResponseDto; +import com.somemore.community.repository.board.CommunityBoardRepository; +import com.somemore.community.repository.comment.CommunityCommentRepository; +import com.somemore.community.usecase.comment.DeleteCommunityCommentUseCase; +import com.somemore.volunteer.domain.Volunteer; +import com.somemore.volunteer.repository.VolunteerRepository; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + + +class CommunityCommentQueryServiceTest extends IntegrationTestSupport { + @Autowired + private CommunityCommentQueryService communityCommentQueryService; + @Autowired + private CommunityCommentRepository communityCommentRepository; + @Autowired + private CommunityBoardRepository communityBoardRepository; + @Autowired + private VolunteerRepository volunteerRepository; + @Autowired + private DeleteCommunityCommentUseCase deleteCommunityCommentUseCase; + + private Long boardId, commentId, replyId; + UUID writerId1, writerId2; + + @BeforeEach + void setUp() { + String oAuthId1 = "example-oauth-id"; + Volunteer volunteer1 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId1); + volunteerRepository.save(volunteer1); + writerId1 = volunteer1.getId(); + + String oAuthId2 = "example-oauth-id"; + Volunteer volunteer2 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId2); + volunteerRepository.save(volunteer2); + writerId2 = volunteer2.getId(); + + CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto.builder() + .title("커뮤니티 테스트 제목") + .content("커뮤니티 테스트 내용") + .build(); + CommunityBoard communityBoard = communityBoardRepository.save(boardDto.toEntity(writerId1, "https://test.image/123")); + boardId = communityBoard.getId(); + + CommunityCommentCreateRequestDto dto1 = CommunityCommentCreateRequestDto.builder() + .communityBoardId(boardId) + .content("커뮤니티 댓글 테스트 내용") + .parentCommentId(null) + .build(); + CommunityComment communityComment = communityCommentRepository.save(dto1.toEntity(writerId2)); + commentId = communityComment.getId(); + + CommunityCommentCreateRequestDto dto2 = CommunityCommentCreateRequestDto.builder() + .communityBoardId(boardId) + .content("커뮤니티 대댓글 테스트 내용") + .parentCommentId(commentId) + .build(); + CommunityComment communityReply = communityCommentRepository.save(dto2.toEntity(writerId1)); + replyId = communityReply.getId(); + } + + @AfterEach + void tearDown() { + communityCommentRepository.deleteAllInBatch(); + } + + @DisplayName("커뮤니티 게시글에 달린 댓글을 조회할 수 있다.") + @Test + void getCommentsByCommunityBoardId() { + + //given + //when + List comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId); + + //then + assertThat(comments).hasSize(1); + assertThat(comments.getFirst().id()).isEqualTo(commentId); + assertThat(comments.getFirst().replies()).hasSize(1); + assertThat(comments.getFirst().replies().getFirst().id()).isEqualTo(replyId); + } + + @DisplayName("삭제된 댓글의 경우 조회할 수 없다.") + @Test + void doesNotFind() { + + //given + CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder() + .communityBoardId(boardId) + .content("커뮤니티 댓글 테스트 내용") + .parentCommentId(null) + .build(); + CommunityComment communityComment = communityCommentRepository.save(dto.toEntity(writerId2)); + + deleteCommunityCommentUseCase.deleteCommunityComment(writerId2, communityComment.getId()); + deleteCommunityCommentUseCase.deleteCommunityComment(writerId1, replyId); + + //when + List comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId); + + //then + assertThat(comments).hasSize(1); + assertThat(comments.getFirst().id()).isEqualTo(commentId); + assertThat(comments.getFirst().replies()).isEmpty(); + } + + @DisplayName("대댓글이 있는 댓글의 경우 삭제된 댓글로 조회할 수 있다.") + @Test + void getCommentsByCommunityBoardIdWithDeletedComment() { + + //given + deleteCommunityCommentUseCase.deleteCommunityComment(writerId2, commentId); + //when + List comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId); + + //then + assertThat(comments).hasSize(1); + assertThat(comments.getFirst().content()).isEqualTo("삭제된 댓글입니다"); + assertThat(comments.getFirst().writerNickname()).isEqualTo(""); + assertThat(comments.getFirst().replies()).hasSize(1); + assertThat(comments.getFirst().replies().getFirst().id()).isEqualTo(replyId); + } +} \ No newline at end of file From 3a3ea7b373e467fa711ee6b4a3935d310d173d8a Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 02:16:20 +0900 Subject: [PATCH 3/9] =?UTF-8?q?feat(community):=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EC=8B=9C=20comment(=EB=82=B4=EC=9A=A9)=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기존 댓글 내용 -> "삭제된 댓글입니다" --- .../java/com/somemore/community/domain/CommunityComment.java | 4 ++++ .../service/comment/DeleteCommunityCommentService.java | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/main/java/com/somemore/community/domain/CommunityComment.java b/src/main/java/com/somemore/community/domain/CommunityComment.java index fd6a85d82..5187b0d78 100644 --- a/src/main/java/com/somemore/community/domain/CommunityComment.java +++ b/src/main/java/com/somemore/community/domain/CommunityComment.java @@ -51,4 +51,8 @@ public boolean isWriter(UUID writerId) { public void updateWith(CommunityCommentUpdateRequestDto dto) { this.content = dto.content(); } + + public void replaceComment() { + this.content = "삭제된 댓글입니다"; + } } diff --git a/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java index 6072c4e4f..5d2791a0e 100644 --- a/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java +++ b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java @@ -28,6 +28,8 @@ public void deleteCommunityComment(UUID writerId, Long id) { communityComment.markAsDeleted(); + communityComment.replaceComment(); + communityCommentRepository.save(communityComment); } From 7959ad61e3d081854dab9ff0c7def4d7ec55a8c1 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 02:17:55 +0900 Subject: [PATCH 4/9] =?UTF-8?q?feat(community):=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20View=20=EB=B0=8F=20responeDto=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../response/CommunityCommentResponseDto.java | 36 +++++++++++++++++++ .../mapper/CommunityCommentView.java | 21 +++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java create mode 100644 src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java diff --git a/src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java b/src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java new file mode 100644 index 000000000..78731c7be --- /dev/null +++ b/src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java @@ -0,0 +1,36 @@ +package com.somemore.community.dto.response; + +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import com.somemore.community.repository.mapper.CommunityCommentView; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +public record CommunityCommentResponseDto( + Long id, + String writerNickname, + String content, + LocalDateTime updatedAt, + List replies +) { + public CommunityCommentResponseDto { + replies = replies == null ? new ArrayList<>() : replies; + } + + public static CommunityCommentResponseDto fromView(CommunityCommentView comment) { + return new CommunityCommentResponseDto( + comment.communityComment().getId(), + comment.writerNickname(), + comment.communityComment().getContent(), + comment.communityComment().getUpdatedAt(), + new ArrayList<>() + ); + } + + public void addReply(CommunityCommentResponseDto reply) { + this.replies.add(reply); + } +} diff --git a/src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java b/src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java new file mode 100644 index 000000000..0481b2e0f --- /dev/null +++ b/src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java @@ -0,0 +1,21 @@ +package com.somemore.community.repository.mapper; + +import com.somemore.community.domain.CommunityComment; +import lombok.Builder; + +public record CommunityCommentView( + CommunityComment communityComment, + String writerNickname +) { + @Builder + public CommunityCommentView(CommunityComment communityComment, String writerNickname) { + this.communityComment = communityComment; + this.writerNickname = writerNickname; + } + + public CommunityCommentView replaceWriterNickname(CommunityCommentView communityCommentView) { + return CommunityCommentView.builder() + .communityComment(communityCommentView.communityComment) + .writerNickname("").build(); + } +} From fe89e8641fe3d58dc68f14d16272ae98ae010471 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 02:19:22 +0900 Subject: [PATCH 5/9] =?UTF-8?q?feat(community):=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20ID=EB=A1=9C=20=EB=8C=93=EA=B8=80=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/CommunityCommentRepository.java | 3 +++ .../CommunityCommentRepositoryImpl.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java index c5db8228b..f71e5f630 100644 --- a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java +++ b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java @@ -1,12 +1,15 @@ package com.somemore.community.repository.comment; import com.somemore.community.domain.CommunityComment; +import com.somemore.community.repository.mapper.CommunityCommentView; +import java.util.List; import java.util.Optional; public interface CommunityCommentRepository { CommunityComment save(CommunityComment communityComment); Optional findById(Long id); + List findCommentsByBoardId(Long boardId); boolean existsById(Long id); default boolean doesNotExistById(Long id) { return !existsById(id); diff --git a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java index 8a6199f65..c3eba1fc8 100644 --- a/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java +++ b/src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java @@ -1,11 +1,15 @@ package com.somemore.community.repository.comment; +import com.querydsl.core.types.Projections; import com.querydsl.jpa.impl.JPAQueryFactory; import com.somemore.community.domain.CommunityComment; import com.somemore.community.domain.QCommunityComment; +import com.somemore.community.repository.mapper.CommunityCommentView; +import com.somemore.volunteer.domain.QVolunteer; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; +import java.util.List; import java.util.Optional; @RequiredArgsConstructor @@ -31,6 +35,21 @@ public Optional findById(Long id) { .fetchOne()); } + public List findCommentsByBoardId(Long boardId) { + QCommunityComment communityComment = QCommunityComment.communityComment; + QVolunteer volunteer = QVolunteer.volunteer; + + return queryFactory + .select(Projections.constructor(CommunityCommentView.class, + communityComment, + volunteer.nickname)) + .from(communityComment) + .join(volunteer).on(communityComment.writerId.eq(volunteer.id)) + .where(communityComment.communityBoardId.eq(boardId)) + .orderBy(communityComment.parentCommentId.asc().nullsFirst(), communityComment.createdAt.asc()) + .fetch(); + } + @Override public boolean existsById(Long id) { QCommunityComment communityComment = QCommunityComment.communityComment; From d1fc3fc6f4aeadf8080ee405ff1a3ab94d1f2603 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 02:24:10 +0900 Subject: [PATCH 6/9] =?UTF-8?q?feat(community):=20=EB=8C=93=EA=B8=80-?= =?UTF-8?q?=EB=8C=80=EB=8C=93=EA=B8=80=20=EA=B5=AC=EC=A1=B0=EB=A1=9C=20?= =?UTF-8?q?=EB=8C=93=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20service=20=EB=B0=8F?= =?UTF-8?q?=20usecase=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/CommunityCommentQueryService.java | 78 +++++++++++++++++++ .../comment/CommunityCommentQueryUseCase.java | 9 +++ 2 files changed, 87 insertions(+) create mode 100644 src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java create mode 100644 src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java diff --git a/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java b/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java new file mode 100644 index 000000000..a02b611aa --- /dev/null +++ b/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java @@ -0,0 +1,78 @@ +package com.somemore.community.service.comment; + +import com.somemore.community.domain.CommunityComment; +import com.somemore.community.dto.response.CommunityCommentResponseDto; +import com.somemore.community.repository.comment.CommunityCommentRepository; +import com.somemore.community.repository.mapper.CommunityCommentView; +import com.somemore.community.usecase.comment.CommunityCommentQueryUseCase; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.*; + +@RequiredArgsConstructor +@Transactional(readOnly = true) +@Service +public class CommunityCommentQueryService implements CommunityCommentQueryUseCase { + + private final CommunityCommentRepository communityCommentRepository; + + @Override + public List getCommunityCommentsByBoardId(Long boardId) { + List allComments = communityCommentRepository.findCommentsByBoardId(boardId); + List filteredComments = filterValidComments(allComments); + return createCommentHierarchy(filteredComments); + } + + private List filterValidComments(List comments) { + List parentCommentIds = findParentCommentIds(comments); + + return comments.stream() + .flatMap(comment -> processDeletedComment(parentCommentIds, comment).stream()) + .toList(); + } + + private List findParentCommentIds(List comments) { + return comments.stream() + .filter(comment -> !comment.communityComment().getDeleted()) + .map(comment -> comment.communityComment().getParentCommentId()) + .filter(Objects::nonNull) + .toList(); + } + + private Optional processDeletedComment(List parentCommentIds, CommunityCommentView commentView) { + CommunityComment comment = commentView.communityComment(); + + if (comment.getDeleted()) { + if (parentCommentIds.contains(comment.getId())) { + return Optional.of(commentView.replaceWriterNickname(commentView)); + } + return Optional.empty(); + } + + return Optional.of(commentView); + } + + private List createCommentHierarchy(List comments) { + + Map commentMap = new HashMap<>(); + List rootComments = new ArrayList<>(); + + for (CommunityCommentView comment : comments) { + CommunityCommentResponseDto dto = CommunityCommentResponseDto.fromView(comment); + commentMap.put(dto.id(), dto); + + Long parentCommentId = comment.communityComment().getParentCommentId(); + + if (parentCommentId == null) { + rootComments.add(dto); + } else { + commentMap.get(parentCommentId).addReply(dto); + } + } + + return rootComments; + } +} + diff --git a/src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java b/src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java new file mode 100644 index 000000000..33ab69c94 --- /dev/null +++ b/src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java @@ -0,0 +1,9 @@ +package com.somemore.community.usecase.comment; + +import com.somemore.community.dto.response.CommunityCommentResponseDto; + +import java.util.List; + +public interface CommunityCommentQueryUseCase { + List getCommunityCommentsByBoardId(Long boardId); +} From a036c0cdd2ec199864d2f2a8fff93758c4541a89 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 03:31:27 +0900 Subject: [PATCH 7/9] =?UTF-8?q?refactor(community):=20sonar=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - View 생성자 제거 - isDeleted 메서드 추가 - test isEqual("") -> isEmpty() --- .../com/somemore/community/domain/CommunityComment.java | 4 ++++ .../community/repository/mapper/CommunityCommentView.java | 7 +------ .../service/comment/CommunityCommentQueryService.java | 2 +- .../service/comment/CommunityCommentQueryServiceTest.java | 2 +- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/somemore/community/domain/CommunityComment.java b/src/main/java/com/somemore/community/domain/CommunityComment.java index 5187b0d78..b7bafa499 100644 --- a/src/main/java/com/somemore/community/domain/CommunityComment.java +++ b/src/main/java/com/somemore/community/domain/CommunityComment.java @@ -55,4 +55,8 @@ public void updateWith(CommunityCommentUpdateRequestDto dto) { public void replaceComment() { this.content = "삭제된 댓글입니다"; } + + public Boolean isDeleted() { + return this.getDeleted(); + } } diff --git a/src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java b/src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java index 0481b2e0f..48664c0a9 100644 --- a/src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java +++ b/src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java @@ -3,16 +3,11 @@ import com.somemore.community.domain.CommunityComment; import lombok.Builder; +@Builder public record CommunityCommentView( CommunityComment communityComment, String writerNickname ) { - @Builder - public CommunityCommentView(CommunityComment communityComment, String writerNickname) { - this.communityComment = communityComment; - this.writerNickname = writerNickname; - } - public CommunityCommentView replaceWriterNickname(CommunityCommentView communityCommentView) { return CommunityCommentView.builder() .communityComment(communityCommentView.communityComment) diff --git a/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java b/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java index a02b611aa..5be670611 100644 --- a/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java +++ b/src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java @@ -44,7 +44,7 @@ private List findParentCommentIds(List comments) { private Optional processDeletedComment(List parentCommentIds, CommunityCommentView commentView) { CommunityComment comment = commentView.communityComment(); - if (comment.getDeleted()) { + if (comment.isDeleted()) { if (parentCommentIds.contains(comment.getId())) { return Optional.of(commentView.replaceWriterNickname(commentView)); } diff --git a/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java index 86fb8d32a..ad20e4140 100644 --- a/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java +++ b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java @@ -131,7 +131,7 @@ void getCommentsByCommunityBoardIdWithDeletedComment() { //then assertThat(comments).hasSize(1); assertThat(comments.getFirst().content()).isEqualTo("삭제된 댓글입니다"); - assertThat(comments.getFirst().writerNickname()).isEqualTo(""); + assertThat(comments.getFirst().writerNickname()).isEmpty(); assertThat(comments.getFirst().replies()).hasSize(1); assertThat(comments.getFirst().replies().getFirst().id()).isEqualTo(replyId); } From 3fc1fb9d3b42bd12ccc24ec8a2a85b35acb5d047 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 03:58:16 +0900 Subject: [PATCH 8/9] =?UTF-8?q?refactor(community):=20sonar=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Boolean -> boolean --- .../java/com/somemore/community/domain/CommunityComment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/somemore/community/domain/CommunityComment.java b/src/main/java/com/somemore/community/domain/CommunityComment.java index b7bafa499..2a121c55f 100644 --- a/src/main/java/com/somemore/community/domain/CommunityComment.java +++ b/src/main/java/com/somemore/community/domain/CommunityComment.java @@ -56,7 +56,7 @@ public void replaceComment() { this.content = "삭제된 댓글입니다"; } - public Boolean isDeleted() { + public boolean isDeleted() { return this.getDeleted(); } } From 7152613afb8542716cd4955229866a6f0ac67645 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Mon, 2 Dec 2024 11:23:46 +0900 Subject: [PATCH 9/9] =?UTF-8?q?refactor(community):=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EB=A6=AC=EB=B7=B0=20=EC=82=AC=ED=95=AD=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - markAsDeleted 메서드 내에서 content 내용 변경 - 개행 추가 --- .../java/com/somemore/community/domain/CommunityComment.java | 4 +++- .../service/comment/DeleteCommunityCommentService.java | 2 -- .../service/comment/CommunityCommentQueryServiceTest.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/somemore/community/domain/CommunityComment.java b/src/main/java/com/somemore/community/domain/CommunityComment.java index 2a121c55f..650981767 100644 --- a/src/main/java/com/somemore/community/domain/CommunityComment.java +++ b/src/main/java/com/somemore/community/domain/CommunityComment.java @@ -52,7 +52,9 @@ public void updateWith(CommunityCommentUpdateRequestDto dto) { this.content = dto.content(); } - public void replaceComment() { + @Override + public void markAsDeleted() { + super.markAsDeleted(); this.content = "삭제된 댓글입니다"; } diff --git a/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java index 5d2791a0e..6072c4e4f 100644 --- a/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java +++ b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java @@ -28,8 +28,6 @@ public void deleteCommunityComment(UUID writerId, Long id) { communityComment.markAsDeleted(); - communityComment.replaceComment(); - communityCommentRepository.save(communityComment); } diff --git a/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java index ad20e4140..a41b33119 100644 --- a/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java +++ b/src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java @@ -135,4 +135,4 @@ void getCommentsByCommunityBoardIdWithDeletedComment() { assertThat(comments.getFirst().replies()).hasSize(1); assertThat(comments.getFirst().replies().getFirst().id()).isEqualTo(replyId); } -} \ No newline at end of file +}