Skip to content

Commit

Permalink
fix: 랜덤 보틀 여러개 받아지는 문제 해결 (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
miseongk authored Nov 15, 2024
1 parent 7ddfb55 commit 3675137
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,13 @@ class BottleService(
val matchingUsers = userRepository.findByIdInAndDeletedFalse(matchingUserDtos.map { it.willMatchUserId })

val now = LocalDateTime.now()
var bottles = matchingUsers.map { matchingUser -> Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = now.plusDays(1)) }
var bottles = matchingUsers.map { matchingUser ->
Bottle(
targetUser = user,
sourceUser = matchingUser,
expiredAt = now.plusDays(1)
)
}
val savedBottles = bottleRepository.saveAll(bottles)

user.updateLastRandomMatchedAt(LocalDateTime.now())
Expand Down Expand Up @@ -225,7 +231,13 @@ class BottleService(
val matchingUsers = userRepository.findByIdInAndDeletedFalse(matchingUserDtos.map { it.willMatchUserId })

val now = LocalDateTime.now()
var bottles = matchingUsers.map { matchingUser -> Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = now.plusDays(1)) }
var bottles = matchingUsers.map { matchingUser ->
Bottle(
targetUser = user,
sourceUser = matchingUser,
expiredAt = now.plusDays(1)
)
}
val savedBottles = bottleRepository.saveAll(bottles)

user.updateLastRandomMatchedAt(LocalDateTime.now())
Expand Down Expand Up @@ -255,14 +267,16 @@ class BottleService(


// 필터링된 사용자가 count에 도달하지 못하면 추가로 사용자 채우기
return if (canBeMatchedDtos.size < count) {
val result = if (canBeMatchedDtos.size < count) {
val additionalDtos = usersCanBeMatchedDtos.shuffled()
.filter { it !in canBeMatchedDtos } // 이미 선택된 항목을 제외
.take(count - canBeMatchedDtos.size) // 모자란 개수만큼 추가
canBeMatchedDtos + additionalDtos // 기존 결과에 추가 결과를 합쳐서 반환
} else {
canBeMatchedDtos
}

return result.take(count) // 결과에서 count만큼만 반환
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -301,14 +315,20 @@ class BottleService(
fun matchFirstRandomBottle(userId: Long, count: Int): List<Bottle> {
val user = userRepository.findByIdOrNull(userId) ?: throw IllegalStateException("회원가입 상태를 문의해주세요")

val usersCanBeMatched = bottleMatchingRepository.findAllUserCanBeMatched(user.id, user.gender!!).take(count)
val usersCanBeMatched = bottleMatchingRepository.findAllUserCanBeMatched(user.id, user.gender!!)
if (usersCanBeMatched.isEmpty()) return emptyList()

val matchingUserDtos = findUserSameRegionOrRandom(usersCanBeMatched, user, count)
val matchingUsers = userRepository.findByIdInAndDeletedFalse(matchingUserDtos.map { it.willMatchUserId })

val now = LocalDateTime.now()
var bottles = matchingUsers.map { matchingUser -> Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = now.plusDays(1)) }
var bottles = matchingUsers.map { matchingUser ->
Bottle(
targetUser = user,
sourceUser = matchingUser,
expiredAt = now.plusDays(1)
)
}
val savedBottles = bottleRepository.saveAll(bottles)

user.updateLastRandomMatchedAt(now)
Expand Down

0 comments on commit 3675137

Please sign in to comment.