Skip to content

Commit f9568ce

Browse files
authored
Merge pull request #132 from SEMOSAN/feat/#120-free-post-liked-by-me
Feat/#120 free post liked by me
2 parents 8877dec + eae202d commit f9568ce

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/main/java/com/semosan/api/domain/community/like/repository/PostLikeRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public interface PostLikeRepository extends JpaRepository<PostLike, Long> {
1414

1515
boolean existsByPostAndUser(Post post, User user);
1616

17+
boolean existsByPostIdAndUserId(Long postId, Long userId);
18+
1719
long countByPost(Post post);
1820

1921
Optional<PostLike> findByPostAndUser(Post post, User user);

src/main/java/com/semosan/api/domain/community/post/dto/FreePostDetailResponse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ public record FreePostDetailResponse(
1414
List<PostImageResponse> images,
1515
Integer viewCount,
1616
Long likeCount,
17+
boolean likedByMe,
1718
Long commentCount,
1819
LocalDateTime createdAt
1920
) {
2021
public static FreePostDetailResponse of(
2122
FreePost post,
2223
List<PostImage> images,
2324
long likeCount,
24-
long commentCount
25+
long commentCount,
26+
boolean likedByMe
2527
) {
2628
return new FreePostDetailResponse(
2729
post.getId(),
@@ -31,6 +33,7 @@ public static FreePostDetailResponse of(
3133
images.stream().map(PostImageResponse::from).toList(),
3234
post.getViewCount(),
3335
likeCount,
36+
likedByMe,
3437
commentCount,
3538
post.getCreatedAt()
3639
);

src/main/java/com/semosan/api/domain/community/post/service/FreePostService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public FreePostDetailResponse create(
5555

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

58-
return FreePostDetailResponse.of(post, images, 0L, 0L);
58+
return FreePostDetailResponse.of(post, images, 0L, 0L, false);
5959
}
6060

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

91-
return FreePostDetailResponse.of(post, images, likeCount, commentCount);
92+
return FreePostDetailResponse.of(post, images, likeCount, commentCount, likedByMe);
9293
}
9394

9495
@Transactional

src/test/java/com/semosan/api/domain/community/post/service/FreePostServiceTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ void getListUsesViewerSpecificVisibleQuery() throws Exception {
6767
Page<FreePost> page = new PageImpl<>(List.of(post), PageRequest.of(0, 10), 1);
6868

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

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

81+
@Test
82+
void getDetailReturnsLikedByMe() throws Exception {
83+
User author = user(2L, "author");
84+
FreePost post = freePost(10L, author, "제목", "본문");
85+
86+
when(freePostRepository.findById(10L)).thenReturn(Optional.of(post));
87+
when(userBlockRepository.existsByBlocker_IdAndBlockedUser_Id(1L, 2L)).thenReturn(false);
88+
when(postImageRepository.findByPostOrderBySortOrderAsc(post)).thenReturn(List.of());
89+
when(postLikeRepository.countByPost(post)).thenReturn(3L);
90+
when(commentRepository.countByPostAndDeletedFalse(post)).thenReturn(2L);
91+
when(postLikeRepository.existsByPostIdAndUserId(10L, 1L)).thenReturn(true);
92+
93+
FreePostDetailResponse result = freePostService.getDetail(1L, 10L);
94+
95+
assertThat(result.likedByMe()).isTrue();
96+
assertThat(result.likeCount()).isEqualTo(3L);
97+
assertThat(result.commentCount()).isEqualTo(2L);
98+
}
99+
81100
@Test
82101
void getDetailThrowsWhenViewerBlockedAuthor() throws Exception {
83102
User author = user(2L, "author");

0 commit comments

Comments
 (0)