Add expense history to overview page

This commit is contained in:
2019-10-08 22:41:11 +02:00
parent 1fb4c8fc98
commit b81303f20d
20 changed files with 217 additions and 30 deletions

View File

@@ -11,6 +11,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("transactions")
public class TransactionController {
@@ -108,4 +110,19 @@ public class TransactionController {
return expensePeriodTotals;
}
@RequestMapping("getExpensesAllPeriods")
public List<Long> getExpensesAllPeriods() {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("/transactions/getExpensesAllPeriods called"));
}
final List<Long> response = this.transactionService.getExpensesAllPeriods();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("/transactions/getExpensesAllPeriods returns with %s", response));
}
return response;
}
}

View File

@@ -20,6 +20,9 @@ public interface TransactionRepository extends CrudRepository<Transaction, Long>
@Query("SELECT SUM(t.amount) FROM Transaction t JOIN t.periods p JOIN t.toAccount a WHERE a.type IN :expenseTypes AND p = :period")
Long getExpensesForPeriod(Period period, AccountType... expenseTypes);
@Query("SELECT SUM(t.amount) FROM Transaction t JOIN t.periods p JOIN t.toAccount a WHERE a.type IN :expenseTypes GROUP BY p ORDER BY p.start ASC")
List<Long> getExpensesForAllPeriods(AccountType... expenseTypes);
// The HQL contains a hack because Hibernate can't resolve the alias of the CASE column in the GROUP BY clause
// That's why the generated alias is used directly in the HQL. It will break if the columns in the SELECT clause get reordered
// col_2_0_ instead of AccType

View File

@@ -18,6 +18,7 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@Service
@@ -243,6 +244,10 @@ public class TransactionService {
return Optional.ofNullable(expensesCurrentPeriod).orElse(Long.valueOf(0l));
}
public List<Long> getExpensesAllPeriods() {
return this.transactionRepository.getExpensesForAllPeriods(AccountType.EXPENSE, AccountType.LIABILITY);
}
public Iterable<ExpensePeriodTotal> getExpensePeriodTotals(Integer year) {
final Iterable<Period> periods = this.periodService.getAllExpensePeriodsForYear(year);