Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Created July 8, 2021 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenr/be1054fa7e42b96a9564af20e6dcd8ff to your computer and use it in GitHub Desktop.
Save jeroenr/be1054fa7e42b96a9564af20e6dcd8ff to your computer and use it in GitHub Desktop.
Domain service example
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