Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.jedrzejuk committed Oct 9, 2024
1 parent faec879 commit c4af2f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ class Account private constructor(
balancePln += amountPln
}

private fun addEvent(event: AccountEvent) {
events.add(event)
}

fun getEvents(): List<AccountEvent> = events.toList()

fun toSnapshot(): AccountSnapshot {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import jakarta.annotation.PostConstruct
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.context.annotation.Configuration

fun <T> executeInTransaction(block: () -> T): T {
return inTransaction(block as () -> Any) as T
fun <T> inTransaction(block: () -> T): T {
return executeInTransaction(block as () -> Any) as T
}

private var inTransaction: (() -> Any) -> Any = { block ->
private var executeInTransaction: (() -> Any) -> Any = { block ->
block()
}

Expand All @@ -19,7 +19,7 @@ class TransactionManagerConfig {

@PostConstruct
fun setupProductionTransaction() {
inTransaction = { block ->
executeInTransaction = { block ->
transaction { block() }
}
}
Expand Down

0 comments on commit c4af2f6

Please sign in to comment.