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}") } }