Add current assets to status on overview page

This commit is contained in:
2019-06-24 21:11:36 +02:00
parent 30ebc2a154
commit 6ec073d08e
10 changed files with 71 additions and 17 deletions

View File

@@ -86,4 +86,19 @@ public class AccountController {
return responseReason.toResponseEntity();
}
@RequestMapping("getCurrentAssets")
public Long getCurrentAssets() {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("/accounts/getCurrentAssets called");
}
final Long response = this.accountService.getCurrentAssets();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("/accounts/getCurrentAssets returns with %s", response));
}
return response;
}
}

View File

@@ -1,6 +1,8 @@
package de.financer.dba;
import de.financer.model.Account;
import de.financer.model.AccountType;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@@ -8,4 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional(propagation = Propagation.REQUIRED)
public interface AccountRepository extends CrudRepository<Account, Long> {
Account findByKey(String key);
@Query("SELECT SUM(a.currentBalance) FROM Account a WHERE a.type IN :accountTypes")
Long getCurrentAssets(AccountType... accountTypes);
}

View File

@@ -31,6 +31,7 @@ public class AccountService {
* This method returns the account identified by the given key.
*
* @param key the key to get the account for
*
* @return the account or <code>null</code> if no account with the given key can be found
*/
public Account getAccountByKey(String key) {
@@ -38,15 +39,16 @@ public class AccountService {
}
/**
* @return all existing accounts, regardless of their type or status. This explicitly covers accounts in {@link AccountStatus#CLOSED CLOSED} as well.
* @return all existing accounts, regardless of their type or status. This explicitly covers accounts in {@link
* AccountStatus#CLOSED CLOSED} as well.
*/
public Iterable<Account> getAll() {
return this.accountRepository.findAll();
}
/**
* This method saves the given account. It either updates the account if it already exists or inserts
* it if it's new.
* This method saves the given account. It either updates the account if it already exists or inserts it if it's
* new.
*
* @param account the account to save
*/
@@ -56,18 +58,18 @@ public class AccountService {
}
/**
* This method creates new account with the given key and type. The account has status {@link AccountStatus#OPEN OPEN}
* and a current balance of <code>0</code>.
* This method creates new account with the given key and type. The account has status {@link AccountStatus#OPEN
* OPEN} and a current balance of <code>0</code>.
*
* @param key the key of the new account
* @param type the type of the new account. Must be one of {@link AccountType}.
* @param accountGroupName the name of the account group to use, can be <code>null</code>
* @return {@link ResponseReason#INVALID_ACCOUNT_TYPE} if the given type is not a valid {@link AccountType},
* {@link ResponseReason#UNKNOWN_ERROR} if an unexpected error occurs,
* {@link ResponseReason#OK} if the operation completed successfully,
* {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key
* already exists and {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the optional parameter
* <code>accountGroupName</code> does not identify a valid account group. Never returns <code>null</code>.
*
* @return {@link ResponseReason#INVALID_ACCOUNT_TYPE} if the given type is not a valid {@link AccountType}, {@link
* ResponseReason#UNKNOWN_ERROR} if an unexpected error occurs, {@link ResponseReason#OK} if the operation completed
* successfully, {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key already exists and
* {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the optional parameter
* <code>accountGroupName</code> does not identify a valid account group. Never returns <code>null</code>.
*/
@Transactional(propagation = Propagation.SUPPORTS)
public ResponseReason createAccount(String key, String type, String accountGroupName) {
@@ -96,13 +98,11 @@ public class AccountService {
try {
this.accountRepository.save(account);
}
catch (DataIntegrityViolationException dive) {
} catch (DataIntegrityViolationException dive) {
LOGGER.error(String.format("Duplicate key! %s|%s|%s", key, type, accountGroupName), dive);
return ResponseReason.DUPLICATE_ACCOUNT_KEY;
}
catch (Exception e) {
} catch (Exception e) {
LOGGER.error(String.format("Could not save account %s|%s|%s", key, type, accountGroupName), e);
return ResponseReason.UNKNOWN_ERROR;
@@ -133,8 +133,7 @@ public class AccountService {
try {
this.accountRepository.save(account);
}
catch (Exception e) {
} catch (Exception e) {
LOGGER.error(String.format("Could not update account status %s|%s", key, accountStatus.name()), e);
return ResponseReason.UNKNOWN_ERROR;
@@ -142,4 +141,15 @@ public class AccountService {
return ResponseReason.OK;
}
/**
* Get the current assets for the specified account types, basically the sum of all the current balances of all
* accounts with the given types. We are specifying BANK and CASH here as these are virtually always available on a
* very short notice. So this is the amount of money we have at disposal right now.
*
* @return the current assets
*/
public Long getCurrentAssets() {
return this.accountRepository.getCurrentAssets(AccountType.BANK, AccountType.CASH);
}
}