Skip to content

Commit 213b1ec

Browse files
committed
Implement HTTP mapping of funds withdrawals
1 parent 5be8fe8 commit 213b1ec

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

http/src/main/kotlin/lmirabal/bank/http/BankHttp.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fun bankHttp() = bankHttp(BankService(InMemoryBankAccountRepository()))
2323

2424
internal const val BANK_ACCOUNTS_BASE_URL = "/bank/accounts"
2525
internal const val BANK_ACCOUNT_DEPOSIT_PATH = "/{id}/deposit"
26+
internal const val BANK_ACCOUNT_WITHDRAWAL_PATH = "/{id}/withdraw"
2627
internal val bankAccountLens = Body.auto<BankAccount>().toLens()
2728
internal val bankAccountListLens = Body.auto<List<BankAccount>>().toLens()
2829
internal val accountIdLens = Path.map({ BankAccountId(UUID.fromString(it)) }, { it.value.toString() }).of("id")
@@ -40,6 +41,12 @@ internal fun bankHttp(bank: Bank): HttpHandler {
4041
val amount = amountLens(request)
4142

4243
Response(OK).with(bankAccountLens of bank.deposit(accountId, amount))
44+
},
45+
BANK_ACCOUNT_WITHDRAWAL_PATH bind POST to { request ->
46+
val accountId = accountIdLens(request)
47+
val amount = amountLens(request)
48+
49+
Response(OK).with(bankAccountLens of bank.withdraw(accountId, amount))
4350
}
4451
),
4552
)

http/src/main/kotlin/lmirabal/bank/http/BankHttpClient.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ class BankHttpClient(val http: HttpHandler) : Bank {
2222
}
2323

2424
override fun deposit(id: BankAccountId, amount: Amount): BankAccount {
25+
return changeBalanceAction(BANK_ACCOUNT_DEPOSIT_PATH, id, amount)
26+
}
27+
28+
override fun withdraw(id: BankAccountId, amount: Amount): BankAccount {
29+
return changeBalanceAction(BANK_ACCOUNT_WITHDRAWAL_PATH, id, amount)
30+
}
31+
32+
private fun changeBalanceAction(actionPath: String, id: BankAccountId, amount: Amount): BankAccount {
2533
val response = http(
26-
Request(POST, BANK_ACCOUNTS_BASE_URL + BANK_ACCOUNT_DEPOSIT_PATH)
34+
Request(POST, BANK_ACCOUNTS_BASE_URL + actionPath)
2735
.with(accountIdLens of id, amountLens of amount)
2836
)
2937
return bankAccountLens(response)
3038
}
31-
32-
override fun withdraw(id: BankAccountId, amount: Amount): BankAccount {
33-
TODO("Not yet implemented")
34-
}
3539
}

http/src/test/kotlin/lmirabal/bank/http/BankHttpIntegrationTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import org.http4k.server.SunHttp
99
import org.http4k.server.asServer
1010
import org.junit.jupiter.api.AfterEach
1111
import org.junit.jupiter.api.BeforeEach
12+
import org.junit.jupiter.api.Tag
1213

14+
@Tag("ImplementationReady")
1315
class BankHttpIntegrationTest : BankTest() {
1416
private val server = bankHttp().asServer(SunHttp())
1517
override val bank = BankHttpClient(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package lmirabal.bank.http
22

33
import lmirabal.bank.BankTest
4+
import org.junit.jupiter.api.Tag
45

6+
@Tag("ImplementationReady")
57
class BankHttpTest : BankTest() {
68
override val bank = BankHttpClient(bankHttp())
79
}

0 commit comments

Comments
 (0)