Skip to content

Commit

Permalink
코드 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
gunkim committed May 20, 2024
1 parent abdd9ed commit aa69669
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ public void deposit(AccountId accountId, Money depositAmount) {
var account = findAccount(accountId);
var depositedBalanceAmount = account.deposit(depositAmount);

accountRepository.save(account);
transactionRepository.save(Transaction.of(TransactionType.DEPOSIT, depositAmount, depositedBalanceAmount));
persist(account, createTransaction(TransactionType.DEPOSIT, depositAmount, depositedBalanceAmount));
}

public void withdraw(AccountId accountId, Money withdrawAmount) {
var account = findAccount(accountId);
var withdrawnBalanceAmount = account.withdraw(withdrawAmount);

accountRepository.save(account);
transactionRepository.save(Transaction.of(TransactionType.WITHDRAW, withdrawAmount, withdrawnBalanceAmount));
persist(account, createTransaction(TransactionType.WITHDRAW, withdrawAmount, withdrawnBalanceAmount));
}

private Transaction createTransaction(TransactionType type, Money withdrawAmount, Money withdrawnBalanceAmount) {
return Transaction.of(type, withdrawAmount, withdrawnBalanceAmount);
}

private Account findAccount(AccountId accountId) {
return accountRepository.findById(accountId)
.orElseThrow(() -> new AccountNotFoundException("해당 account(%s)를 찾을 수 없습니다.".formatted(accountId)));
}

private void persist(Account account, Transaction transaction) {
accountRepository.save(account);
transactionRepository.save(transaction);
}
}

0 comments on commit aa69669

Please sign in to comment.