Files
scavengerhunt-api/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/SignupService.kt
aarbit 04b61485ea Adds login ability, error handling, and logging (#1)
Reviewed-on: #1
Co-authored-by: aarbit <aarbit@gmail.com>
Co-committed-by: aarbit <aarbit@gmail.com>
2025-12-19 05:15:18 +00:00

31 lines
1.3 KiB
Kotlin

package net.halfbinary.scavengerhuntapi.service
import net.halfbinary.scavengerhuntapi.error.exception.InvalidEmailException
import net.halfbinary.scavengerhuntapi.error.exception.PreexistingAccountException
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
import org.apache.commons.validator.routines.EmailValidator
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class SignupService(private val hunterRepository: HunterRepository) {
companion object {
private val log = LoggerFactory.getLogger(SignupService::class.java)
}
fun createNewHunter(hunter: Hunter) {
log.info("Creating new Hunter with email: ${hunter.email}...")
if (!EmailValidator.getInstance().isValid(hunter.email)) {
log.error("Invalid email ${hunter.email}")
throw InvalidEmailException(hunter.email)
}
if (hunterRepository.findByEmail(hunter.email) != null) {
log.error("Hunter ${hunter.email} already exists")
throw PreexistingAccountException()
}
hunterRepository.save(hunter.toRecord())
log.info("...Created new Hunter with email: ${hunter.email}")
}
}