diff --git a/src/main/java/com/swmarastro/mykkumiserver/like/LikesRepository.java b/src/main/java/com/swmarastro/mykkumiserver/like/LikesRepository.java index 5abd588..054f7ac 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/like/LikesRepository.java +++ b/src/main/java/com/swmarastro/mykkumiserver/like/LikesRepository.java @@ -7,6 +7,8 @@ import org.springframework.data.jpa.repository.Lock; import org.springframework.data.jpa.repository.Query; +import java.util.List; +import java.util.Map; import java.util.Optional; public interface LikesRepository extends JpaRepository { @@ -14,4 +16,11 @@ public interface LikesRepository extends JpaRepository { @Lock(LockModeType.OPTIMISTIC) @Query("SELECT l from Likes l WHERE l.post = :post AND l.user = :user") Optional findByPostAndUser(Post post, User user); //TODO postid, userid로 하는게 나을지??? + + @Query(value = "SELECT p.post_id AS postId, " + + " CASE WHEN l.user_id = :userId AND l.is_deleted = false THEN true ELSE false END AS isLiked " + + "FROM post p " + + "LEFT JOIN likes l ON p.post_id = l.post_id AND l.user_id = :userId " + + "WHERE p.post_id IN :postIds", nativeQuery = true) + List> findPostLikesStatus(List postIds, Long userId); } diff --git a/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java b/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java index ebe9df9..485de7b 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java +++ b/src/main/java/com/swmarastro/mykkumiserver/like/LikesService.java @@ -2,14 +2,17 @@ import com.swmarastro.mykkumiserver.global.exception.CommonException; import com.swmarastro.mykkumiserver.global.exception.ErrorCode; +import com.swmarastro.mykkumiserver.post.PostRepository; import com.swmarastro.mykkumiserver.post.domain.Post; -import com.swmarastro.mykkumiserver.post.service.PostService; import com.swmarastro.mykkumiserver.user.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -17,11 +20,11 @@ public class LikesService { private final LikesRepository likeRepository; - private final PostService postService; + private final PostRepository postRepository; public Boolean like(User user, Long postId) { - Post post = postService.findById(postId); + Post post = postRepository.findById(postId).orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND, "해당 포스트가 존재하지 않습니다.", "해당 포스트가 존재하지 않습니다.")); //like가 테이블에 있는지 확인 Optional optionalLike = likeRepository.findByPostAndUser(post, user); @@ -41,7 +44,7 @@ public Boolean like(User user, Long postId) { } public Boolean unlike(User user, Long postId) { - Post post = postService.findById(postId); + Post post = postRepository.findById(postId).orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND, "해당 포스트가 존재하지 않습니다.", "해당 포스트가 존재하지 않습니다.")); //like가 테이블에 있는지 확인 Optional optionalLike = likeRepository.findByPostAndUser(post, user); @@ -59,4 +62,15 @@ public Boolean unlike(User user, Long postId) { //TODO 좋아요가 이미 취소된 상태 -> 그냥 success 보내고 가만히 있으면 되지않나 } + public List isLikedByUser(User user, List postIds) { + List> results = likeRepository.findPostLikesStatus(postIds, user.getId()); + Map likeStatusMap = results.stream() + .collect(Collectors.toMap( + result -> ((Number) result.get("postId")).longValue(), + result -> ((Number) result.get("isLiked")).intValue() == 1 // 변환 로직 추가 + )); + return postIds.stream() + .map(postId -> likeStatusMap.getOrDefault(postId, false)) + .collect(Collectors.toList()); + } } 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 ee51c05..56a70f3 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/post/dto/PostDto.java +++ b/src/main/java/com/swmarastro/mykkumiserver/post/dto/PostDto.java @@ -22,6 +22,8 @@ public class PostDto { private String category; private String subCategory; private Writer writer; + private Long likeCount; + private Boolean likedByCurrentUser; private List content; private List images; @@ -52,4 +54,8 @@ public static PostDto of(PostView postView) { .build(); } + public void updateLikedNByCurrentUser(Boolean likedByCurrentUser) { + this.likedByCurrentUser = likedByCurrentUser; + } + } 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 c948842..dd6bf90 100644 --- a/src/main/java/com/swmarastro/mykkumiserver/post/service/PostService.java +++ b/src/main/java/com/swmarastro/mykkumiserver/post/service/PostService.java @@ -7,6 +7,7 @@ import com.swmarastro.mykkumiserver.global.exception.ErrorCode; import com.swmarastro.mykkumiserver.global.util.Base64Utils; import com.swmarastro.mykkumiserver.hashtag.*; +import com.swmarastro.mykkumiserver.like.LikesService; import com.swmarastro.mykkumiserver.post.PostLatestCursor; import com.swmarastro.mykkumiserver.post.PostMongoRepository; import com.swmarastro.mykkumiserver.post.PostRepository; @@ -45,6 +46,7 @@ public class PostService { private final HashtagService hashtagService; private final PostMongoRepository postMongoRepository; private final ApplicationEventPublisher eventPublisher; + private final LikesService likesService; public PostListResponse getInfiniteScrollPosts(User user, String encodedCursor, Integer limit) { if (limit <= 0 || limit > 10) @@ -65,11 +67,28 @@ public PostListResponse getInfiniteScrollPosts(User user, String encodedCursor, 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) { + if (user == null) { + for (PostDto post : posts) { + post.updateLikedNByCurrentUser(false); + } + return; + } + List postIds = extractPostIds(posts); + List likedByUser = likesService.isLikedByUser(user, postIds); + for (int i = 0; i < posts.size(); i++) { + posts.get(i).updateLikedNByCurrentUser(likedByUser.get(i)); + } + } + public Long registerPost(User user, Long subCategoryId, String content, List images) { if (subCategoryId == null) { @@ -185,4 +204,11 @@ public Post findById(Long postId) { return postRepository.findById(postId) .orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND, "포스트가 존재하지 않습니다.", "포스트가 존재하지 않습니다.")); } + + private List extractPostIds(List posts) { + return posts.stream() + .map(PostDto::getId) + .collect(Collectors.toList()); + } + }