feat: added stiff to ViewService
This commit is contained in:
parent
5eea4149f7
commit
a05c77e8ff
3 changed files with 21 additions and 21 deletions
|
@ -6,6 +6,8 @@ import com.application.munera.data.ExpenseType;
|
||||||
import com.vaadin.flow.component.html.Span;
|
import com.vaadin.flow.component.html.Span;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ViewService {
|
public class ViewService {
|
||||||
|
|
||||||
|
@ -15,7 +17,7 @@ public class ViewService {
|
||||||
this.expenseService = expenseService;
|
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 isExpensePaid = Boolean.TRUE.equals(this.expenseService.isExpensePaid(expense));
|
||||||
final var badgeMessage = determineBadgeMessage(expense.getExpenseType(), isExpensePaid);
|
final var badgeMessage = determineBadgeMessage(expense.getExpenseType(), isExpensePaid);
|
||||||
|
|
||||||
|
@ -26,6 +28,21 @@ public class ViewService {
|
||||||
return badge;
|
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) {
|
private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) {
|
||||||
return switch (type) {
|
return switch (type) {
|
||||||
case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE;
|
case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE;
|
||||||
|
|
|
@ -96,7 +96,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
|
||||||
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date");
|
grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date");
|
||||||
// grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true);
|
// 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.getColumns().forEach(col -> col.setAutoWidth(true));
|
||||||
|
|
||||||
grid.setItems(this.expenseService.findAllOrderByDateDescending());
|
grid.setItems(this.expenseService.findAllOrderByDateDescending());
|
||||||
|
|
|
@ -13,7 +13,6 @@ import com.vaadin.flow.component.dependency.Uses;
|
||||||
import com.vaadin.flow.component.formlayout.FormLayout;
|
import com.vaadin.flow.component.formlayout.FormLayout;
|
||||||
import com.vaadin.flow.component.grid.GridVariant;
|
import com.vaadin.flow.component.grid.GridVariant;
|
||||||
import com.vaadin.flow.component.html.Div;
|
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.icon.Icon;
|
||||||
import com.vaadin.flow.component.notification.Notification;
|
import com.vaadin.flow.component.notification.Notification;
|
||||||
import com.vaadin.flow.component.notification.Notification.Position;
|
import com.vaadin.flow.component.notification.Notification.Position;
|
||||||
|
@ -33,7 +32,6 @@ import com.vaadin.flow.router.Route;
|
||||||
import jakarta.annotation.security.PermitAll;
|
import jakarta.annotation.security.PermitAll;
|
||||||
import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@ -80,8 +78,8 @@ public class PeopleView extends Div implements BeforeEnterObserver {
|
||||||
grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
|
grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
|
||||||
grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true);
|
grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true);
|
||||||
grid.addColumn(new ComponentRenderer<>(persona -> {
|
grid.addColumn(new ComponentRenderer<>(persona -> {
|
||||||
if (persona instanceof Person) return createPersonBadge(personService.calculateNetBalance((Person) persona));
|
if (persona instanceof Person) return this.viewService.createPersonBadge(personService.calculateNetBalance((Person) persona));
|
||||||
else return this.viewService.createBadge(((Expense) persona));
|
else return this.viewService.createExpenseBadge(((Expense) persona));
|
||||||
})).setHeader("Balance Status");
|
})).setHeader("Balance Status");
|
||||||
|
|
||||||
List<Person> people = (List<Person>) personService.findAll();
|
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) {
|
public void setGridData(List<Person> people) {
|
||||||
for (Person user : people) {
|
for (Person user : people) {
|
||||||
// Add the person as a root item
|
// Add the person as a root item
|
||||||
|
|
Loading…
Reference in a new issue