Created
July 8, 2021 19:15
-
-
Save jeroenr/be1054fa7e42b96a9564af20e6dcd8ff to your computer and use it in GitHub Desktop.
Domain service example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DepositService(private val exchangeRateApiClient: ExchangeRateApiClientPort) { | |
suspend fun deposit(userAccount: UserAccountDto, amount: Money): UserAccountDto { | |
require(amount.largerThanZero) { "Amount must be larger than 0" } | |
val rateToUsd = if (amount.currency != Currency.USD) { | |
exchangeRateApiClient.getRate(amount.currency, Currency.USD) | |
} else { | |
ExchangeRateDto(BigDecimal.ONE, Currency.USD) | |
} | |
val rateToPref = if (userAccount.balance.currency != Currency.USD) { | |
exchangeRateApiClient.getRate(Currency.USD, userAccount.balance.currency) | |
} else { | |
ExchangeRateDto(BigDecimal.ONE, Currency.USD) | |
} | |
return userAccount.copy( | |
balance = userAccount.balance.add( | |
amount | |
.convert(rateToUsd) | |
.convert(rateToPref) | |
) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment