From 6165bcd51267f6dd72174816107badcadf824fa6 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Mon, 3 Feb 2025 22:06:53 +0100 Subject: [PATCH] cleanup AccountService --- .../kotlin/ltd/hlaeja/service/AccountService.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ltd/hlaeja/service/AccountService.kt b/src/main/kotlin/ltd/hlaeja/service/AccountService.kt index a18cb53..cd0d326 100644 --- a/src/main/kotlin/ltd/hlaeja/service/AccountService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/AccountService.kt @@ -7,6 +7,9 @@ import ltd.hlaeja.entity.AccountEntity import ltd.hlaeja.repository.AccountRepository import org.springframework.dao.DuplicateKeyException import org.springframework.http.HttpStatus +import org.springframework.http.HttpStatus.BAD_REQUEST +import org.springframework.http.HttpStatus.CONFLICT +import org.springframework.http.HttpStatus.NOT_FOUND import org.springframework.stereotype.Service import org.springframework.web.server.ResponseStatusException import reactor.core.publisher.Flux @@ -23,13 +26,13 @@ class AccountService( uuid: UUID, ): Mono = accountRepository.findById(uuid) .doOnNext { log.debug { "Get account ${it.id}" } } - .switchIfEmpty(Mono.error(ResponseStatusException(HttpStatus.NOT_FOUND))) + .switchIfEmpty(Mono.error(ResponseStatusException(NOT_FOUND))) fun getUserByUsername( username: String, ): Mono = accountRepository.findByUsername(username) .doOnNext { log.debug { "Get account ${it.id} for username $username" } } - .switchIfEmpty(Mono.error(ResponseStatusException(HttpStatus.NOT_FOUND))) + .switchIfEmpty(Mono.error(ResponseStatusException(NOT_FOUND))) fun addAccount( accountEntity: AccountEntity, @@ -43,7 +46,7 @@ class AccountService( .take(size.toLong()) .doOnNext { log.debug { "Retrieved accounts $page with size $size" } } } catch (e: IllegalArgumentException) { - Flux.error(ResponseStatusException(HttpStatus.BAD_REQUEST, null, e)) + Flux.error(ResponseStatusException(BAD_REQUEST, null, e)) } fun updateAccount( @@ -55,8 +58,8 @@ class AccountService( private fun onSaveError(throwable: Throwable): Mono { log.debug { throwable.localizedMessage } return when { - throwable is DuplicateKeyException -> Mono.error(ResponseStatusException(HttpStatus.CONFLICT)) - else -> Mono.error(ResponseStatusException(HttpStatus.BAD_REQUEST)) + throwable is DuplicateKeyException -> Mono.error(ResponseStatusException(CONFLICT)) + else -> Mono.error(ResponseStatusException(BAD_REQUEST)) } } }