Skip to content

Commit

Permalink
fix: 친구 없을 때 에러 대응 및 친구의 친구 로직 수정 (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunom authored Jan 10, 2024
2 parents e5e7ab1 + d8e61ff commit 37dc6e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
18 changes: 6 additions & 12 deletions src/main/java/org/sopt/app/application/poke/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,15 @@ public List<Long> 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<Long> 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<Long> findAllFriendIdsByUserIdRandomlyExcludeUserId(Long friendsUserId, List<Long> excludedUserId, int limitNum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,11 +15,20 @@ public class FriendRepositoryImpl implements FriendCustomRepository {

public List<Long> getFriendRandom(Long userId, int limitNum) {
QFriend main = new QFriend("main");
QFriend subQuery = new QFriend("subQuery");

SubQueryExpression<Long> 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();
Expand All @@ -28,10 +38,19 @@ public List<Long> getFriendRandom(Long userId, int limitNum) {
public List<Long> getFriendRandom(Long userId, List<Long> excludeUserIds, int limitNum) {
QFriend main = new QFriend("main");

QFriend subQuery = new QFriend("subQuery");

SubQueryExpression<Long> 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())
Expand Down

0 comments on commit 37dc6e5

Please sign in to comment.