diff --git a/src/main/kotlin/com/yourssu/ssudateserver/repository/UserRepository.kt b/src/main/kotlin/com/yourssu/ssudateserver/repository/UserRepository.kt index 7d949c8..50e7bac 100644 --- a/src/main/kotlin/com/yourssu/ssudateserver/repository/UserRepository.kt +++ b/src/main/kotlin/com/yourssu/ssudateserver/repository/UserRepository.kt @@ -29,4 +29,7 @@ interface UserRepository : JpaRepository { fun findByCode(code: String): User? fun findTop15ByOrderByCreatedAtDescIdDesc(): List + + @Query("SELECT * FROM users WHERE users.o_auth_name= :oauthName FOR UPDATE", nativeQuery = true) + fun findByOauthNameWithLock(oauthName: String): User? } diff --git a/src/main/kotlin/com/yourssu/ssudateserver/service/SSUDateService.kt b/src/main/kotlin/com/yourssu/ssudateserver/service/SSUDateService.kt index 991cace..4e56820 100644 --- a/src/main/kotlin/com/yourssu/ssudateserver/service/SSUDateService.kt +++ b/src/main/kotlin/com/yourssu/ssudateserver/service/SSUDateService.kt @@ -20,8 +20,9 @@ class SSUDateService( private val followRepository: FollowRepository, ) { @Transactional - fun recentSearch(): List { - return userRepository.findTop15ByOrderByCreatedAtDescIdDesc() + fun recentSearch(): List = + userRepository + .findTop15ByOrderByCreatedAtDescIdDesc() .map { user -> SearchResponseDto( animals = user.animals, @@ -32,14 +33,14 @@ class SSUDateService( weight = user.weight, ) } - } fun search( gender: Gender, animals: Animals, - ): List { - return if (animals == Animals.ALL) { - userRepository.getRandomUserWithGender(gender.toString()) + ): List = + if (animals == Animals.ALL) { + userRepository + .getRandomUserWithGender(gender.toString()) .map { user -> SearchResponseDto( animals = user.animals, @@ -51,7 +52,8 @@ class SSUDateService( ) } } else { - userRepository.getRandomUserWithGenderAndAnimals(gender.toString(), animals.toString()) + userRepository + .getRandomUserWithGenderAndAnimals(gender.toString(), animals.toString()) .map { user -> SearchResponseDto( animals = user.animals, @@ -63,7 +65,6 @@ class SSUDateService( ) } } - } fun searchContact(oauthName: String): List { val user = @@ -71,7 +72,8 @@ class SSUDateService( val toUserIdList: List = followRepository.findAllByFromUserId(user.id!!).map { it.toUserId } - return userRepository.findAllByIdIn(toUserIdList) + return userRepository + .findAllByIdIn(toUserIdList) .map { SearchContactResponseDto( animals = it.animals, @@ -91,7 +93,7 @@ class SSUDateService( nickName: String, ): ContactResponseDto { val fromUser = - userRepository.findByOauthName(oauthName) ?: throw UserNotFoundException("해당 oauthName인 유저가 없습니다.") + userRepository.findByOauthNameWithLock(oauthName) ?: throw UserNotFoundException("해당 oauthName인 유저가 없습니다.") if (fromUser.nickName == nickName) { throw SelfContactException("본인의 프로필은 조회할 수 없어요.")