diff --git a/src/main/java/com/swmarastro/mykkumiserver/post/domain/Post.java b/src/main/java/com/swmarastro/mykkumiserver/post/domain/Post.java index fcff1ad..fa10e31 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/post/domain/Post.java +++ b/src/main/java/com/swmarastro/mykkumiserver/post/domain/Post.java @@ -39,6 +39,8 @@ public class Post extends BaseEntity { @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true) private List postImages = new ArrayList<>(); + private Long likeCount = 0L; + public List getOrderedPostImages() { return postImages.stream() .sorted(Comparator.comparing(PostImage::getOrderList)) diff --git a/src/main/java/com/swmarastro/mykkumiserver/post/dto/PostDto.java b/src/main/java/com/swmarastro/mykkumiserver/post/dto/PostDto.java index 56a70f3..4a96e2e 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/post/dto/PostDto.java +++ b/src/main/java/com/swmarastro/mykkumiserver/post/dto/PostDto.java @@ -58,4 +58,8 @@ public void updateLikedNByCurrentUser(Boolean likedByCurrentUser) { this.likedByCurrentUser = likedByCurrentUser; } + public void updateLikeCount(Long likeCount) { + this.likeCount = likeCount; + } + } diff --git a/src/main/java/com/swmarastro/mykkumiserver/post/service/PostService.java b/src/main/java/com/swmarastro/mykkumiserver/post/service/PostService.java index dd6bf90..caad056 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/post/service/PostService.java +++ b/src/main/java/com/swmarastro/mykkumiserver/post/service/PostService.java @@ -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 posts, User user) { + setLikesCount(posts); + if (user == null) { for (PostDto post : posts) { post.updateLikedNByCurrentUser(false); @@ -89,6 +89,15 @@ private void setLikes(List posts, User user) { } } + private void setLikesCount(List 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 images) { if (subCategoryId == null) {