From eb147dc80dc208341a848f3e47d019db939e9ade Mon Sep 17 00:00:00 2001 From: rafaelpereira Date: Fri, 22 Mar 2024 13:49:52 +0000 Subject: [PATCH] Add put endpoint for depositing money. Refactor inAdapter to be in single file Refactor Account out adapter. Add DepositAccount in port and moneyDeposited event. --- ...stInAdapter.kt => AccountRestInAdapter.kt} | 15 ++++++++-- .../outbound/account/jpa/AccountOutAdapter.kt | 20 ++++++------- .../inbound/account/DepositAccountInPort.kt | 7 +++++ .../domain/model/event/MoneyDepositedEvent.kt | 6 ++++ .../src/main/resources/api/api.yaml | 28 +++++++++++++++++++ 5 files changed, 63 insertions(+), 13 deletions(-) rename demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/inbound/account/{CreateAccountRestInAdapter.kt => AccountRestInAdapter.kt} (66%) create mode 100644 demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/port/inbound/account/DepositAccountInPort.kt create mode 100644 demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/event/MoneyDepositedEvent.kt diff --git a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/inbound/account/CreateAccountRestInAdapter.kt b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/inbound/account/AccountRestInAdapter.kt similarity index 66% rename from demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/inbound/account/CreateAccountRestInAdapter.kt rename to demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/inbound/account/AccountRestInAdapter.kt index a984e70..7be08ee 100644 --- a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/inbound/account/CreateAccountRestInAdapter.kt +++ b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/inbound/account/AccountRestInAdapter.kt @@ -4,17 +4,21 @@ import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.Acco import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.account.mapper.toDto import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.dto.AccountDto import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.dto.CreateAccountRequestDto +import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.inbound.dto.DepositRequestDto import io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.inbound.account.CreateAccountInPort +import io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.inbound.account.DepositAccountInPort import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.customer.CustomerNumber import io.holixon.cqrshexagonaldemo.demoparent.transactions.framework.InAdapter import mu.KLogging import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.RestController +import java.math.BigDecimal @RestController @InAdapter -class CreateAccountRestInAdapter( - private val createAccountInPort: CreateAccountInPort +class AccountRestInAdapter( + private val createAccountInPort: CreateAccountInPort, + private val depositAccountInPort: DepositAccountInPort ) : AccountApiDelegate { companion object : KLogging() @@ -23,4 +27,9 @@ class CreateAccountRestInAdapter( val createdAccount = createAccountInPort.createAccount(CustomerNumber(createAccountRequestDto.customerNumber)) return ResponseEntity.ok(createdAccount.toDto()) } -} \ No newline at end of file + + override fun deposit(depositRequestDto: DepositRequestDto): ResponseEntity { + depositAccountInPort.deposit(depositRequestDto.accountNumber, BigDecimal.valueOf(depositRequestDto.amount)) + return ResponseEntity.noContent().build() + } +} diff --git a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/outbound/account/jpa/AccountOutAdapter.kt b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/outbound/account/jpa/AccountOutAdapter.kt index bcc6ca9..d559735 100644 --- a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/outbound/account/jpa/AccountOutAdapter.kt +++ b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/adapter/outbound/account/jpa/AccountOutAdapter.kt @@ -2,10 +2,12 @@ package io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.outbound.ac import io.holixon.cqrshexagonaldemo.demoparent.transactions.adapter.outbound.account.jpa.mapper.AccountEntityMapper import io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.outbound.account.AccountOutPort +import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.exception.AccountNotFoundException import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Account import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Iban import io.holixon.cqrshexagonaldemo.demoparent.transactions.framework.OutAdapter import jakarta.transaction.Transactional +import mu.KLogging import java.math.BigDecimal @OutAdapter @@ -14,11 +16,14 @@ open class AccountOutAdapter( private val accountEntityMapper: AccountEntityMapper ) : AccountOutPort { + companion object : KLogging() + override fun findAccount(iban: Iban): Account? { - val entity = jpaAccountOutAdapter.findById(iban.value) - if (!entity.isPresent) return null + val entity = jpaAccountOutAdapter.findById(iban.value).orElseThrow { + AccountNotFoundException("Account not found for Iban: ${iban.value}") + } - return accountEntityMapper.toDomain(entity.get()) + return accountEntityMapper.toDomain(entity) } override fun createAccount(account: Account): Account { @@ -29,16 +34,11 @@ open class AccountOutAdapter( return accountEntityMapper.toDomain(savedAccount) } - //Refactor code similar to withdraw... @Transactional override fun deposit(iban: Iban, amount: BigDecimal): Account { - require(amount > BigDecimal.ZERO) { - "Amount to deposit must be above zero" - } - val entity = jpaAccountOutAdapter.findById(iban.value) .orElseThrow { - IllegalArgumentException("Account not found for Iban: ${iban.value}") + AccountNotFoundException("Account not found for Iban: ${iban.value}") } val account = accountEntityMapper.toDomain(entity) @@ -56,7 +56,7 @@ open class AccountOutAdapter( val entity = jpaAccountOutAdapter.findById(iban.value) .orElseThrow { - IllegalArgumentException("Account not found for Iban: ${iban.value}") + AccountNotFoundException("Account not found for Iban: ${iban.value}") } val account = accountEntityMapper.toDomain(entity) diff --git a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/port/inbound/account/DepositAccountInPort.kt b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/port/inbound/account/DepositAccountInPort.kt new file mode 100644 index 0000000..4cf7dd5 --- /dev/null +++ b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/port/inbound/account/DepositAccountInPort.kt @@ -0,0 +1,7 @@ +package io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.inbound.account + +import java.math.BigDecimal + +interface DepositAccountInPort { + fun deposit(accountNumber: String, amount: BigDecimal) +} \ No newline at end of file diff --git a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/event/MoneyDepositedEvent.kt b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/event/MoneyDepositedEvent.kt new file mode 100644 index 0000000..c141f41 --- /dev/null +++ b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/event/MoneyDepositedEvent.kt @@ -0,0 +1,6 @@ +package io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.event + +import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Iban +import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Money + +data class MoneyDepositedEvent(val iban: Iban, val balance: Money, val timeStamp: Long) : Event diff --git a/demo-transactions/src/main/resources/api/api.yaml b/demo-transactions/src/main/resources/api/api.yaml index b029ea2..2c485b2 100644 --- a/demo-transactions/src/main/resources/api/api.yaml +++ b/demo-transactions/src/main/resources/api/api.yaml @@ -72,6 +72,34 @@ paths: application/json: schema: $ref: '#/components/schemas/Account' + /api/v1/account/deposit: + put: + tags: + - account + summary: > + Deposit money + description: > + Deposit money for a specified account + operationId: deposit + requestBody: + required: true + content: + application/json: + schema: + properties: + accountNumber: + type: string + amount: + type: number + required: + - accountNumber + - amount + responses: + '204': + description: Money Deposited + '404': + description: No Account found + components: schemas: Account: