diff --git a/src/main/java/org/sopt/app/application/poke/FriendService.java b/src/main/java/org/sopt/app/application/poke/FriendService.java index 2fc3d09a..f26d88cd 100644 --- a/src/main/java/org/sopt/app/application/poke/FriendService.java +++ b/src/main/java/org/sopt/app/application/poke/FriendService.java @@ -3,6 +3,7 @@ import java.util.Collections; import java.util.List; import lombok.RequiredArgsConstructor; +import lombok.val; import org.sopt.app.common.exception.NotFoundException; import org.sopt.app.common.response.ErrorCode; import org.sopt.app.domain.entity.Friend; @@ -101,9 +102,18 @@ public List getMutualFriendIds(Long pokerId, Long pokedId) { } public List getPokeFriendIdRandomly(Long userId) { - List pokeFriendIds = friendRepository.findAllOfFriendIdsByUserId(userId); - Collections.shuffle(pokeFriendIds); - return pokeFriendIds.subList(0, 1); + val friendIdsPokeMe = friendRepository.findAllByFriendUserId(userId).stream() + .map(Friend::getUserId) + .toList(); + val friendIds = friendRepository.findAllByUserIdAndFriendUserIdIn(userId, friendIdsPokeMe).stream().map( + Friend::getFriendUserId + ).toList(); + + if(friendIds.isEmpty()) { + return friendIds; + } + Collections.shuffle(friendIds); + return friendIds.subList(0, 1); } public List findAllFriendIdsByUserIdRandomlyExcludeUserId(Long friendsUserId, List excludedUserId, int limitNum) { diff --git a/src/main/java/org/sopt/app/interfaces/postgres/FriendRepository.java b/src/main/java/org/sopt/app/interfaces/postgres/FriendRepository.java index da63a6e1..304474d7 100644 --- a/src/main/java/org/sopt/app/interfaces/postgres/FriendRepository.java +++ b/src/main/java/org/sopt/app/interfaces/postgres/FriendRepository.java @@ -15,6 +15,9 @@ public interface FriendRepository extends JpaRepository, FriendCus @Query("SELECT f.friendUserId FROM Friend f WHERE f.userId = :userId") List findAllOfFriendIdsByUserId(@Param("userId") Long userId); + + List findAllByFriendUserId(Long friendUserId); + @Query(value = "SELECT f FROM Friend f WHERE f.userId = :userId AND f.pokeCount BETWEEN :lowerLimit AND :upperLimit ORDER BY f.pokeCount") List findAllByUserIdAndPokeCountBetweenOrderByPokeCountDesc(@Param("userId") Long userId, @Param("lowerLimit") Integer lowerLimit, @Param("upperLimit") Integer upperLimit); @Query(value = "SELECT f FROM Friend f WHERE f.userId = :userId AND f.pokeCount BETWEEN :lowerLimit AND :upperLimit ORDER BY f.pokeCount") @@ -24,4 +27,6 @@ public interface FriendRepository extends JpaRepository, FriendCus int findSizeByUserIdAndPokeCountBetween(Long userId, Integer lowerLimit, Integer upperLimit); List findAllByUserId(Long userId); + + List findAllByUserIdAndFriendUserIdIn(Long userId, List friendIdsPokeMe); }