Skip to content

Commit

Permalink
FIX : 친구의 친구와 이미 친구일 때 빈 배열 반환되는 엣지케이스 요구사항 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
yummygyudon committed Dec 27, 2023
1 parent dbfe85e commit cf64910
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class FriendService {
public List<Long> findAllFriendIdsByUserIdRandomly(Long userId, List<Long> excludeUserIds, int limitNum) {
return friendRepository.getFriendRandom(userId, excludeUserIds, limitNum);
}
public List<Long> findAllFriendIdsByUserIdRandomlyIncludeDuplicatedFriend(Long userId, List<Long> excludeUserIds, int limitNum) {
return friendRepository.getFriendRandomIncludeDuplicated(userId, excludeUserIds, limitNum);
}

public List<Friend> findAllFriendsByFriendship(Long userId, Integer lowerLimit, Integer upperLimit) {
return friendRepository.findAllByUserIdAndPokeCountBetweenOrderByPokeCountDesc(
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/sopt/app/facade/PokeFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ private List<Long> pickRandomUserIds(
@Transactional(readOnly = true)
public List<PokeResponse.Friend> getRecommendFriendsOfUsersFriend(User user) {
val hasPokeMeBeforeUserIds = pokeHistoryService.getPokeFriendIds(user.getId());
val friendsUserIds = friendService.findAllFriendIdsByUserIdRandomly(user.getId(), hasPokeMeBeforeUserIds, 2);
List<Long> friendsUserIds = friendService.findAllFriendIdsByUserIdRandomly(user.getId(), hasPokeMeBeforeUserIds, 2);
if (friendsUserIds.isEmpty()) {
friendsUserIds = friendService.findAllFriendIdsByUserIdRandomlyIncludeDuplicatedFriend(
user.getId(), hasPokeMeBeforeUserIds, 2);
}
return friendsUserIds.stream().map(
friendsUserId -> {
val friendUser = userService.getUserProfile(friendsUserId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface FriendCustomRepository {

List<Long> getFriendRandom(Long userId, List<Long> excludeUserIds, int limitNum);

List<Long> getFriendRandomIncludeDuplicated(Long userId, List<Long> excludeUserIds, int limitNum);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,44 @@ public class FriendRepositoryImpl implements FriendCustomRepository {
public List<Long> getFriendRandom(Long userId, List<Long> excludeUserIds, int limitNum) {
QFriend main = new QFriend("main");
QFriend sub = new QFriend("sub");
QFriend f = new QFriend("f");

SubQueryExpression<Long> excludedFriendIds =
queryFactory
.select(f.friendUserId)
.from(f)
.where(f.userId.eq(userId));
SubQueryExpression<Long> queryOfExcludeIds = getQueryOfExcludeIds(userId);
return queryFactory
.select(sub.userId)
.from(main)
.join(sub).on(main.friendUserId.eq(sub.userId))
.where(main.userId.eq(userId)
.and(sub.friendUserId.ne(userId))
.and(sub.friendUserId.notIn(excludedFriendIds)) // Exclude the friend IDs
.and(sub.friendUserId.notIn(queryOfExcludeIds)) // Exclude the friend IDs
.and(sub.friendUserId.notIn(excludeUserIds))) // Exclude friend who user have to reply first IDs
.groupBy(sub.userId)
.having(sub.userId.count().goe(1))
.orderBy(Expressions.numberTemplate(Long.class, "RANDOM()").asc())
.limit(2)
.fetch();
}

public List<Long> getFriendRandomIncludeDuplicated(Long userId, List<Long> excludeUserIds, int limitNum) {
QFriend main = new QFriend("main");
QFriend sub = new QFriend("sub");
return queryFactory
.select(sub.userId)
.from(main)
.join(sub).on(main.friendUserId.eq(sub.userId))
.where(main.userId.eq(userId)
.and(sub.friendUserId.ne(userId))
.and(sub.friendUserId.notIn(excludeUserIds))) // Exclude friend who user have to reply first IDs
.groupBy(sub.userId)
.having(sub.userId.count().goe(1))
.orderBy(Expressions.numberTemplate(Long.class, "RANDOM()").asc())
.limit(2)
.fetch();
}

private SubQueryExpression<Long> getQueryOfExcludeIds(Long userId) {
QFriend f = new QFriend("f");
return queryFactory
.select(f.friendUserId)
.from(f)
.where(f.userId.eq(userId));
}
}

0 comments on commit cf64910

Please sign in to comment.