Skip to content

Commit 0b50ac0

Browse files
authored
Feature/78 커뮤니티 댓글 조회 기능 (#108)
* refactor(community): communityBoardView 폴더 이동 * test(community): 댓글 조회 테스트 추가 * feat(community): 댓글 삭제 시 comment(내용) 변경 - 기존 댓글 내용 -> "삭제된 댓글입니다" * feat(community): 댓글 조회 View 및 responeDto 추가 * feat(community): 게시글 ID로 댓글 조회 메서드 추가 * feat(community): 댓글-대댓글 구조로 댓글 조회 service 및 usecase 추가 * refactor(community): sonar 반영 - View 생성자 제거 - isDeleted 메서드 추가 - test isEqual("") -> isEmpty() * refactor(community): 코드 리뷰 사항 반영 - markAsDeleted 메서드 내에서 content 내용 변경 - 개행 추가
1 parent a9f5743 commit 0b50ac0

15 files changed

+356
-11
lines changed

src/main/java/com/somemore/community/domain/CommunityComment.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ public boolean isWriter(UUID writerId) {
5151
public void updateWith(CommunityCommentUpdateRequestDto dto) {
5252
this.content = dto.content();
5353
}
54+
55+
@Override
56+
public void markAsDeleted() {
57+
super.markAsDeleted();
58+
this.content = "삭제된 댓글입니다";
59+
}
60+
61+
public boolean isDeleted() {
62+
return this.getDeleted();
63+
}
5464
}

src/main/java/com/somemore/community/dto/response/CommunityBoardGetResponseDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
44
import com.fasterxml.jackson.databind.annotation.JsonNaming;
5-
import com.somemore.community.domain.CommunityBoardView;
5+
import com.somemore.community.repository.mapper.CommunityBoardView;
66

77
import java.time.LocalDateTime;
88

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.somemore.community.dto.response;
2+
3+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
4+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
5+
import com.somemore.community.repository.mapper.CommunityCommentView;
6+
7+
import java.time.LocalDateTime;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
12+
public record CommunityCommentResponseDto(
13+
Long id,
14+
String writerNickname,
15+
String content,
16+
LocalDateTime updatedAt,
17+
List<CommunityCommentResponseDto> replies
18+
) {
19+
public CommunityCommentResponseDto {
20+
replies = replies == null ? new ArrayList<>() : replies;
21+
}
22+
23+
public static CommunityCommentResponseDto fromView(CommunityCommentView comment) {
24+
return new CommunityCommentResponseDto(
25+
comment.communityComment().getId(),
26+
comment.writerNickname(),
27+
comment.communityComment().getContent(),
28+
comment.communityComment().getUpdatedAt(),
29+
new ArrayList<>()
30+
);
31+
}
32+
33+
public void addReply(CommunityCommentResponseDto reply) {
34+
this.replies.add(reply);
35+
}
36+
}

src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.somemore.community.repository.board;
22

33
import com.somemore.community.domain.CommunityBoard;
4-
import com.somemore.community.domain.CommunityBoardView;
4+
import com.somemore.community.repository.mapper.CommunityBoardView;
55

66
import java.util.List;
77
import java.util.Optional;

src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.querydsl.jpa.impl.JPAQuery;
55
import com.querydsl.jpa.impl.JPAQueryFactory;
66
import com.somemore.community.domain.CommunityBoard;
7-
import com.somemore.community.domain.CommunityBoardView;
7+
import com.somemore.community.repository.mapper.CommunityBoardView;
88
import com.somemore.community.domain.QCommunityBoard;
99
import com.somemore.volunteer.domain.QVolunteer;
1010
import lombok.RequiredArgsConstructor;

src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.somemore.community.repository.comment;
22

33
import com.somemore.community.domain.CommunityComment;
4+
import com.somemore.community.repository.mapper.CommunityCommentView;
45

6+
import java.util.List;
57
import java.util.Optional;
68

79
public interface CommunityCommentRepository {
810
CommunityComment save(CommunityComment communityComment);
911
Optional<CommunityComment> findById(Long id);
12+
List<CommunityCommentView> findCommentsByBoardId(Long boardId);
1013
boolean existsById(Long id);
1114
default boolean doesNotExistById(Long id) {
1215
return !existsById(id);

src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.somemore.community.repository.comment;
22

3+
import com.querydsl.core.types.Projections;
34
import com.querydsl.jpa.impl.JPAQueryFactory;
45
import com.somemore.community.domain.CommunityComment;
56
import com.somemore.community.domain.QCommunityComment;
7+
import com.somemore.community.repository.mapper.CommunityCommentView;
8+
import com.somemore.volunteer.domain.QVolunteer;
69
import lombok.RequiredArgsConstructor;
710
import org.springframework.stereotype.Repository;
811

12+
import java.util.List;
913
import java.util.Optional;
1014

1115
@RequiredArgsConstructor
@@ -31,6 +35,21 @@ public Optional<CommunityComment> findById(Long id) {
3135
.fetchOne());
3236
}
3337

38+
public List<CommunityCommentView> findCommentsByBoardId(Long boardId) {
39+
QCommunityComment communityComment = QCommunityComment.communityComment;
40+
QVolunteer volunteer = QVolunteer.volunteer;
41+
42+
return queryFactory
43+
.select(Projections.constructor(CommunityCommentView.class,
44+
communityComment,
45+
volunteer.nickname))
46+
.from(communityComment)
47+
.join(volunteer).on(communityComment.writerId.eq(volunteer.id))
48+
.where(communityComment.communityBoardId.eq(boardId))
49+
.orderBy(communityComment.parentCommentId.asc().nullsFirst(), communityComment.createdAt.asc())
50+
.fetch();
51+
}
52+
3453
@Override
3554
public boolean existsById(Long id) {
3655
QCommunityComment communityComment = QCommunityComment.communityComment;

src/main/java/com/somemore/community/domain/CommunityBoardView.java renamed to src/main/java/com/somemore/community/repository/mapper/CommunityBoardView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package com.somemore.community.domain;
1+
package com.somemore.community.repository.mapper;
2+
3+
import com.somemore.community.domain.CommunityBoard;
24

35
public record CommunityBoardView(
46
CommunityBoard communityBoard,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.somemore.community.repository.mapper;
2+
3+
import com.somemore.community.domain.CommunityComment;
4+
import lombok.Builder;
5+
6+
@Builder
7+
public record CommunityCommentView(
8+
CommunityComment communityComment,
9+
String writerNickname
10+
) {
11+
public CommunityCommentView replaceWriterNickname(CommunityCommentView communityCommentView) {
12+
return CommunityCommentView.builder()
13+
.communityComment(communityCommentView.communityComment)
14+
.writerNickname("").build();
15+
}
16+
}

src/main/java/com/somemore/community/service/board/CommunityBoardQueryService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.somemore.community.service.board;
22

33
import com.somemore.community.domain.CommunityBoard;
4-
import com.somemore.community.domain.CommunityBoardView;
4+
import com.somemore.community.repository.mapper.CommunityBoardView;
55
import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto;
66
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
77
import com.somemore.community.repository.board.CommunityBoardRepository;

0 commit comments

Comments
 (0)