fix: fetch for DashboardView

This commit is contained in:
effe 2024-09-14 13:20:25 -04:00
parent b476cb9846
commit 95b0508cab
3 changed files with 12 additions and 20 deletions

View file

@ -34,11 +34,11 @@ public class Expense {
@Column(name = "Description")
private String description;
@Column(name = "PeriodicExpense", nullable = false)
@Column(name = "Periodic", nullable = false)
private Boolean isPeriodic;
@Enumerated(EnumType.ORDINAL)
@Column(name = "PeriodUnit")
@Column(name = "Period")
private PeriodUnit periodUnit;
@Column(name = "PeriodInterval")
@ -62,7 +62,7 @@ public class Expense {
private Boolean isPaid = false;
@Enumerated(EnumType.STRING)
@Column(name = "expenseType", nullable = false)
@Column(name = "type", nullable = false)
private ExpenseType expenseType;
@Column(name = "userId", nullable = false)

View file

@ -121,15 +121,6 @@ public class ExpenseService {
return this.expenseRepository.findByUserIdOrderByDateDesc(userId);
}
/**
* Finds expenses by year excluding those marked as credit and paid.
* @param year the year for which to find expenses
* @return the list of expenses found
*/
public List<Expense> findExpensesByYearExcludingCreditPaid(int year) {
return expenseRepository.findByYearAndFilterCreditPaid(year, ExpenseType.CREDIT);
}
/**
* Checks if an expense has been paid.
* @param expense the expense to check

View file

@ -32,12 +32,14 @@ public class DashboardView extends Div {
private final PersonService personService;
private final UserService userService;
private final User loggedUser;
private final Person loggedPerson;
public DashboardView(final ExpenseService expenseService, final PersonService personService, UserService userService) {
this.expenseService = expenseService;
this.personService = personService;
this.userService = userService;
loggedUser = userService.getLoggedInUser();
loggedPerson = personService.getLoggedInPerson();
addClassName("highcharts-view"); // Optional CSS class for styling
VerticalLayout mainLayout = new VerticalLayout();
@ -100,19 +102,18 @@ public class DashboardView extends Div {
String barChartJs = generateBarChartScript();
String pieChartJs = generatePieChartScript();
String bottomLeftChartJs = generateNegativeColumnChartScript();
String bottomRightChartJs = generateExpensesOverTimeByCategoryScript();
String negativeColumnChartJs = generateNegativeColumnChartScript();
String expensesOverTimeByCategoryChart = generateExpensesOverTimeByCategoryScript();
// Execute the JavaScript to initialize the charts
getElement().executeJs(barChartJs);
getElement().executeJs(pieChartJs);
getElement().executeJs(bottomLeftChartJs);
getElement().executeJs(bottomRightChartJs);
getElement().executeJs(negativeColumnChartJs);
getElement().executeJs(expensesOverTimeByCategoryChart);
}
private String generateBarChartScript() {
final var loggedInPerson = this.personService.getLoggedInPerson();
List<Expense> expenses = expenseService.fetchExpensesForDashboard(loggedInPerson, Year.now());
List<Expense> expenses = expenseService.fetchExpensesForDashboard(loggedPerson, Year.now());
// Prepare data for Highcharts
Map<String, Double> monthlyData = new LinkedHashMap<>();
@ -157,7 +158,7 @@ public class DashboardView extends Div {
}
private String generatePieChartScript() {
List<Expense> expenses = expenseService.findExpensesByYearExcludingCreditPaid(Year.now().getValue());
List<Expense> expenses = expenseService.fetchExpensesForDashboard(loggedPerson, Year.now());
// Group expenses by category name and sum their costs
Map<String, Double> categoryData = expenses.stream()
@ -275,7 +276,7 @@ public class DashboardView extends Div {
}
private String generateExpensesOverTimeByCategoryScript() {
List<Expense> expenses = expenseService.findExpensesByYearExcludingCreditPaid(Year.now().getValue());
List<Expense> expenses = expenseService.fetchExpensesForDashboard(loggedPerson, Year.now());
// Group expenses by category and by month
Map<String, Map<String, Double>> categoryMonthlyData = new LinkedHashMap<>();