From cb38bdc479dc1c04b80c6732a3e8b8473bcfc31c Mon Sep 17 00:00:00 2001 From: Robert Goldmann <deadlocker@gmx.de> Date: Thu, 22 Jun 2023 22:06:56 +0200 Subject: [PATCH] #749 - added method to get account budget by ID --- .../budgetmaster/services/HelpersService.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/services/HelpersService.java b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/services/HelpersService.java index a8ba0af68..5944bdae7 100644 --- a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/services/HelpersService.java +++ b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/services/HelpersService.java @@ -28,10 +28,12 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.text.DecimalFormatSymbols; +import java.text.MessageFormat; import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @@ -175,20 +177,36 @@ public class HelpersService public int getCurrentAccountBudget() { - Account currentAccount = getCurrentAccount(); + final Account currentAccount = getCurrentAccount(); + return getBudgetForAccount(currentAccount); + } + + public int getAccountBudgetByID(Integer accountID) + { + final Optional<Account> accountOptional = accountRepository.findById(accountID); + if(accountOptional.isEmpty()) + { + throw new IllegalArgumentException(MessageFormat.format("No account with ID \"{0)\" found", accountID)); + } + + final Account account = accountOptional.get(); + return getBudgetForAccount(account); + } + + private int getBudgetForAccount(Account account) + { final LocalDate endDate = DateHelper.getCurrentDate(); - List<Transaction> transactions = transactionService.getTransactionsForAccountUntilDate(currentAccount, endDate, FilterConfiguration.DEFAULT); + List<Transaction> transactions = transactionService.getTransactionsForAccountUntilDate(account, endDate, FilterConfiguration.DEFAULT); int sum = 0; for(Transaction transaction : transactions) { - sum += getAmount(transaction, currentAccount); + sum += getAmount(transaction, account); } return sum; } - public List<RepeatingModifierType> getRepeatingModifierTypes() { return Arrays.asList(RepeatingModifierType.values()); -- GitLab