generated from pricelees/issue-pr-template
[#34] 회원 / 인증 도메인 재정의 #43
52
src/main/kotlin/roomescape/member/business/UserService.kt
Normal file
52
src/main/kotlin/roomescape/member/business/UserService.kt
Normal file
@ -0,0 +1,52 @@
|
||||
package roomescape.member.business
|
||||
|
||||
import io.github.oshai.kotlinlogging.KLogger
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import roomescape.common.dto.CurrentUserContext
|
||||
import roomescape.common.dto.PrincipalType
|
||||
import roomescape.common.dto.UserLoginCredentials
|
||||
import roomescape.member.exception.UserErrorCode
|
||||
import roomescape.member.exception.UserException
|
||||
import roomescape.member.infrastructure.persistence.UserEntity
|
||||
import roomescape.member.infrastructure.persistence.UserRepository
|
||||
|
||||
private val log: KLogger = KotlinLogging.logger {}
|
||||
|
||||
@Service
|
||||
class UserService(
|
||||
private val userRepository: UserRepository,
|
||||
) {
|
||||
@Transactional(readOnly = true)
|
||||
fun findContextById(id: Long): CurrentUserContext {
|
||||
log.info { "[UserService.findContextById] 현재 로그인된 회원 조회 시작: id=${id}" }
|
||||
val user: UserEntity = findOrThrow(id)
|
||||
|
||||
return CurrentUserContext(user.id, user.name, PrincipalType.USER)
|
||||
.also {
|
||||
log.info { "[UserService.findContextById] 현재 로그인된 회원 조회 완료: id=${id}" }
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
fun findCredentialsByAccount(email: String): UserLoginCredentials {
|
||||
log.info { "[UserService.findCredentialsByAccount] 회원 조회 시작: email=${email}" }
|
||||
|
||||
return userRepository.findByEmail(email)
|
||||
?.let {
|
||||
log.info { "[UserService.findCredentialsByAccount] 회원 조회 완료: id=${it.id}" }
|
||||
UserLoginCredentials(it.id, it.password)
|
||||
}
|
||||
?: run {
|
||||
log.info { "[UserService.findCredentialsByAccount] 회원 조회 실패" }
|
||||
throw UserException(UserErrorCode.USER_NOT_FOUND)
|
||||
}
|
||||
}
|
||||
|
||||
private fun findOrThrow(id: Long): UserEntity {
|
||||
return userRepository.findByIdOrNull(id)
|
||||
?: throw UserException(UserErrorCode.USER_NOT_FOUND)
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,9 @@ package roomescape.member.infrastructure.persistence
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
|
||||
interface UserRepository : JpaRepository<UserEntity, Long>
|
||||
interface UserRepository : JpaRepository<UserEntity, Long> {
|
||||
|
||||
fun findByEmail(email: String): UserEntity?
|
||||
}
|
||||
|
||||
interface UserStatusHistoryRepository : JpaRepository<UserStatusHistoryEntity, Long>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user