feat: added stiff to ViewService

This commit is contained in:
filippo-ferrari 2024-09-07 18:09:36 +02:00
parent 5eea4149f7
commit a05c77e8ff
3 changed files with 21 additions and 21 deletions

View file

@ -6,6 +6,8 @@ import com.application.munera.data.ExpenseType;
import com.vaadin.flow.component.html.Span;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
@Service
public class ViewService {
@ -15,7 +17,7 @@ public class ViewService {
this.expenseService = expenseService;
}
public Span createBadge(final Expense expense) {
public Span createExpenseBadge(final Expense expense) {
final var isExpensePaid = Boolean.TRUE.equals(this.expenseService.isExpensePaid(expense));
final var badgeMessage = determineBadgeMessage(expense.getExpenseType(), isExpensePaid);
@ -26,6 +28,21 @@ public class ViewService {
return badge;
}
public Span createPersonBadge(BigDecimal netBalance) {
Span badge = new Span();
if (netBalance.compareTo(BigDecimal.ZERO) < 0) {
badge.setText("Credit");
badge.getElement().getThemeList().add("badge success");
} else if (netBalance.compareTo(BigDecimal.ZERO) > 0) {
badge.setText("Debit");
badge.getElement().getThemeList().add("badge error");
} else {
badge.setText("Clear");
badge.getElement().getThemeList().add("badge contrast");
}
return badge;
}
private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) {
return switch (type) {
case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE;

View file

@ -96,7 +96,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date");
// grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true);
grid.addColumn(new ComponentRenderer<>(this.viewService::createBadge)).setHeader("Status").setSortable(true);
grid.addColumn(new ComponentRenderer<>(this.viewService::createExpenseBadge)).setHeader("Status").setSortable(true);
grid.getColumns().forEach(col -> col.setAutoWidth(true));
grid.setItems(this.expenseService.findAllOrderByDateDescending());

View file

@ -13,7 +13,6 @@ import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.component.formlayout.FormLayout;
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;
@ -33,7 +32,6 @@ import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
@ -80,8 +78,8 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true);
grid.addColumn(new ComponentRenderer<>(persona -> {
if (persona instanceof Person) return createPersonBadge(personService.calculateNetBalance((Person) persona));
else return this.viewService.createBadge(((Expense) persona));
if (persona instanceof Person) return this.viewService.createPersonBadge(personService.calculateNetBalance((Person) persona));
else return this.viewService.createExpenseBadge(((Expense) persona));
})).setHeader("Balance Status");
List<Person> people = (List<Person>) personService.findAll();
@ -227,21 +225,6 @@ public class PeopleView extends Div implements BeforeEnterObserver {
}
private Span createPersonBadge(BigDecimal netBalance) {
Span badge = new Span();
if (netBalance.compareTo(BigDecimal.ZERO) < 0) {
badge.setText("Credit");
badge.getElement().getThemeList().add("badge success");
} else if (netBalance.compareTo(BigDecimal.ZERO) > 0) {
badge.setText("Debit");
badge.getElement().getThemeList().add("badge error");
} else {
badge.setText("Clear");
badge.getElement().getThemeList().add("badge contrast");
}
return badge;
}
public void setGridData(List<Person> people) {
for (Person user : people) {
// Add the person as a root item