Skip to content
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

Be/#170 Comment 도메인 리팩토링 #171

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public interface CommentWithMaskingDto {
String getContent();
String getNickname();
Long getCommentId();
Integer getChildCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;

public interface ReplyListDto {
String getNickname();
String getContent();
Long getCommentId();
LocalDateTime getCreatedAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

public interface CommentRepository extends JpaRepository<Comment, Long>, CommentCustomRepository {

@Query(value = "SELECT\n" +
" CASE\n" +
@Query(value = "SELECT CASE\n" +
" WHEN parent.is_deleted\n" +
" THEN '삭제된 댓글입니다.'\n" +
" ELSE parent.content\n" +
" END AS content,\n" +
" parent.comment_id AS commentId,\n" +
" parent.created_at AS createdAt, \n" +
" parent.comment_id AS commentId,\n" +
" parent.created_at AS createdAt,\n" +
" member.nickname AS nickname,\n" +
" COUNT(child.comment_id) AS childCount\n" +
"FROM (SELECT *\n" +
" FROM comment\n" +
Expand All @@ -28,18 +28,25 @@ public interface CommentRepository extends JpaRepository<Comment, Long>, Comment
" comment AS child\n" +
" ON\n" +
" parent.comment_id = child.parent_id\n" +
"GROUP BY parent.comment_id", nativeQuery = true)
" JOIN\n" +
" member\n" +
" ON\n" +
" parent.member_id = member.id\n" +
"GROUP BY parent.comment_id;\n", nativeQuery = true)
List<CommentWithMaskingDto> findCommentsWithMasking(Long id);

@Query(value = "SELECT\n" +
" CASE\n" +
" WHEN is_deleted\n" +
" THEN '삭제된 댓글입니다.'\n" +
" ELSE content\n" +
" END AS content,\n" +
" comment_id AS commentId,\n" +
" created_at AS createdAt\n" +
"FROM comment\n" +
"WHERE parent_id = :parentId", nativeQuery = true)
List<ReplyListDto> findReplyList(Long parentId);
@Query(value = "SELECT CASE\n" +
" WHEN comment.is_deleted\n" +
" THEN '삭제된 댓글입니다.'\n" +
" ELSE comment.content\n" +
" END AS content,\n" +
" comment_id AS commentId,\n" +
" comment.created_at AS createdAt,\n" +
" member.nickname AS nickname\n" +
"FROM comment\n" +
" JOIN member ON comment.member_id = member.id\n" +
"WHERE parent_id = :parentId\n" +
"ORDER BY comment.created_at ASC", nativeQuery = true)
List<ReplyListDto> findReplyList(Long parentId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.graphy.backend.domain.comment.dto.ReplyListDto;
import com.graphy.backend.domain.comment.service.CommentService;
import com.graphy.backend.domain.project.service.ProjectService;
import com.graphy.backend.global.auth.jwt.TokenProvider;
import com.graphy.backend.global.auth.redis.repository.RefreshTokenRepository;
import com.graphy.backend.test.MockApiTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -43,7 +45,10 @@ class CommentControllerTest extends MockApiTest {
@MockBean
private CommentService commentService;
@MockBean
private ProjectService projectService;
private TokenProvider tokenProvider;

@MockBean
private RefreshTokenRepository refreshTokenRepository;


@BeforeEach
Expand Down Expand Up @@ -113,6 +118,11 @@ void deleteCommentTest() throws Exception {
public void getReplyList() throws Exception {
//given
List<ReplyListDto> dtoList = Arrays.asList(new ReplyListDto() {
@Override
public String getNickname() {
return null;
}

@Override
public String getContent() {
return "test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.graphy.backend.domain.comment.dto.CommentDto;
import com.graphy.backend.domain.comment.dto.ReplyListDto;
import com.graphy.backend.domain.comment.repository.CommentRepository;
import com.graphy.backend.domain.member.domain.Member;
import com.graphy.backend.domain.project.domain.Project;
import com.graphy.backend.domain.project.repository.ProjectRepository;
import com.graphy.backend.global.auth.jwt.CustomUserDetailsService;
import com.graphy.backend.test.MockTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -19,6 +21,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.InvalidMarkException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,16 +42,18 @@ class CommentServiceTest extends MockTest {
private CommentRepository commentRepository;
@Mock
private ProjectRepository projectRepository;
@Mock
private CustomUserDetailsService customUserDetailsService;

@Test
@WithMockUser(username = "[email protected]", password = "1234", roles = "USER")
@DisplayName("댓글이 생성된다")
void createCommentTest() {

//given
CreateCommentRequest dto = new CreateCommentRequest("내용", 1L, null);
Project project = Project.builder().id(1L).content("테스트 프로젝트").description("테스트 프로젝트 한 줄 소개").projectName("테스트").build();
Comment comment = Comment.builder().id(1L).content("내용").project(project).parent(null).build();
Member member = Member.builder().email("youKeon").build();

// mocking
given(commentRepository.save(any()))
Expand All @@ -57,6 +62,8 @@ void createCommentTest() {
.willReturn(Optional.ofNullable(project));
given(commentRepository.findById(1L))
.willReturn(Optional.ofNullable(comment));
given(customUserDetailsService.getLoginUser())
.willReturn(member);

//when
CreateCommentResponse response = commentService.createComment(dto);
Expand Down Expand Up @@ -140,6 +147,11 @@ public void getReplyList() throws Exception {

List<ReplyListDto> list = new ArrayList<>();
ReplyListDto dto1 = new ReplyListDto() {
@Override
public String getNickname() {
return null;
}

@Override
public String getContent() {
return "comment1";
Expand All @@ -157,6 +169,11 @@ public LocalDateTime getCreatedAt() {
};

ReplyListDto dto2 = new ReplyListDto() {
@Override
public String getNickname() {
return null;
}

@Override
public String getContent() {
return "comment2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public String getContent() {
return "testComment";
}

@Override
public String getNickname() {
return null;
}

@Override
public Long getCommentId() {
return null;
Expand Down
Loading