Files
basic-banking/src/main/kotlin/ltd/lulz/controller/AccountController.kt
Swordsteel e46bf55232 add account not found exception
- update balance in AccountController with onErrorResume
- update AccountService
  - update getForUpdateById with switchIfEmpty
  - update getById with switchIfEmpty
- add AccountNotFoundException
2025-09-14 18:12:11 +02:00

45 lines
1.6 KiB
Kotlin

package ltd.lulz.controller
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.validation.Valid
import java.util.UUID
import ltd.lulz.model.Account
import ltd.lulz.service.AccountService
import ltd.lulz.util.toEntity
import ltd.lulz.util.toResponse
import org.springframework.http.HttpStatus.CREATED
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
import reactor.core.publisher.Mono
private val log = KotlinLogging.logger {}
@RestController
@Validated
class AccountController(
private val accountService: AccountService,
) {
@PostMapping("/account")
@ResponseStatus(CREATED)
fun create(
@Valid @RequestBody request: Account.Request,
): Mono<Account.Response> = accountService.create(request.toEntity())
.map { it.toResponse() }
@GetMapping("/balance/account-{account}")
fun balance(
@PathVariable account: UUID,
): Mono<Account.Response> = accountService.getById(account)
.map { it.toResponse() }
.onErrorResume { Mono.error(ResponseStatusException(NOT_FOUND)) }
.doOnError { log.debug { "account $account not found for balance" } }
}