update AccountController with balance

This commit is contained in:
2025-09-11 14:26:40 +02:00
parent 942750d9d8
commit 68926b0159
4 changed files with 109 additions and 47 deletions

View File

@@ -9,10 +9,10 @@ insert_final_newline = true
max_line_length = 120 max_line_length = 120
tab_width = 4 tab_width = 4
[*.{json,md,txt,xml,yaml,yml}] [*.{http,json,md,txt,xml,yaml,yml}]
max_line_length = 1024 max_line_length = 1024
[*.{json,xml,yaml,yml}] [*.{http,json,xml,yaml,yml}]
indent_size = 2 indent_size = 2
tab_width = 2 tab_width = 2

View File

@@ -6,3 +6,6 @@ Content-Type: application/json
"name": "account name", "name": "account name",
"amount": -1.11 "amount": -1.11
} }
### Create Account
GET {{url}}/balance/account-00000000-0000-0000-0000-000000000000

View File

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

View File

@@ -8,12 +8,13 @@ import ltd.lulz.model.Account
import ltd.lulz.model.AccountEntity import ltd.lulz.model.AccountEntity
import ltd.lulz.service.AccountService import ltd.lulz.service.AccountService
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.springframework.http.MediaType.APPLICATION_JSON import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.test.web.reactive.server.WebTestClient import org.springframework.test.web.reactive.server.WebTestClient
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@Suppress("MayBeConstant") @Suppress("MayBeConstant", "ReactiveStreamsUnusedPublisher")
class AccountControllerTest { class AccountControllerTest {
companion object { companion object {
@@ -30,6 +31,9 @@ class AccountControllerTest {
webTestClient = WebTestClient.bindToController(AccountController(accountService)).build() webTestClient = WebTestClient.bindToController(AccountController(accountService)).build()
} }
@Nested
inner class CreateAccount {
@Test @Test
fun `create account success`() { fun `create account success`() {
// given // given
@@ -85,4 +89,43 @@ class AccountControllerTest {
// then // then
result.expectStatus().isBadRequest result.expectStatus().isBadRequest
} }
}
@Nested
inner class AccountBalance {
@Test
fun `account balance success`() {
// given
every { accountService.getById(any()) } returns Mono.just(
AccountEntity(id = uuid, name = name, amount = amount),
)
// when
val result = webTestClient.get()
.uri("/balance/account-$uuid")
.exchange()
// then
result.expectStatus().isOk
.expectBody()
.jsonPath("$.id").isEqualTo(uuid.toString())
.jsonPath("$.name").isEqualTo(name)
.jsonPath("$.amount").isEqualTo(amount.toString())
}
@Test
fun `account balance fail`() {
// given
every { accountService.getById(any()) } returns Mono.empty()
// when
val result = webTestClient.get()
.uri("/balance/account-$uuid")
.exchange()
// then
result.expectStatus().isNotFound
}
}
} }