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

View file

@ -121,15 +121,6 @@ public class ExpenseService {
return this.expenseRepository.findByUserIdOrderByDateDesc(userId); 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. * Checks if an expense has been paid.
* @param expense the expense to check * @param expense the expense to check

View file

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