Skip to content
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 @@ -14,6 +14,8 @@ public interface PostLikeRepository extends JpaRepository<PostLike, Long> {

boolean existsByPostAndUser(Post post, User user);

boolean existsByPostIdAndUserId(Long postId, Long userId);

long countByPost(Post post);

Optional<PostLike> findByPostAndUser(Post post, User user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ public record FreePostDetailResponse(
List<PostImageResponse> images,
Integer viewCount,
Long likeCount,
boolean likedByMe,
Long commentCount,
LocalDateTime createdAt
) {
public static FreePostDetailResponse of(
FreePost post,
List<PostImage> images,
long likeCount,
long commentCount
long commentCount,
boolean likedByMe
) {
return new FreePostDetailResponse(
post.getId(),
Expand All @@ -31,6 +33,7 @@ public static FreePostDetailResponse of(
images.stream().map(PostImageResponse::from).toList(),
post.getViewCount(),
likeCount,
likedByMe,
commentCount,
post.getCreatedAt()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public FreePostDetailResponse create(

List<PostImage> images = saveImages(post, imageUrls, mainImageIndex);

return FreePostDetailResponse.of(post, images, 0L, 0L);
return FreePostDetailResponse.of(post, images, 0L, 0L, false);
}

public Page<FreePostListResponse> getList(Long viewerId, Pageable pageable) {
Expand Down Expand Up @@ -87,8 +87,9 @@ public FreePostDetailResponse getDetail(Long viewerId, Long postId) {
List<PostImage> images = postImageRepository.findByPostOrderBySortOrderAsc(post);
long likeCount = postLikeRepository.countByPost(post);
long commentCount = commentRepository.countByPostAndDeletedFalse(post);
boolean likedByMe = postLikeRepository.existsByPostIdAndUserId(post.getId(), viewerId);

return FreePostDetailResponse.of(post, images, likeCount, commentCount);
return FreePostDetailResponse.of(post, images, likeCount, commentCount, likedByMe);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void getListUsesViewerSpecificVisibleQuery() throws Exception {
Page<FreePost> page = new PageImpl<>(List.of(post), PageRequest.of(0, 10), 1);

when(freePostRepository.findVisibleByViewerId(1L, PageRequest.of(0, 10))).thenReturn(page);
when(postLikeRepository.countByPostIdsGrouped(anyList())).thenReturn(List.of(new Object[]{10L, 3L}));
when(commentRepository.countByPostIdsGrouped(anyList())).thenReturn(List.of(new Object[]{10L, 2L}));
when(postLikeRepository.countByPostIdsGrouped(anyList())).thenReturn(List.<Object[]>of(new Object[]{10L, 3L}));
when(commentRepository.countByPostIdsGrouped(anyList())).thenReturn(List.<Object[]>of(new Object[]{10L, 2L}));
when(postImageRepository.findMainImagesByPostIds(anyList())).thenReturn(List.of());
when(postImageRepository.countByPostIdsGrouped(anyList())).thenReturn(List.of());

Expand All @@ -78,6 +78,25 @@ void getListUsesViewerSpecificVisibleQuery() throws Exception {
verify(freePostRepository).findVisibleByViewerId(eq(1L), eq(PageRequest.of(0, 10)));
}

@Test
void getDetailReturnsLikedByMe() throws Exception {
User author = user(2L, "author");
FreePost post = freePost(10L, author, "제목", "본문");

when(freePostRepository.findById(10L)).thenReturn(Optional.of(post));
when(userBlockRepository.existsByBlocker_IdAndBlockedUser_Id(1L, 2L)).thenReturn(false);
when(postImageRepository.findByPostOrderBySortOrderAsc(post)).thenReturn(List.of());
when(postLikeRepository.countByPost(post)).thenReturn(3L);
when(commentRepository.countByPostAndDeletedFalse(post)).thenReturn(2L);
when(postLikeRepository.existsByPostIdAndUserId(10L, 1L)).thenReturn(true);

FreePostDetailResponse result = freePostService.getDetail(1L, 10L);

assertThat(result.likedByMe()).isTrue();
assertThat(result.likeCount()).isEqualTo(3L);
assertThat(result.commentCount()).isEqualTo(2L);
}

@Test
void getDetailThrowsWhenViewerBlockedAuthor() throws Exception {
User author = user(2L, "author");
Expand Down