From d8e61ffefa83a9f76354246c9e5f4d022971c6d8 Mon Sep 17 00:00:00 2001 From: gunom Date: Wed, 10 Jan 2024 21:00:09 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B9=9C=EA=B5=AC=20=EC=97=86=EC=9D=84?= =?UTF-8?q?=20=EB=95=8C=20=EC=97=90=EB=9F=AC=20=EB=8C=80=EC=9D=91=20?= =?UTF-8?q?=EB=B0=8F=20=EC=B9=9C=EA=B5=AC=EC=9D=98=20=EC=B9=9C=EA=B5=AC=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/application/poke/FriendService.java | 18 ++++++---------- .../postgres/FriendRepositoryImpl.java | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 13 deletions(-) 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 c85058ef..6046e2e2 100644 --- a/src/main/java/org/sopt/app/application/poke/FriendService.java +++ b/src/main/java/org/sopt/app/application/poke/FriendService.java @@ -153,21 +153,15 @@ public List getPokeFriendIdRandomly(Long userId) { .map(Friend::getUserId) .toList(); - if (friendIdsPokeMe.isEmpty()) { - return friendIdsPokeMe; - } - - val friendIds = friendRepository.findAllByUserIdAndFriendUserIdIn(userId, friendIdsPokeMe).stream().map( - Friend::getFriendUserId - ).collect(Collectors.toList()); + val friends = friendRepository.findAllByUserIdAndFriendUserIdIn(userId, friendIdsPokeMe); - if(friendIds.isEmpty()) { - return friendIds; + if (friendIdsPokeMe.isEmpty() || friends.isEmpty()) { + throw new NotFoundException(ErrorCode.FRIENDSHIP_NOT_FOUND.getMessage()); } - List modifiableFriendIds = new ArrayList<>(friendIds); - Collections.shuffle(modifiableFriendIds); - return modifiableFriendIds.subList(0, 1); + Collections.shuffle(friends); + + return friends.subList(0, 1).stream().map(Friend::getFriendUserId).toList(); } public List findAllFriendIdsByUserIdRandomlyExcludeUserId(Long friendsUserId, List excludedUserId, int limitNum) { diff --git a/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java b/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java index b0a2c1a6..1e67da53 100644 --- a/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java +++ b/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java @@ -5,6 +5,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.List; import lombok.RequiredArgsConstructor; +import lombok.val; import org.sopt.app.domain.entity.QFriend; @RequiredArgsConstructor @@ -14,11 +15,20 @@ public class FriendRepositoryImpl implements FriendCustomRepository { public List getFriendRandom(Long userId, int limitNum) { QFriend main = new QFriend("main"); + QFriend subQuery = new QFriend("subQuery"); + + SubQueryExpression subQueryExpression = + queryFactory + .select(subQuery.userId) + .from(subQuery) + .where(subQuery.friendUserId.eq(userId)); return queryFactory .select(main.friendUserId) .from(main) - .where(main.userId.eq(userId)) + .where(main.userId.eq(userId) + .and(main.friendUserId.in(subQueryExpression)) + ) .orderBy(Expressions.numberTemplate(Long.class, "RANDOM()").asc()) .limit(limitNum) .fetch(); @@ -28,10 +38,19 @@ public List getFriendRandom(Long userId, int limitNum) { public List getFriendRandom(Long userId, List excludeUserIds, int limitNum) { QFriend main = new QFriend("main"); + QFriend subQuery = new QFriend("subQuery"); + + SubQueryExpression subQueryExpression = + queryFactory + .select(subQuery.userId) + .from(subQuery) + .where(subQuery.friendUserId.eq(userId)); + return queryFactory .select(main.friendUserId) .from(main) .where(main.userId.eq(userId) + .and(main.friendUserId.in(subQueryExpression)) .and(main.friendUserId.notIn(excludeUserIds)) ) .orderBy(Expressions.numberTemplate(Long.class, "RANDOM()").asc())