Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,24 @@ public Page<User> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,4 @@ public interface FollowJpaRepository extends JpaRepository<Follow, Long> {
@Query("SELECT f.followingId FROM Follow f WHERE f.followerId = :followerId")
List<Long> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -108,4 +109,20 @@ List<User> findNearbyUsersByType(
Page<User> findByNickNameContainingIgnoreCase(String keyword, Pageable pageable);

Page<User> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ List<User> findNearbyUsersByType(double lat, double lng, double radiusKm,
Page<User> findAll(Pageable pageable);

long count();

void incrementFollowerCount(Long userId);
void decrementFollowerCount(Long userId);
void incrementFollowingCount(Long userId);
void decrementFollowingCount(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,8 @@ private void removeFollowIfExists(Long followerId, Long followingId) {
Optional<Follow> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Loading