Created
September 26, 2019 07:37
-
-
Save thombergs/38fca5023c58e23e623e96e3ca28432c to your computer and use it in GitHub Desktop.
AccountPersistenceAdapter.java
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
@RequiredArgsConstructor | |
@Component | |
class AccountPersistenceAdapter implements | |
LoadAccountPort, | |
UpdateAccountStatePort { | |
private final AccountRepository accountRepository; | |
private final ActivityRepository activityRepository; | |
private final AccountMapper accountMapper; | |
@Override | |
public Account loadAccount( | |
AccountId accountId, | |
LocalDateTime baselineDate) { | |
AccountJpaEntity account = | |
accountRepository.findById(accountId.getValue()) | |
.orElseThrow(EntityNotFoundException::new); | |
List<ActivityJpaEntity> activities = | |
activityRepository.findByOwnerSince( | |
accountId.getValue(), | |
baselineDate); | |
Long withdrawalBalance = orZero(activityRepository | |
.getWithdrawalBalanceUntil( | |
accountId.getValue(), | |
baselineDate)); | |
Long depositBalance = orZero(activityRepository | |
.getDepositBalanceUntil( | |
accountId.getValue(), | |
baselineDate)); | |
return accountMapper.mapToDomainEntity( | |
account, | |
activities, | |
withdrawalBalance, | |
depositBalance); | |
} | |
private Long orZero(Long value){ | |
return value == null ? 0L : value; | |
} | |
@Override | |
public void updateActivities(Account account) { | |
for (Activity activity : account.getActivityWindow().getActivities()) { | |
if (activity.getId() == null) { | |
activityRepository.save(accountMapper.mapToJpaEntity(activity)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment