diff --git a/build.gradle.kts b/build.gradle.kts index 80d7ccd..d7fcc6d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,12 +28,14 @@ repositories { dependencies { val mysqlConnectorJ = "9.5.0" + val commonsValidator = "1.10.1" implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("com.mysql:mysql-connector-j:${mysqlConnectorJ}") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("commons-validator:commons-validator:${commonsValidator}") developmentOnly("org.springframework.boot:spring-boot-devtools") annotationProcessor("org.springframework.boot:spring-boot-configuration-processor") testImplementation("org.springframework.boot:spring-boot-starter-actuator-test") diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/SignupController.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/SignupController.kt index 7929f49..496d62e 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/SignupController.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/SignupController.kt @@ -3,6 +3,7 @@ package net.halfbinary.scavengerhuntapi.controller import net.halfbinary.scavengerhuntapi.model.converter.toDomain import net.halfbinary.scavengerhuntapi.model.request.HunterSignupRequest import net.halfbinary.scavengerhuntapi.service.SignupService +import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController @@ -10,7 +11,13 @@ import org.springframework.web.bind.annotation.RestController @RestController class SignupController(private val signupService: SignupService) { @PostMapping("/signup") - fun hunterSignup(@RequestBody body: HunterSignupRequest) { - signupService.createNewHunter(body.toDomain()) + fun hunterSignup(@RequestBody body: HunterSignupRequest): ResponseEntity { + try { + signupService.createNewHunter(body.toDomain()) + return ResponseEntity.ok().build() + } catch (e: RuntimeException) { + return ResponseEntity.badRequest().body(e.message) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/error/exception/InvalidEmailException.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/error/exception/InvalidEmailException.kt new file mode 100644 index 0000000..51206cc --- /dev/null +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/error/exception/InvalidEmailException.kt @@ -0,0 +1,3 @@ +package net.halfbinary.scavengerhuntapi.error.exception + +class InvalidEmailException(email: String): RuntimeException("The email ${email} is not valid.") \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/error/exception/PreexistingAccountException.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/error/exception/PreexistingAccountException.kt new file mode 100644 index 0000000..7b8526a --- /dev/null +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/error/exception/PreexistingAccountException.kt @@ -0,0 +1,3 @@ +package net.halfbinary.scavengerhuntapi.error.exception + +class PreexistingAccountException: RuntimeException("An account with that email already exists.") \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/HunterRepository.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/HunterRepository.kt index 56b8ac5..254cf4b 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/HunterRepository.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/HunterRepository.kt @@ -6,4 +6,7 @@ import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository @Repository -interface HunterRepository : JpaRepository \ No newline at end of file +interface HunterRepository : JpaRepository +interface HunterRepository : JpaRepository { + fun findByEmail(email: String): HunterRecord? +} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/SignupService.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/SignupService.kt index 42e608b..068f431 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/SignupService.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/SignupService.kt @@ -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()) } } \ No newline at end of file