Adds validation and error handling to signup
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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) {
|
||||
fun hunterSignup(@RequestBody body: HunterSignupRequest): ResponseEntity<Any> {
|
||||
try {
|
||||
signupService.createNewHunter(body.toDomain())
|
||||
return ResponseEntity.ok().build()
|
||||
} catch (e: RuntimeException) {
|
||||
return ResponseEntity.badRequest().body(e.message)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package net.halfbinary.scavengerhuntapi.error.exception
|
||||
|
||||
class InvalidEmailException(email: String): RuntimeException("The email ${email} is not valid.")
|
||||
@@ -0,0 +1,3 @@
|
||||
package net.halfbinary.scavengerhuntapi.error.exception
|
||||
|
||||
class PreexistingAccountException: RuntimeException("An account with that email already exists.")
|
||||
@@ -7,3 +7,6 @@ import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface HunterRepository : JpaRepository<HunterRecord, HunterId>
|
||||
interface HunterRepository : JpaRepository<HunterRecord, HunterId> {
|
||||
fun findByEmail(email: String): HunterRecord?
|
||||
}
|
||||
@@ -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