-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/78 커뮤니티 댓글 조회 기능 #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ce6f443
refactor(community): communityBoardView 폴더 이동
ayoung-dev 9d6cbc8
test(community): 댓글 조회 테스트 추가
ayoung-dev 3a3ea7b
feat(community): 댓글 삭제 시 comment(내용) 변경
ayoung-dev 7959ad6
feat(community): 댓글 조회 View 및 responeDto 추가
ayoung-dev fe89e86
feat(community): 게시글 ID로 댓글 조회 메서드 추가
ayoung-dev d1fc3fc
feat(community): 댓글-대댓글 구조로 댓글 조회 service 및 usecase 추가
ayoung-dev a036c0c
refactor(community): sonar 반영
ayoung-dev 3fc1fb9
refactor(community): sonar 반영
ayoung-dev 7152613
refactor(community): 코드 리뷰 사항 반영
ayoung-dev e9c20d6
Merge branch 'main' of https://github.com/prgrms-web-devcourse-final-…
ayoung-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/somemore/community/dto/response/CommunityCommentResponseDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<CommunityCommentResponseDto> 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); | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
.../community/domain/CommunityBoardView.java → ...repository/mapper/CommunityBoardView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/somemore/community/repository/mapper/CommunityCommentView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.somemore.community.repository.mapper; | ||
|
|
||
| import com.somemore.community.domain.CommunityComment; | ||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| public record CommunityCommentView( | ||
| CommunityComment communityComment, | ||
| String writerNickname | ||
| ) { | ||
| public CommunityCommentView replaceWriterNickname(CommunityCommentView communityCommentView) { | ||
| return CommunityCommentView.builder() | ||
| .communityComment(communityCommentView.communityComment) | ||
| .writerNickname("").build(); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/com/somemore/community/service/comment/CommunityCommentQueryService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<CommunityCommentResponseDto> getCommunityCommentsByBoardId(Long boardId) { | ||
| List<CommunityCommentView> allComments = communityCommentRepository.findCommentsByBoardId(boardId); | ||
| List<CommunityCommentView> filteredComments = filterValidComments(allComments); | ||
| return createCommentHierarchy(filteredComments); | ||
| } | ||
|
|
||
| private List<CommunityCommentView> filterValidComments(List<CommunityCommentView> comments) { | ||
| List<Long> parentCommentIds = findParentCommentIds(comments); | ||
|
|
||
| return comments.stream() | ||
| .flatMap(comment -> processDeletedComment(parentCommentIds, comment).stream()) | ||
| .toList(); | ||
| } | ||
|
|
||
| private List<Long> findParentCommentIds(List<CommunityCommentView> comments) { | ||
| return comments.stream() | ||
| .filter(comment -> !comment.communityComment().getDeleted()) | ||
| .map(comment -> comment.communityComment().getParentCommentId()) | ||
| .filter(Objects::nonNull) | ||
| .toList(); | ||
| } | ||
|
|
||
| private Optional<CommunityCommentView> processDeletedComment(List<Long> parentCommentIds, CommunityCommentView commentView) { | ||
| CommunityComment comment = commentView.communityComment(); | ||
|
|
||
| if (comment.isDeleted()) { | ||
| if (parentCommentIds.contains(comment.getId())) { | ||
| return Optional.of(commentView.replaceWriterNickname(commentView)); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| return Optional.of(commentView); | ||
| } | ||
|
|
||
| private List<CommunityCommentResponseDto> createCommentHierarchy(List<CommunityCommentView> comments) { | ||
|
|
||
| Map<Long, CommunityCommentResponseDto> commentMap = new HashMap<>(); | ||
| List<CommunityCommentResponseDto> 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; | ||
| } | ||
| } | ||
|
|
9 changes: 9 additions & 0 deletions
9
src/main/java/com/somemore/community/usecase/comment/CommunityCommentQueryUseCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<CommunityCommentResponseDto> getCommunityCommentsByBoardId(Long boardId); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.