Skip to content

Commit

Permalink
Merge pull request #158 from SMWU-POCHAK/test/#157-comment-repository
Browse files Browse the repository at this point in the history
[Refactor/#157-comment-repository] 차단 회원의 자식 댓글 조회 막기
  • Loading branch information
yeahjinjeong authored Feb 9, 2025
2 parents ebba1d3 + 1ed78c5 commit 5749daa
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import static com.apps.pochak.global.Constant.DEFAULT_PAGING_SIZE;
import static com.apps.pochak.global.Constant.COMMENT_PAGING_SIZE;
import static com.apps.pochak.global.api_payload.code.status.SuccessStatus.SUCCESS_DELETE_COMMENT;
import static com.apps.pochak.global.api_payload.code.status.SuccessStatus.SUCCESS_UPLOAD_COMMENT;

Expand All @@ -29,7 +29,7 @@ public class CommentController {
public ApiResponse<CommentElements> getComments(
@Auth final Accessor accessor,
@PathVariable("postId") final Long postId,
@PageableDefault(DEFAULT_PAGING_SIZE) final Pageable pageable
@PageableDefault(COMMENT_PAGING_SIZE) final Pageable pageable
) {
return ApiResponse.onSuccess(
commentService.getComments(
Expand All @@ -46,7 +46,7 @@ public ApiResponse<ParentCommentElement> getChildComments(
@Auth final Accessor accessor,
@PathVariable("postId") final Long postId,
@PathVariable("parentCommentId") final Long parentCommentId,
@PageableDefault(DEFAULT_PAGING_SIZE) final Pageable pageable
@PageableDefault(COMMENT_PAGING_SIZE) final Pageable pageable
) {
return ApiResponse.onSuccess(
commentService.getChildCommentsByParentCommentId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,28 @@ Page<Comment> findParentCommentByPost(
Pageable pageable
);

@Query("select c from Comment c " +
"join fetch c.member " +
"where c.id = :commentId " +
" and c.parentComment is null " +
" and c.member not in (select b.blockedMember from Block b where b.blocker = :loginMember)" +
" and :loginMember not in (select b.blockedMember from Block b where b.blocker = c.member) ")
@Query("""
select c from Comment c
join fetch c.member
where c.id = :commentId
and c.parentComment is null
and c.member not in (select b.blockedMember from Block b where b.blocker = :loginMember)
and :loginMember not in (select b.blockedMember from Block b where b.blocker = c.member)
order by c.createdDate desc
""")
Optional<Comment> findParentCommentById(
@Param("commentId") final Long commentId,
@Param("loginMember") final Member loginMember
);

@Query("select c from Comment c " +
"join fetch c.member " +
"where c.parentComment = :parentComment " +
" and c.member not in (select b.blockedMember from Block b where b.blocker = :loginMember) " +
" and :loginMember not in (select b.blockedMember from Block b where b.blocker = c.member)")
@Query("""
select c from Comment c
join fetch c.member
where c.parentComment = :parentComment
and c.member not in (select b.blockedMember from Block b where b.blocker = :loginMember)
and :loginMember not in (select b.blockedMember from Block b where b.blocker = c.member)
order by c.createdDate desc
""")
Page<Comment> findChildCommentByParentComment(@Param("parentComment") Comment parentComment,
@Param("loginMember") Member loginMember,
Pageable pageable);
Expand All @@ -73,14 +79,14 @@ Page<Comment> findChildCommentByParentComment(@Param("parentComment") Comment pa
select child_comments.*
from (
select c.*,
ROW_NUMBER() OVER (partition by c.parent_comment_id) as row_num
ROW_NUMBER() OVER (partition by c.parent_comment_id order by c.created_date desc) as row_num
from comment c
join member m on c.member_id = m.id
where c.parent_comment_id in ?1
and c.member_id not in (select b.blocked_id from block b where b.blocker_id = ?2)
and ?2 not in (select b.blocked_id from block b where b.blocker_id = c.member_id)
) child_comments
where child_comments.row_num <= 30""", nativeQuery = true)
where child_comments.row_num = 1""", nativeQuery = true)
List<Comment> findChildCommentByParentComments(List<Long> parentCommentIds, Long loginMemberId);

@Modifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public CommentElements(
for (Comment childComment : childComments) {
if (Objects.equals(parentComment.getId(), childComment.getParentComment().getId())) {
childCommentList.add(childComment);
break;
}
}
parentCommentList.add(new ParentCommentElement(parentComment, childCommentList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ParentCommentElement(
this(
parentComment,
childCommentList,
PageRequest.of(0, Constant.DEFAULT_PAGING_SIZE)
PageRequest.of(0, Constant.COMMENT_PAGING_SIZE)
);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/apps/pochak/global/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class Constant {
public static final String HEADER_APPLE_AUTHORIZATION_CODE = "AuthorizationCode";

public static final int DEFAULT_PAGING_SIZE = 30;
public static final int COMMENT_PAGING_SIZE = 10;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.apps.pochak.member.domain.repository.MemberRepository;
import com.apps.pochak.post.domain.Post;
import com.apps.pochak.post.domain.repository.PostRepository;
import com.apps.pochak.post.fixture.PostFixture;
import com.apps.pochak.tag.domain.repository.TagRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -21,10 +22,7 @@

import static com.apps.pochak.global.Constant.DEFAULT_PAGING_SIZE;
import static com.apps.pochak.member.fixture.MemberFixture.*;
import static com.apps.pochak.post.fixture.PostFixture.CAPTION;
import static com.apps.pochak.post.fixture.PostFixture.POST_IMAGE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@Transactional
@SpringBootTest
Expand Down Expand Up @@ -70,9 +68,11 @@ void findChildCommentByParentComments() {
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE));
List<Comment> childCommentByParentComment = commentRepository.findChildCommentByParentComments(parentCommentByPost.stream().map(Comment::getId).toList(), loginMember.getId());
//then
assertTrue(parentCommentByPost.hasContent());
assertEquals(parentCommentByPost.getContent().get(0), parentComment);
assertEquals(childCommentByParentComment.size(), 1);
assertAll(
() -> assertTrue(parentCommentByPost.hasContent()),
() -> assertEquals(parentCommentByPost.getContent().get(0), parentComment),
() -> assertEquals(1, childCommentByParentComment.size())
);
}

@DisplayName("[전체 댓글 조회] 부모 댓글의 특정 자식 댓글 차단시")
Expand All @@ -81,64 +81,88 @@ void findChildCommentByParentCommentsWhenBlocked() {
//given
block(childCommenter, loginMember);
//when
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE));
List<Comment> childCommentByParentComment = commentRepository.findChildCommentByParentComments(parentCommentByPost.stream().map(Comment::getId).toList(), loginMember.getId());
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(
post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE)
);
List<Comment> childCommentByParentComment = commentRepository.findChildCommentByParentComments(
parentCommentByPost.stream().map(Comment::getId).toList(), loginMember.getId()
);

//then
assertTrue(parentCommentByPost.hasContent());
assertEquals(parentCommentByPost.getContent().get(0), parentComment);
assertEquals(childCommentByParentComment.size(), 0);
assertAll(
() -> assertTrue(parentCommentByPost.hasContent()),
() -> assertEquals(parentCommentByPost.getContent().get(0), parentComment),
() -> assertEquals(0, childCommentByParentComment.size())
);
}

@DisplayName("[부모 댓글 조회] 정상 테스트")
@Test
void findParentCommentByPost() {
//when
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE));
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(
post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE)
);

//then
assertTrue(parentCommentByPost.hasContent());
assertEquals(parentCommentByPost.getTotalElements(), 1);
assertEquals(parentCommentByPost.getContent().get(0), parentComment);
assertAll(
() -> assertTrue(parentCommentByPost.hasContent()),
() -> assertEquals(parentCommentByPost.getTotalElements(), 1),
() -> assertEquals(parentComment, parentCommentByPost.getContent().get(0))
);
}

@DisplayName("[부모 댓글 조회] 부모 댓글 작성자 차단시")
@Test
void findParentCommentByPostWhenBlocked() {
//given
block(parentCommenter, loginMember);

//when
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE));

//then
assertEquals(parentCommentByPost.getTotalElements(), 0);
assertEquals(0, parentCommentByPost.getTotalElements());
}

@DisplayName("[자식 댓글 조회] 정상 테스트")
@Test
void findChildCommentByParentComment() {
//when
Page<Comment> childCommentByParentComment = commentRepository.findChildCommentByParentComment(parentComment, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE));
Page<Comment> childCommentByParentComment = commentRepository.findChildCommentByParentComment(
parentComment, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE));

//then
assertEquals(childCommentByParentComment.getContent().size(), 1);
assertEquals(childCommentByParentComment.getContent().get(0), childComment);
assertEquals(childComment, childCommentByParentComment.getContent().get(0));
}

