Skip to content

Commit

Permalink
refactor : Comment 도메인 QueryDSL 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
youKeon committed Sep 11, 2023
1 parent 9e01080 commit a7fbb40
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.graphy.backend.domain.comment.dto.response;

import com.querydsl.core.annotations.QueryProjection;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -9,12 +10,20 @@

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GetCommentWithMaskingResponse {
private String content;
private Long commentId;
private LocalDateTime createdAt;
private String nickname;
private Long childCount;

@QueryProjection
public GetCommentWithMaskingResponse(String content, Long commentId, LocalDateTime createdAt, String nickname, Long childCount) {
this.content = content;
this.commentId = commentId;
this.createdAt = createdAt;
this.nickname = nickname;
this.childCount = childCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.graphy.backend.domain.comment.dto.response;

import com.graphy.backend.domain.comment.domain.Comment;
import com.querydsl.core.annotations.QueryProjection;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -10,14 +11,21 @@

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GetReplyListResponse {
private String nickname;
private String content;
private Long commentId;
private LocalDateTime createdAt;

@QueryProjection
public GetReplyListResponse(String nickname, String content, Long commentId, LocalDateTime createdAt) {
this.nickname = nickname;
this.content = content;
this.commentId = commentId;
this.createdAt = createdAt;
}

public static GetReplyListResponse from(Comment comment) {
return GetReplyListResponse.builder()
.nickname(comment.getMember().getNickname())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.graphy.backend.domain.comment.repository;


import com.graphy.backend.domain.comment.domain.Comment;
import com.graphy.backend.domain.comment.dto.response.GetCommentWithMaskingResponse;
import com.graphy.backend.domain.comment.dto.response.GetReplyListResponse;

import java.util.List;

public interface CommentCustomRepository {
List<GetCommentWithMaskingResponse> findCommentsWithMasking(Long id);
List<Comment> findReplyList(Long parentId);
List<GetReplyListResponse> findReplyList(Long parentId);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.graphy.backend.domain.comment.repository;

import com.graphy.backend.domain.comment.domain.Comment;
import com.graphy.backend.domain.comment.domain.QComment;
import com.graphy.backend.domain.comment.dto.response.GetCommentWithMaskingResponse;
import com.querydsl.core.types.Projections;
import com.graphy.backend.domain.comment.dto.response.GetReplyListResponse;
import com.graphy.backend.domain.comment.dto.response.QGetCommentWithMaskingResponse;
import com.graphy.backend.domain.comment.dto.response.QGetReplyListResponse;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,7 +23,7 @@ public List<GetCommentWithMaskingResponse> findCommentsWithMasking(Long id) {
QComment child = new QComment("child");

return jpaQueryFactory
.select(Projections.constructor(GetCommentWithMaskingResponse.class,
.select(new QGetCommentWithMaskingResponse(
new CaseBuilder()
.when(comment.isDeleted.isTrue()).then("삭제된 댓글입니다.")
.otherwise(comment.content),
Expand All @@ -42,10 +43,16 @@ public List<GetCommentWithMaskingResponse> findCommentsWithMasking(Long id) {
}

@Override
public List<Comment> findReplyList(Long parentId) {
public List<GetReplyListResponse> findReplyList(Long parentId) {
return jpaQueryFactory
.selectFrom(comment)
.join(comment.member, member).fetchJoin()
.select(new QGetReplyListResponse(
member.nickname,
comment.content,
comment.id,
comment.createdAt
))
.from(comment)
.join(comment.member, member)
.where(comment.parent.id.eq(parentId))
.orderBy(comment.createdAt.asc())
.fetch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ public void deleteComment(Long id) {
}

public List<GetReplyListResponse> findCommentList(Long commentId) {
List<Comment> commentList = commentRepository.findReplyList(commentId);
List<GetReplyListResponse> commentList = commentRepository.findReplyList(commentId);

if (commentList.size() == 0) throw new EmptyResultException(ErrorCode.COMMENT_DELETED_OR_NOT_EXIST);
if (commentList.isEmpty()) throw new EmptyResultException(ErrorCode.COMMENT_DELETED_OR_NOT_EXIST);

return commentList.stream()
.map(GetReplyListResponse::from)
.collect(Collectors.toList());
return commentList;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CreateProjectRequest {

private String thumbNail;

public Project toEntity( Member member) {
public Project toEntity(Member member) {
return Project.builder()
.member(member)
.projectName(projectName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.graphy.backend.domain.comment.domain.Comment;
import com.graphy.backend.domain.comment.dto.response.GetCommentWithMaskingResponse;
import com.graphy.backend.domain.comment.dto.response.GetReplyListResponse;
import com.graphy.backend.domain.member.domain.Member;
import com.graphy.backend.domain.member.domain.Role;
import com.graphy.backend.domain.member.repository.MemberRepository;
Expand Down Expand Up @@ -149,25 +150,22 @@ public void findCommentWithMakingEmptyListTest() throws Exception {
@DisplayName("댓글 ID를 입력 받아 답글 목록을 조회한다")
public void findReCommentListTest() throws Exception {
// when
List<Comment> actual = commentRepository.findReplyList(parentComment.getId());
List<GetReplyListResponse> actual = commentRepository.findReplyList(parentComment.getId());

// then
assertThat(actual.size()).isEqualTo(3);

assertThat(actual.get(0).getId()).isEqualTo(comment1.getId());
assertThat(actual.get(0).getNickname()).isEqualTo(member.getNickname());
assertThat(actual.get(0).getContent()).isEqualTo(comment1.getContent());
assertThat(actual.get(0).getParent()).isEqualTo(comment1.getParent());
assertThat(actual.get(0).getProject()).isEqualTo(comment1.getProject());
assertThat(actual.get(0).getCommentId()).isEqualTo(comment1.getId());

assertThat(actual.get(1).getId()).isEqualTo(comment2.getId());
assertThat(actual.get(1).getNickname()).isEqualTo(member.getNickname());
assertThat(actual.get(1).getContent()).isEqualTo(comment2.getContent());
assertThat(actual.get(1).getParent()).isEqualTo(comment2.getParent());
assertThat(actual.get(1).getProject()).isEqualTo(comment2.getProject());
assertThat(actual.get(1).getCommentId()).isEqualTo(comment2.getId());

assertThat(actual.get(2).getId()).isEqualTo(comment3.getId());
assertThat(actual.get(2).getNickname()).isEqualTo(member.getNickname());
assertThat(actual.get(2).getContent()).isEqualTo(comment3.getContent());
assertThat(actual.get(2).getParent()).isEqualTo(comment3.getParent());
assertThat(actual.get(2).getProject()).isEqualTo(comment3.getProject());
assertThat(actual.get(2).getCommentId()).isEqualTo(comment3.getId());
}

@Test
Expand All @@ -177,7 +175,7 @@ public void findReCommentListEmptyListTest() throws Exception {
Long 답글이_존재하지_않는_댓글_ID = 0L;

// when
List<Comment> actual = commentRepository.findReplyList(답글이_존재하지_않는_댓글_ID);
List<GetReplyListResponse> actual = commentRepository.findReplyList(답글이_존재하지_않는_댓글_ID);

// then
assertThat(actual.size()).isEqualTo(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,43 +176,18 @@ void removeReCommentNotExistParentCommentExceptionTest() {
@DisplayName("답글 목록을 조회한다")
void findReCommentListTest() {
//given
Comment reComment1 = Comment.builder()
.id(2L)
.content("reComment1")
.parent(parentComment)
.member(member)
.build();

Comment reComment2 = Comment.builder()
.id(3L)
.content("reComment2")
.parent(parentComment).member(member)
.build();

Comment reComment3 = Comment.builder()
.id(4L)
.content("reComment3")
.parent(parentComment)
.member(member)
.build();

List<Comment> commentList = new LinkedList<>(List.of(reComment1, reComment2, reComment3));
List<GetReplyListResponse> reCommentList = new LinkedList<>(Arrays.asList(
GetReplyListResponse.builder().nickname(member.getNickname()).content("reComment1").build(),
GetReplyListResponse.builder().nickname(member.getNickname()).content("reComment2").build(),
GetReplyListResponse.builder().nickname(member.getNickname()).content("reComment3").build()
));

//when
when(commentRepository.findReplyList(parentComment.getId())).thenReturn(commentList);
when(commentRepository.findReplyList(parentComment.getId())).thenReturn(reCommentList);
List<GetReplyListResponse> actual = commentService.findCommentList(parentComment.getId());

//then
assertThat(actual.size()).isEqualTo(commentList.size());

assertThat(actual.get(0).getCommentId()).isEqualTo(reComment1.getId());
assertThat(actual.get(0).getContent()).isEqualTo(reComment1.getContent());

assertThat(actual.get(1).getCommentId()).isEqualTo(reComment2.getId());
assertThat(actual.get(1).getContent()).isEqualTo(reComment2.getContent());

assertThat(actual.get(2).getCommentId()).isEqualTo(reComment3.getId());
assertThat(actual.get(2).getContent()).isEqualTo(reComment3.getContent());
assertThat(actual).usingRecursiveComparison().isEqualTo(reCommentList);
}

@Test
Expand Down

0 comments on commit a7fbb40

Please sign in to comment.