diff --git a/src/main/java/com/application/munera/repositories/ExpenseRepository.java b/src/main/java/com/application/munera/repositories/ExpenseRepository.java index 380cf97..839770f 100644 --- a/src/main/java/com/application/munera/repositories/ExpenseRepository.java +++ b/src/main/java/com/application/munera/repositories/ExpenseRepository.java @@ -12,6 +12,8 @@ import java.util.List; import java.util.Set; public interface ExpenseRepository extends JpaRepository, JpaSpecificationExecutor { + @Query("SELECT DISTINCT YEAR(e.date) FROM Expense e WHERE e.userId = :userId ORDER BY YEAR(e.date)") + List findExpenseYearsByUserId(@Param("userId") Long userId); // Find expenses where the payer is a specific person @Query("SELECT e FROM Expense e WHERE e.payer.id = :personId") diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index 21ff86a..121303c 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -10,7 +10,6 @@ import com.application.munera.security.SecurityUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; -import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; @@ -204,6 +203,15 @@ public class ExpenseService { return totalExpenses; } + /** + * Gets the list of years in which at least one expense has been made + * @param userId the user from which to look into the expenses + * @return the list of years found + */ + public List getAvailableExpenseYearsForUser(Long userId) { + return expenseRepository.findExpenseYearsByUserId(userId); + } + // ================================ // Private methods // ================================ diff --git a/src/main/java/com/application/munera/views/dashboard/DashboardView.java b/src/main/java/com/application/munera/views/dashboard/DashboardView.java index 6f11b7b..cf6a6bb 100644 --- a/src/main/java/com/application/munera/views/dashboard/DashboardView.java +++ b/src/main/java/com/application/munera/views/dashboard/DashboardView.java @@ -44,7 +44,7 @@ public class DashboardView extends Div { addClassName("highcharts-view"); // Optional CSS class for styling // Fetch available years from the database - List availableYears = List.of(Integer.valueOf("2024"), Integer.valueOf("2023")); + List availableYears = this.expenseService.getAvailableExpenseYearsForUser(loggedUser.getId()); // Initialize the ComboBox for year selection yearComboBox = new ComboBox<>("Select Year"); @@ -115,9 +115,7 @@ public class DashboardView extends Div { bottomRowLayout.add(bottomRightChartDiv); mainLayout.add(bottomRowLayout); - add(mainLayout); - updateCharts(Year.now()); }