feat: DashboardView year selector

This commit is contained in:
effe 2024-09-14 16:05:45 -04:00
parent 76e17cb152
commit d67359ff11
3 changed files with 12 additions and 4 deletions

View file

@ -12,6 +12,8 @@ import java.util.List;
import java.util.Set; import java.util.Set;
public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpecificationExecutor<Expense> { public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpecificationExecutor<Expense> {
@Query("SELECT DISTINCT YEAR(e.date) FROM Expense e WHERE e.userId = :userId ORDER BY YEAR(e.date)")
List<Integer> findExpenseYearsByUserId(@Param("userId") Long userId);
// Find expenses where the payer is a specific person // Find expenses where the payer is a specific person
@Query("SELECT e FROM Expense e WHERE e.payer.id = :personId") @Query("SELECT e FROM Expense e WHERE e.payer.id = :personId")

View file

@ -10,7 +10,6 @@ import com.application.munera.security.SecurityUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -204,6 +203,15 @@ public class ExpenseService {
return totalExpenses; 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<Integer> getAvailableExpenseYearsForUser(Long userId) {
return expenseRepository.findExpenseYearsByUserId(userId);
}
// ================================ // ================================
// Private methods // Private methods
// ================================ // ================================

View file

@ -44,7 +44,7 @@ public class DashboardView extends Div {
addClassName("highcharts-view"); // Optional CSS class for styling addClassName("highcharts-view"); // Optional CSS class for styling
// Fetch available years from the database // Fetch available years from the database
List<Integer> availableYears = List.of(Integer.valueOf("2024"), Integer.valueOf("2023")); List<Integer> availableYears = this.expenseService.getAvailableExpenseYearsForUser(loggedUser.getId());
// Initialize the ComboBox for year selection // Initialize the ComboBox for year selection
yearComboBox = new ComboBox<>("Select Year"); yearComboBox = new ComboBox<>("Select Year");
@ -115,9 +115,7 @@ public class DashboardView extends Div {
bottomRowLayout.add(bottomRightChartDiv); bottomRowLayout.add(bottomRightChartDiv);
mainLayout.add(bottomRowLayout); mainLayout.add(bottomRowLayout);
add(mainLayout); add(mainLayout);
updateCharts(Year.now()); updateCharts(Year.now());
} }