Skip to content

Commit

Permalink
refactor: Remove Validation of negative values of Amount in order to …
Browse files Browse the repository at this point in the history
…be reusable for negative values (negative balance of the account).

Validation of amount rules in useCase.
  • Loading branch information
rafaelcp94 committed Apr 22, 2024
1 parent bf3fa84 commit 3693cfc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit 3693cfc

Please sign in to comment.