From 3693cfc56ea781a868187a174acd1f504d9b51a5 Mon Sep 17 00:00:00 2001 From: rafaelpereira Date: Mon, 22 Apr 2024 16:27:48 +0100 Subject: [PATCH] refactor: Remove Validation of negative values of Amount in order to be reusable for negative values (negative balance of the account). Validation of amount rules in useCase. --- .../application/usecase/DepositUsecase.kt | 19 ++++++++++++++----- .../domain/model/account/Amount.kt | 6 ------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/usecase/DepositUsecase.kt b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/usecase/DepositUsecase.kt index c1afa53..3c15239 100644 --- a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/usecase/DepositUsecase.kt +++ b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/application/usecase/DepositUsecase.kt @@ -8,6 +8,7 @@ import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.event.MoneyDepositedEvent import io.holixon.cqrshexagonaldemo.demoparent.transactions.framework.Usecase import mu.KLogging +import java.math.BigDecimal @Usecase class DepositUsecase( @@ -20,15 +21,23 @@ class DepositUsecase( override fun deposit(iban: Iban, amount: Amount) { logger().info { "deposit $amount to account ${iban.value}" } + require(amount.value >= BigDecimal.ZERO) { + logger().error { + "Amount must be positive" + } + } + val account = accountOutPort.findAccount(iban) val updatedAccount = accountOutPort.deposit(account, amount) - eventingOutAdapter.publishEvent(MoneyDepositedEvent( - updatedAccount.iban, - updatedAccount.balance, - System.currentTimeMillis() - )) + eventingOutAdapter.publishEvent( + MoneyDepositedEvent( + updatedAccount.iban, + updatedAccount.balance, + System.currentTimeMillis() + ) + ) logger().info { "updated account $updatedAccount" } } diff --git a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/account/Amount.kt b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/account/Amount.kt index 432ab79..aab20f9 100644 --- a/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/account/Amount.kt +++ b/demo-transactions/src/main/kotlin/io/holixon/cqrshexagonaldemo/demoparent/transactions/domain/model/account/Amount.kt @@ -7,12 +7,6 @@ import java.math.BigDecimal */ @JvmInline value class Amount(val value: BigDecimal) { - init { - require(value >= BigDecimal.ZERO){ - "Amount must be positive" - } - } - operator fun plus(value2: Amount) : Amount { return Amount(this.value.plus(value2.value)) }