Skip to content

Commit

Permalink
Refactor: Add amount Type
Browse files Browse the repository at this point in the history
Refactor: Remove deposit and withdraw implementation to be fixed on next branch.
  • Loading branch information
rafaelcp94 committed Mar 21, 2024
1 parent 7ad3a28 commit 5062fcc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.out
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 java.math.BigDecimal

@OutAdapter
open class AccountOutAdapter(
class AccountOutAdapter(
private val jpaAccountOutAdapter: JpaAccountOutAdapter,
private val accountEntityMapper: AccountEntityMapper
) : AccountOutPort {
Expand All @@ -29,39 +27,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}")
}

val account = accountEntityMapper.toDomain(entity)
account.deposit(amount)
jpaAccountOutAdapter.save(accountEntityMapper.toEntity(account))

return account
override fun deposit(account: Account): Account {
TODO("Not implemented yet")
}

@Transactional
override fun withdraw(iban: Iban, amount: BigDecimal): Account {
require(amount > BigDecimal.ZERO) {
"Amount to withdraw must be above zero"
}

val entity = jpaAccountOutAdapter.findById(iban.value)
.orElseThrow {
IllegalArgumentException("Account not found for Iban: ${iban.value}")
}

val account = accountEntityMapper.toDomain(entity)
account.withdraw(amount)
jpaAccountOutAdapter.save(accountEntityMapper.toEntity(account))
return account
override fun withdraw(account: Account): Account {
TODO("Not implemented yet")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package io.holixon.cqrshexagonaldemo.demoparent.transactions.application.port.ou

import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Account
import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account.Iban
import java.math.BigDecimal

interface AccountOutPort {
fun findAccount(iban: Iban): Account?
fun createAccount(account: Account): Account
fun deposit(iban: Iban, amount: BigDecimal): Account
fun withdraw(iban: Iban, amount: BigDecimal): Account
fun deposit(account: Account): Account
fun withdraw(account: Account): Account
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account

import io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.customer.CustomerNumber
import java.math.BigDecimal

data class Account(
var customerNumber: CustomerNumber,
var iban: Iban,
var balance: Money
) {
fun deposit(amount: BigDecimal) {
this.balance.amount.plus(amount)
fun deposit(amount: Amount) {
this.balance.amount += amount.value
}

fun withdraw(amount: BigDecimal) {
require(this.balance.amount > amount) {
fun withdraw(amount: Amount) {
require(this.balance.amount > amount.value) {
"Cannot withdraw more than account balance"
}
this.balance.amount -= amount
this.balance.amount -= amount.value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.account

import java.math.BigDecimal

class Amount(val value: BigDecimal) {
init {
require(value > BigDecimal.ZERO){
"Amount must not be negative or zero"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.holixon.cqrshexagonaldemo.demoparent.transactions.domain.model.accoun

import java.math.BigDecimal

//TODO: Find a better name (!?)
class Money(var amount: BigDecimal = BigDecimal.ZERO) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand Down

0 comments on commit 5062fcc

Please sign in to comment.