Adds validation and error handling to signup
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
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.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class SignupService(private val hunterRepository: HunterRepository) {
|
||||
fun createNewHunter(hunter: Hunter) {
|
||||
if (!EmailValidator.getInstance().isValid(hunter.email)) {
|
||||
throw InvalidEmailException(hunter.email)
|
||||
}
|
||||
if (hunterRepository.findByEmail(hunter.email) != null) {
|
||||
throw PreexistingAccountException()
|
||||
}
|
||||
hunterRepository.save(hunter.toRecord())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user