Skip to content

Commit

Permalink
KKUMI-118 [FEATURE] #73 포스트 조회시 좋아요 개수도 조회되도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
eekrwl committed Nov 11, 2024
1 parent f3c77b8 commit 20d79b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class Post extends BaseEntity {
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostImage> postImages = new ArrayList<>();

private Long likeCount = 0L;

public List<PostImage> getOrderedPostImages() {
return postImages.stream()
.sorted(Comparator.comparing(PostImage::getOrderList))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ public void updateLikedNByCurrentUser(Boolean likedByCurrentUser) {
this.likedByCurrentUser = likedByCurrentUser;
}

public void updateLikeCount(Long likeCount) {
this.likeCount = likeCount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ public PostListResponse getInfiniteScrollPosts(User user, String encodedCursor,
categoryIds = UserSubCategory.stringToList(userSubCategories.getSubCategory());
posts = getPostsByCursorAndLimitAndUserCategory(cursor, limit + 1, categoryIds); //포스트 가져오기
}

setLikes(posts, user); //좋아요 눌렀는지 세팅
if (posts.size() < limit + 1) { //다음에 조회할 내용 없음
return PostListResponse.end(posts);
}
posts.removeLast();

setLikes(posts, user); //좋아요 눌렀는지 세팅

Long lastId = getLastIdFromPostList(posts);
PostLatestCursor nextCursor = PostLatestCursor.of(cursor.getStartedAt(), lastId);
return PostListResponse.of(posts, Base64Utils.encode(nextCursor));
}

private void setLikes(List<PostDto> posts, User user) {
setLikesCount(posts);

if (user == null) {
for (PostDto post : posts) {
post.updateLikedNByCurrentUser(false);
Expand All @@ -89,6 +89,15 @@ private void setLikes(List<PostDto> posts, User user) {
}
}

private void setLikesCount(List<PostDto> posts) {
//TODO 모아서 한번에 10개 디비에서 조회해오기 vs 그냥 하나씩 하기
for (int i = 0; i < posts.size(); i++) {
Long postId = posts.get(i).getId();
Long postLikeCount = likesService.getPostLikeCount(postId);
posts.get(i).updateLikeCount(postLikeCount);
}
}

public Long registerPost(User user, Long subCategoryId, String content, List<PostImageDto> images) {

if (subCategoryId == null) {
Expand Down

0 comments on commit 20d79b9

Please sign in to comment.