Adds validation and error handling to signup

This commit is contained in:
2025-12-05 00:15:20 -06:00
parent 8b808bfd34
commit 302feeab1e
6 changed files with 30 additions and 3 deletions

View File

@@ -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())
}
}