44 lines
1.6 KiB
Kotlin

package roomescape.member.business
import org.springframework.data.repository.findByIdOrNull
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import roomescape.member.infrastructure.persistence.Member
import roomescape.member.infrastructure.persistence.MemberRepository
import roomescape.member.web.MembersResponse
import roomescape.member.web.toResponse
import roomescape.common.exception.ErrorType
import roomescape.common.exception.RoomEscapeException
@Service
@Transactional(readOnly = true)
class MemberService(
private val memberRepository: MemberRepository
) {
fun readAllMembers(): MembersResponse = MembersResponse(
memberRepository.findAll()
.map { it.toResponse() }
.toList()
)
fun findById(memberId: Long): Member = memberRepository.findByIdOrNull(memberId)
?: throw RoomEscapeException(
ErrorType.MEMBER_NOT_FOUND,
String.format("[memberId: %d]", memberId),
HttpStatus.BAD_REQUEST
)
fun findMemberByEmailAndPassword(email: String, password: String): Member =
memberRepository.findByEmailAndPassword(email, password)
?: throw RoomEscapeException(
ErrorType.MEMBER_NOT_FOUND,
String.format("[email: %s, password: %s]", email, password),
HttpStatus.BAD_REQUEST
)
fun existsById(memberId: Long): Boolean = memberRepository.existsById(memberId)
}