From 23b9e09aab7ba55f434eabda914b1dde911f10ae Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sun, 7 Jul 2024 17:21:46 +0200 Subject: [PATCH] feat: added expense status indicator --- .../com/application/munera/data/Expense.java | 8 ++++++- .../repositories/ExpenseRepository.java | 2 ++ .../munera/services/ExpenseService.java | 4 ++++ .../munera/views/expenses/ExpensesView.java | 21 ++++++++++++++++++- .../munera/views/expenses/PeopleView.java | 3 ++- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/application/munera/data/Expense.java b/src/main/java/com/application/munera/data/Expense.java index 807dedb..b005ffe 100644 --- a/src/main/java/com/application/munera/data/Expense.java +++ b/src/main/java/com/application/munera/data/Expense.java @@ -13,7 +13,7 @@ import java.util.Set; @Getter @Setter @Table(name = "expenses") -public class Expense { +public class Expense { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -63,4 +63,10 @@ public class Expense { @Column(name = "Date", nullable = false, columnDefinition = "DATE DEFAULT CURRENT_DATE") private LocalDate date; + + /** + * the isResolved field starts as always false, cause at creation an expense cant be already be resolved + */ + @Column(name = "isResolved", nullable = false) + private Boolean isResolved = false; } diff --git a/src/main/java/com/application/munera/repositories/ExpenseRepository.java b/src/main/java/com/application/munera/repositories/ExpenseRepository.java index 8893a3b..ba7f0d4 100644 --- a/src/main/java/com/application/munera/repositories/ExpenseRepository.java +++ b/src/main/java/com/application/munera/repositories/ExpenseRepository.java @@ -20,4 +20,6 @@ public interface ExpenseRepository extends JpaRepository, JpaSpec @Query("SELECT e FROM Expense e WHERE YEAR(e.date) = :year") List findAllByYear(@Param("year") int year); + + boolean existsByIdAndIsResolvedTrue(Long id); } \ No newline at end of file diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index d92fc9f..74964b1 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -58,4 +58,8 @@ public class ExpenseService { return this.repository.findAllByYear(year); } + public boolean isExpenseResolved(final Expense expense) { + return this.repository.existsByIdAndIsResolvedTrue(expense.getId()); + } + } 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 279a08f..9c58faa 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -18,6 +18,7 @@ import com.vaadin.flow.component.formlayout.FormLayout; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.GridVariant; import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.notification.Notification.Position; @@ -28,13 +29,17 @@ import com.vaadin.flow.component.textfield.TextArea; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.data.binder.BeanValidationBinder; import com.vaadin.flow.data.binder.ValidationException; +import com.vaadin.flow.data.renderer.ComponentRenderer; import com.vaadin.flow.data.renderer.LitRenderer; import com.vaadin.flow.router.*; import com.vaadin.flow.spring.data.VaadinSpringDataHelpers; import org.springframework.data.domain.PageRequest; import org.springframework.orm.ObjectOptimisticLockingFailureException; -import java.util.*; +import java.util.HashSet; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; @PageTitle("Expenses") @Route(value = "/:expenseID?/:action?(edit)", layout = MainLayout.class) @@ -92,6 +97,8 @@ public class ExpensesView extends Div implements BeforeEnterObserver { grid.addColumn(Expense::getPeriodUnit).setHeader("Period Unit").setSortable(true); grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date"); // grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true); + + grid.addColumn(new ComponentRenderer<>(expense1 -> createBadge(expenseService.isExpenseResolved(expense1)))).setHeader("Status").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.setItems(query -> expenseService.list( @@ -287,4 +294,16 @@ public class ExpensesView extends Div implements BeforeEnterObserver { periodUnit.setVisible(isPeriodicChecked); periodInterval.setVisible(isPeriodicChecked); } + + private Span createBadge(Boolean isExpenseResolved) { + Span badge = new Span(); + if (Boolean.TRUE.equals(isExpenseResolved)) { + badge.setText("Resolved"); + badge.getElement().getThemeList().add("badge success"); + } else { + badge.setText("To be Resolved"); + badge.getElement().getThemeList().add("badge error"); + } + return badge; + } } diff --git a/src/main/java/com/application/munera/views/expenses/PeopleView.java b/src/main/java/com/application/munera/views/expenses/PeopleView.java index 35a7fbb..b7bf2cf 100644 --- a/src/main/java/com/application/munera/views/expenses/PeopleView.java +++ b/src/main/java/com/application/munera/views/expenses/PeopleView.java @@ -234,4 +234,5 @@ public class PeopleView extends Div implements BeforeEnterObserver { badge.getElement().getThemeList().add("badge contrast"); } return badge; - }} \ No newline at end of file + } +} \ No newline at end of file