From 86671693d2ee2a3b3b6cf034b9869c4f9e534dde Mon Sep 17 00:00:00 2001 From: effe Date: Sun, 15 Sep 2024 16:22:17 -0400 Subject: [PATCH] feat: expenses name filtering --- .../munera/services/ViewsService.java | 17 +++++++++++++++++ .../munera/views/expenses/ExpensesView.java | 18 +----------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/application/munera/services/ViewsService.java b/src/main/java/com/application/munera/services/ViewsService.java index a806a4b..cb16f65 100644 --- a/src/main/java/com/application/munera/services/ViewsService.java +++ b/src/main/java/com/application/munera/services/ViewsService.java @@ -4,9 +4,13 @@ import com.application.munera.data.BadgeMessage; import com.application.munera.data.Expense; import com.application.munera.data.ExpenseType; import com.vaadin.flow.component.html.Span; +import com.vaadin.flow.component.textfield.TextField; import org.springframework.stereotype.Service; +import org.vaadin.klaudeta.PaginatedGrid; import java.math.BigDecimal; +import java.util.List; +import java.util.Objects; @Service public class ViewsService { @@ -43,6 +47,19 @@ public class ViewsService { return badge; } + public void applyFilter(TextField nameFilter, Long userId, PaginatedGrid grid) { + String filterValue = nameFilter.getValue().trim(); + List filteredExpenses; + if (filterValue.isEmpty()) filteredExpenses = expenseService.findAllOrderByDateDescending(userId); // If the filter is empty, return all expenses + else { + // Apply the filter (e.g., by name) + filteredExpenses = expenseService.findAllOrderByDateDescending(userId) + .stream() + .filter(expense -> expense.getName().toLowerCase().contains(filterValue.toLowerCase())).toList(); + } + grid.setItems(filteredExpenses); + } + private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) { return switch (type) { case CREDIT -> isPaid ? BadgeMessage.PAID_TO_ME : BadgeMessage.OWED_TO_ME; diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 8e96a6c..fd48fd3 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -41,7 +41,6 @@ import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.vaadin.klaudeta.PaginatedGrid; import java.math.BigDecimal; -import java.util.List; import java.util.Objects; import java.util.Optional; @@ -119,7 +118,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { nameFilter.setPlaceholder("Filter by Name..."); nameFilter.setClearButtonVisible(true); nameFilter.setValueChangeMode(ValueChangeMode.LAZY); - nameFilter.addValueChangeListener(e -> applyFilter()); + nameFilter.addValueChangeListener(e -> this.viewsService.applyFilter(nameFilter, userId, grid)); // Add nameFilter field to layout (above the grid) VerticalLayout layout = new VerticalLayout(); @@ -333,19 +332,4 @@ public class ExpensesView extends Div implements BeforeEnterObserver { payer.setValue(loggedInPerson); beneficiary.setValue(loggedInPerson); } - private void applyFilter() { - String filterValue = nameFilter.getValue().trim(); - List filteredExpenses; - - if (filterValue.isEmpty()) { - // If the filter is empty, return all expenses - filteredExpenses = expenseService.findAllOrderByDateDescending(userId); - } else { - filteredExpenses = expenseService.findAllOrderByDateDescending(userId) - .stream() - .filter(expense1 -> expense1.getName().toLowerCase().contains(filterValue.toLowerCase())) - .toList(); - } - grid.setItems(filteredExpenses); - } } \ No newline at end of file