cleanup AccountService
This commit is contained in:
@@ -7,6 +7,9 @@ import ltd.hlaeja.entity.AccountEntity
|
|||||||
import ltd.hlaeja.repository.AccountRepository
|
import ltd.hlaeja.repository.AccountRepository
|
||||||
import org.springframework.dao.DuplicateKeyException
|
import org.springframework.dao.DuplicateKeyException
|
||||||
import org.springframework.http.HttpStatus
|
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.stereotype.Service
|
||||||
import org.springframework.web.server.ResponseStatusException
|
import org.springframework.web.server.ResponseStatusException
|
||||||
import reactor.core.publisher.Flux
|
import reactor.core.publisher.Flux
|
||||||
@@ -23,13 +26,13 @@ class AccountService(
|
|||||||
uuid: UUID,
|
uuid: UUID,
|
||||||
): Mono<AccountEntity> = accountRepository.findById(uuid)
|
): Mono<AccountEntity> = accountRepository.findById(uuid)
|
||||||
.doOnNext { log.debug { "Get account ${it.id}" } }
|
.doOnNext { log.debug { "Get account ${it.id}" } }
|
||||||
.switchIfEmpty(Mono.error(ResponseStatusException(HttpStatus.NOT_FOUND)))
|
.switchIfEmpty(Mono.error(ResponseStatusException(NOT_FOUND)))
|
||||||
|
|
||||||
fun getUserByUsername(
|
fun getUserByUsername(
|
||||||
username: String,
|
username: String,
|
||||||
): Mono<AccountEntity> = accountRepository.findByUsername(username)
|
): Mono<AccountEntity> = accountRepository.findByUsername(username)
|
||||||
.doOnNext { log.debug { "Get account ${it.id} for username $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(
|
fun addAccount(
|
||||||
accountEntity: AccountEntity,
|
accountEntity: AccountEntity,
|
||||||
@@ -43,7 +46,7 @@ class AccountService(
|
|||||||
.take(size.toLong())
|
.take(size.toLong())
|
||||||
.doOnNext { log.debug { "Retrieved accounts $page with size $size" } }
|
.doOnNext { log.debug { "Retrieved accounts $page with size $size" } }
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
Flux.error(ResponseStatusException(HttpStatus.BAD_REQUEST, null, e))
|
Flux.error(ResponseStatusException(BAD_REQUEST, null, e))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateAccount(
|
fun updateAccount(
|
||||||
@@ -55,8 +58,8 @@ class AccountService(
|
|||||||
private fun onSaveError(throwable: Throwable): Mono<out AccountEntity> {
|
private fun onSaveError(throwable: Throwable): Mono<out AccountEntity> {
|
||||||
log.debug { throwable.localizedMessage }
|
log.debug { throwable.localizedMessage }
|
||||||
return when {
|
return when {
|
||||||
throwable is DuplicateKeyException -> Mono.error(ResponseStatusException(HttpStatus.CONFLICT))
|
throwable is DuplicateKeyException -> Mono.error(ResponseStatusException(CONFLICT))
|
||||||
else -> Mono.error(ResponseStatusException(HttpStatus.BAD_REQUEST))
|
else -> Mono.error(ResponseStatusException(BAD_REQUEST))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user