diff --git a/src/main/kotlin/camilyed/github/io/currencyexchangeapi/application/AccountService.kt b/src/main/kotlin/camilyed/github/io/currencyexchangeapi/application/AccountService.kt index b2c1050..b902965 100644 --- a/src/main/kotlin/camilyed/github/io/currencyexchangeapi/application/AccountService.kt +++ b/src/main/kotlin/camilyed/github/io/currencyexchangeapi/application/AccountService.kt @@ -8,7 +8,7 @@ import camilyed.github.io.currencyexchangeapi.domain.AccountRepository import camilyed.github.io.currencyexchangeapi.domain.AccountSnapshot import camilyed.github.io.currencyexchangeapi.domain.CreateAccountData import camilyed.github.io.currencyexchangeapi.domain.CurrentExchangeRateProvider -import camilyed.github.io.currencyexchangeapi.infrastructure.executeInTransaction +import camilyed.github.io.currencyexchangeapi.infrastructure.inTransaction import java.util.UUID class AccountService( @@ -20,11 +20,11 @@ class AccountService( fun create(command: CreateAccountCommand): AccountSnapshot { val accountId = accountOperationRepository.findAccountIdBy(command.commandId) if (accountId != null) { - return executeInTransaction { findAccount(accountId).toSnapshot() } + return inTransaction { findAccount(accountId).toSnapshot() } } val id = repository.nextAccountId() val account = Account.createNewAccount(command.toCreateAccountData(id)) - executeInTransaction { + inTransaction { repository.save(account) val events = account.getEvents() accountOperationRepository.save(events) @@ -35,16 +35,16 @@ class AccountService( fun exchangePlnToUsd(command: ExchangePlnToUsdCommand): AccountSnapshot { val accountId = accountOperationRepository.findAccountIdBy(command.commandId) if (accountId != null) { - return executeInTransaction { findAccount(accountId).toSnapshot() } + return inTransaction { findAccount(accountId).toSnapshot() } } - val account = executeInTransaction { findAccount(command.accountId) } + val account = inTransaction { findAccount(command.accountId) } val currentExchange = currentExchangeRateProvider.currentExchange() account.exchangePlnToUsd( amountPln = Money.pln(command.amount), exchangeRate = currentExchange, operationId = command.commandId, ) - executeInTransaction { + inTransaction { repository.save(account) accountOperationRepository.save(account.getEvents()) } diff --git a/src/main/kotlin/camilyed/github/io/currencyexchangeapi/domain/Account.kt b/src/main/kotlin/camilyed/github/io/currencyexchangeapi/domain/Account.kt index e198163..5b24184 100644 --- a/src/main/kotlin/camilyed/github/io/currencyexchangeapi/domain/Account.kt +++ b/src/main/kotlin/camilyed/github/io/currencyexchangeapi/domain/Account.kt @@ -61,10 +61,6 @@ class Account private constructor( balancePln += amountPln } - private fun addEvent(event: AccountEvent) { - events.add(event) - } - fun getEvents(): List = events.toList() fun toSnapshot(): AccountSnapshot { @@ -76,6 +72,10 @@ class Account private constructor( ) } + private fun addEvent(event: AccountEvent) { + events.add(event) + } + companion object { fun createNewAccount(data: CreateAccountData): Account { val account = Account( diff --git a/src/main/kotlin/camilyed/github/io/currencyexchangeapi/infrastructure/TransactionManagerConfig.kt b/src/main/kotlin/camilyed/github/io/currencyexchangeapi/infrastructure/TransactionManagerConfig.kt index 37acd9a..8899e8f 100644 --- a/src/main/kotlin/camilyed/github/io/currencyexchangeapi/infrastructure/TransactionManagerConfig.kt +++ b/src/main/kotlin/camilyed/github/io/currencyexchangeapi/infrastructure/TransactionManagerConfig.kt @@ -6,11 +6,11 @@ import jakarta.annotation.PostConstruct import org.jetbrains.exposed.sql.transactions.transaction import org.springframework.context.annotation.Configuration -fun executeInTransaction(block: () -> T): T { - return inTransaction(block as () -> Any) as T +fun inTransaction(block: () -> T): T { + return executeInTransaction(block as () -> Any) as T } -private var inTransaction: (() -> Any) -> Any = { block -> +private var executeInTransaction: (() -> Any) -> Any = { block -> block() } @@ -19,7 +19,7 @@ class TransactionManagerConfig { @PostConstruct fun setupProductionTransaction() { - inTransaction = { block -> + executeInTransaction = { block -> transaction { block() } } }