@DisplayName("[자식 댓글 조회] 자식 댓글 작성자 차단시")
@Test
void findChildCommentByParentCommentWhenBlocked() {
//given
block(childCommenter, loginMember);

//when
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE));
Page<Comment> parentCommentByPost = commentRepository.findParentCommentByPost(
post, loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE)
);
Page<Comment> childCommentByParentComment = commentRepository.findChildCommentByParentComment(
parentCommentByPost.getContent().get(0), loginMember, PageRequest.of(0, DEFAULT_PAGING_SIZE)
);
parentCommentByPost.getContent().get(0),
loginMember,
PageRequest.of(0, DEFAULT_PAGING_SIZE)
);

//then
assertTrue(parentCommentByPost.hasContent());
assertEquals(parentCommentByPost.getContent().get(0), parentComment);
assertEquals(childCommentByParentComment.getContent().size(), 0);
assertAll(
() -> assertTrue(parentCommentByPost.hasContent()),
() -> assertEquals(parentCommentByPost.getContent().get(0), parentComment),
() -> assertEquals(0, childCommentByParentComment.getContent().size())
);
}

private Post savePost(Member owner) {
Post post = postRepository.save(new Post(owner, POST_IMAGE, CAPTION));
Post post = postRepository.save(PostFixture.get(owner));
post.makePublic();
return post;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.apps.pochak.member.domain.repository.MemberRepository;
import com.apps.pochak.post.domain.Post;
import com.apps.pochak.post.domain.repository.PostRepository;
import com.apps.pochak.post.fixture.PostFixture;
import com.apps.pochak.tag.domain.repository.TagRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -20,10 +21,8 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import static com.apps.pochak.global.Constant.DEFAULT_PAGING_SIZE;
import static com.apps.pochak.global.Constant.COMMENT_PAGING_SIZE;
import static com.apps.pochak.member.fixture.MemberFixture.*;
import static com.apps.pochak.post.fixture.PostFixture.CAPTION;
import static com.apps.pochak.post.fixture.PostFixture.POST_IMAGE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -79,7 +78,7 @@ void getComments() {
.getComments(
Accessor.member(loginMember.getId()),
post.getId(),
PageRequest.of(0, DEFAULT_PAGING_SIZE)
PageRequest.of(0, COMMENT_PAGING_SIZE)
);
// then
assertAll(
Expand All @@ -99,7 +98,7 @@ void getCommentsWhenBlockParentCommenter() {
.getComments(
Accessor.member(loginMember.getId()),
post.getId(),
PageRequest.of(0, DEFAULT_PAGING_SIZE)
PageRequest.of(0, COMMENT_PAGING_SIZE)
);
// then
assertThat(actual.getParentCommentList()).hasSize(0);
Expand All @@ -115,7 +114,7 @@ void getCommentsWhenBlockedByParentCommenter() {
.getComments(
Accessor.member(loginMember.getId()),
post.getId(),
PageRequest.of(0, DEFAULT_PAGING_SIZE)
PageRequest.of(0, COMMENT_PAGING_SIZE)
);
// then
assertThat(actual.getParentCommentList()).hasSize(0);
Expand All @@ -131,7 +130,7 @@ void getCommentsWhenBlockChildCommenter() {
.getComments(
Accessor.member(loginMember.getId()),
post.getId(),
PageRequest.of(0, DEFAULT_PAGING_SIZE)
PageRequest.of(0, COMMENT_PAGING_SIZE)
);
// then
assertAll(
Expand All @@ -151,7 +150,7 @@ void getCommentsWhenBlockedByChildCommenter() {
.getComments(
Accessor.member(loginMember.getId()),
post.getId(),
PageRequest.of(0, DEFAULT_PAGING_SIZE)
PageRequest.of(0, COMMENT_PAGING_SIZE)
);
// then
assertAll(
Expand All @@ -162,7 +161,7 @@ void getCommentsWhenBlockedByChildCommenter() {
}

private Post savePost(Member owner) {
Post post = postRepository.save(new Post(owner, POST_IMAGE, CAPTION));
Post post = postRepository.save(PostFixture.get(owner));
post.makePublic();
return post;
}
Expand Down

0 comments on commit 5749daa

Please sign in to comment.