From e694a275f1ca97d9dfe8c72f92d8a4664a1c3749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=A3=BC=EC=B0=AC?= Date: Sun, 29 Mar 2026 23:47:55 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=ED=8C=94=EB=A1=9C=EC=9A=B0=20=EC=88=98?= =?UTF-8?q?=20=EC=9E=91=EC=97=85=20=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/impl/UserRepositoryImpl.java | 20 +++++++++++++++++++ .../repository/jpa/FollowJpaRepository.java | 15 -------------- .../repository/jpa/UserJpaRepository.java | 17 ++++++++++++++++ .../repository/repository/UserRepository.java | 5 +++++ .../prothsync/service/BlockService.java | 9 ++------- .../prothsync/service/FollowService.java | 16 ++++++--------- 6 files changed, 50 insertions(+), 32 deletions(-) diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java index b0c1304..84c68ab 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java @@ -89,4 +89,24 @@ public Page findAll(Pageable pageable) { public long count() { return userJpaRepository.count(); } + + @Override + public void incrementFollowerCount(Long userId) { + userJpaRepository.incrementFollowerCount(userId); + } + + @Override + public void decrementFollowerCount(Long userId) { + userJpaRepository.decrementFollowerCount(userId); + } + + @Override + public void incrementFollowingCount(Long userId) { + userJpaRepository.incrementFollowingCount(userId); + } + + @Override + public void decrementFollowingCount(Long userId) { + userJpaRepository.decrementFollowingCount(userId); + } } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/FollowJpaRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/FollowJpaRepository.java index 6d5444f..e3846ba 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/FollowJpaRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/FollowJpaRepository.java @@ -27,19 +27,4 @@ public interface FollowJpaRepository extends JpaRepository { @Query("SELECT f.followingId FROM Follow f WHERE f.followerId = :followerId") List findFollowingIdsByFollowerId(@Param("followerId") Long followerId); - @Modifying - @Query("UPDATE User u SET u.followerCount = u.followerCount + 1 WHERE u.userId = :userId") - void incrementFollowerCount(@Param("userId") Long userId); - - @Modifying - @Query("UPDATE User u SET u.followerCount = u.followerCount - 1 WHERE u.userId = :userId AND u.followerCount > 0") - void decrementFollowerCount(@Param("userId") Long userId); - - @Modifying - @Query("UPDATE User u SET u.followingCount = u.followingCount + 1 WHERE u.userId = :userId") - void incrementFollowingCount(@Param("userId") Long userId); - - @Modifying - @Query("UPDATE User u SET u.followingCount = u.followingCount - 1 WHERE u.userId = :userId AND u.followingCount > 0") - void decrementFollowingCount(@Param("userId") Long userId); } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java index 33cacc6..ab93a2b 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java @@ -6,6 +6,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -108,4 +109,20 @@ List findNearbyUsersByType( Page findByNickNameContainingIgnoreCase(String keyword, Pageable pageable); Page findAll(Pageable pageable); + + @Modifying + @Query("UPDATE User u SET u.followerCount = u.followerCount + 1 WHERE u.userId = :userId") + void incrementFollowerCount(@Param("userId") Long userId); + + @Modifying + @Query("UPDATE User u SET u.followerCount = u.followerCount - 1 WHERE u.userId = :userId AND u.followerCount > 0") + void decrementFollowerCount(@Param("userId") Long userId); + + @Modifying + @Query("UPDATE User u SET u.followingCount = u.followingCount + 1 WHERE u.userId = :userId") + void incrementFollowingCount(@Param("userId") Long userId); + + @Modifying + @Query("UPDATE User u SET u.followingCount = u.followingCount - 1 WHERE u.userId = :userId AND u.followingCount > 0") + void decrementFollowingCount(@Param("userId") Long userId); } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java index 112ca39..d33b650 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java @@ -29,4 +29,9 @@ List findNearbyUsersByType(double lat, double lng, double radiusKm, Page findAll(Pageable pageable); long count(); + + void incrementFollowerCount(Long userId); + void decrementFollowerCount(Long userId); + void incrementFollowingCount(Long userId); + void decrementFollowingCount(Long userId); } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/service/BlockService.java b/prothsync/src/main/java/com/prothsync/prothsync/service/BlockService.java index 8a4a21b..cfb5e65 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/service/BlockService.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/service/BlockService.java @@ -111,13 +111,8 @@ private void removeFollowIfExists(Long followerId, Long followingId) { Optional follow = followRepository.findByFollowerIdAndFollowingId(followerId, followingId); if (follow.isPresent()) { followRepository.delete(follow.get()); - - User follower = findUserOrThrow(followerId); - User following = findUserOrThrow(followingId); - follower.decrementFollowingCount(); - following.decrementFollowerCount(); - userRepository.save(follower); - userRepository.save(following); + userRepository.decrementFollowingCount(followerId); + userRepository.decrementFollowerCount(followingId); } } diff --git a/prothsync/src/main/java/com/prothsync/prothsync/service/FollowService.java b/prothsync/src/main/java/com/prothsync/prothsync/service/FollowService.java index 13f6f68..f0e63e3 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/service/FollowService.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/service/FollowService.java @@ -41,25 +41,21 @@ public FollowResponseDTO toggleFollow(Long followerId, Long followingId) { if (existingFollow.isPresent()) { followRepository.delete(existingFollow.get()); - follower.decrementFollowingCount(); - following.decrementFollowerCount(); - userRepository.save(follower); - userRepository.save(following); - return FollowResponseDTO.of(followingId, false, following.getFollowerCount()); + userRepository.decrementFollowingCount(followerId); + userRepository.decrementFollowerCount(followingId); + return FollowResponseDTO.of(followingId, false, following.getFollowerCount() - 1); } else { blockService.validateNotBlocked(followerId, followingId); Follow follow = Follow.create(followerId, followingId); followRepository.save(follow); - follower.incrementFollowingCount(); - following.incrementFollowerCount(); - userRepository.save(follower); - userRepository.save(following); + userRepository.incrementFollowingCount(followerId); + userRepository.incrementFollowerCount(followingId); notificationService.send( NotificationType.FOLLOW, followerId, followingId, followerId, ReferenceType.USER); - return FollowResponseDTO.of(followingId, true, following.getFollowerCount()); + return FollowResponseDTO.of(followingId, true, following.getFollowerCount() + 1); } }