diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java index 08b2738..1be9c9f 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java @@ -78,4 +78,14 @@ public List findAllByIds(List postIds) { public void incrementViewCount(Long postId, int delta){ postJpaRepository.incrementViewCount(postId,delta); } + + @Override + public void incrementLikeCount(Long postId){ + postJpaRepository.incrementLikeCount(postId); + } + + @Override + public void decrementLikeCount(Long postId){ + postJpaRepository.decrementLikeCount(postId); + } } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java index a15fd62..ceaad64 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java @@ -42,4 +42,12 @@ Page findAllByHashtagIdAndVisibilityPublic( @Modifying @Query("UPDATE Post p SET p.viewCount = p.viewCount + :delta WHERE p.postId = :postId") void incrementViewCount(@Param("postId") Long postId, @Param("delta") int delta); + + @Modifying + @Query("UPDATE Post p Set p.likeCount = p.likeCount + 1 WHERE p.postId = :postId") + void incrementLikeCount(@Param("postId") Long postId); + + @Modifying + @Query("UPDATE Post p SET p.likeCount = p.likeCount - 1 WHERE p.postId = :postId AND p.likeCount > 0") + void decrementLikeCount(@Param("postId") Long postId); } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java index 77a26f6..cbb7a0f 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java @@ -29,4 +29,7 @@ public interface PostRepository { List findAllByIds(List postIds); void incrementViewCount(Long postId, int delta); + + void incrementLikeCount(Long postId); + void decrementLikeCount(Long postId); } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/service/LikeService.java b/prothsync/src/main/java/com/prothsync/prothsync/service/LikeService.java index 0161ad7..90ec8d4 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/service/LikeService.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/service/LikeService.java @@ -31,20 +31,18 @@ public LikeResponseDTO toggleLike(Long postId, Long userId) { if (existingLike.isPresent()) { postLikeRepository.delete(existingLike.get()); - post.decrementLikeCount(); - postRepository.save(post); - return LikeResponseDTO.of(postId, false, post.getLikeCount()); + postRepository.decrementLikeCount(postId); + return LikeResponseDTO.of(postId, false, post.getLikeCount() - 1); } else { blockService.validateNotBlocked(userId, post.getUserId()); PostLike postLike = PostLike.create(userId, postId); postLikeRepository.save(postLike); - post.incrementLikeCount(); - postRepository.save(post); + postRepository.incrementLikeCount(postId); notificationService.send( - NotificationType.LIKE,userId,post.getUserId(),postId, ReferenceType.POST + NotificationType.LIKE, userId, post.getUserId(), postId, ReferenceType.POST ); - return LikeResponseDTO.of(postId, true, post.getLikeCount()); + return LikeResponseDTO.of(postId, true, post.getLikeCount() + 1); } }