refactor: PeopleView

This commit is contained in:
effe 2024-09-14 14:18:06 -04:00
parent a43eccadeb
commit 3ea8e48ed9

View file

@ -60,19 +60,15 @@ public class PeopleView extends Div implements BeforeEnterObserver {
private Person person; private Person person;
private User loggedUser; private User loggedUser;
private Long userId; private Long userId;
private final PersonService personService; private final UserService userService;
private final PersonFacade personFacade; private final PersonFacade personFacade;
private final ExpenseFacade expenseFacade; private final ExpenseFacade expenseFacade;
private final ExpenseService expenseService;
private final ViewsService viewsService; private final ViewsService viewsService;
private final UserService userService;
private TextField firstName; private TextField firstName;
private TextField lastName; private TextField lastName;
private EmailField email; private EmailField email;
public PeopleView(PersonService personService, ExpenseService expenseService, ViewsService viewsService, PersonFacade personFacade, ExpenseFacade expenseFacade, UserService userService) { public PeopleView(ViewsService viewsService, PersonFacade personFacade, ExpenseFacade expenseFacade, UserService userService) {
this.personService = personService;
this.expenseService = expenseService;
this.viewsService = viewsService; this.viewsService = viewsService;
this.personFacade = personFacade; this.personFacade = personFacade;
this.expenseFacade = expenseFacade; this.expenseFacade = expenseFacade;
@ -93,7 +89,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.addHierarchyColumn(this::getNodeName).setHeader("Name"); grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
grid.addColumn(this::getNodeCost).setHeader("Balance").setSortable(true); grid.addColumn(this::getNodeCost).setHeader("Balance").setSortable(true);
grid.addColumn(new ComponentRenderer<>(personEntry -> { grid.addColumn(new ComponentRenderer<>(personEntry -> {
if (personEntry instanceof Person person1) return this.viewsService.createPersonBadge(personService.calculateNetBalance(person1)); if (personEntry instanceof Person person1) return this.viewsService.createPersonBadge(personFacade.calculateNetBalance(person1));
else return this.viewsService.createExpenseBadge(((Expense) personEntry)); else return this.viewsService.createExpenseBadge(((Expense) personEntry));
})).setHeader("Balance Status"); })).setHeader("Balance Status");
@ -124,7 +120,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
} else return new Span(); } else return new Span();
})); }));
List<Person> people = personService.findAllExcludeLoggedUser(loggedUser); List<Person> people = personFacade.findAllExcludeLoggedUser(loggedUser);
this.setGridData(people); this.setGridData(people);
@ -156,7 +152,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
try { try {
if (this.person == null) this.person = new Person(); if (this.person == null) this.person = new Person();
binder.writeBean(this.person); binder.writeBean(this.person);
personService.update(this.person, userId); personFacade.update(this.person, userId);
clearForm(); clearForm();
refreshGrid(); refreshGrid();
Notification.show("Data updated"); Notification.show("Data updated");
@ -174,7 +170,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
delete.addClickListener(e -> { delete.addClickListener(e -> {
try { try {
if (this.person == null) throw new RuntimeException("The person is null!"); //TODO: create proper exception if (this.person == null) throw new RuntimeException("The person is null!"); //TODO: create proper exception
personService.delete(this.person.getId()); personFacade.delete(this.person.getId());
clearForm(); clearForm();
refreshGrid(); refreshGrid();
Notification.show("Data delete"); Notification.show("Data delete");
@ -195,7 +191,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
} }
private String getNodeCost(Object node) { private String getNodeCost(Object node) {
if (node instanceof Person person1) return this.personService.calculateNetBalance(person1) + ""; if (node instanceof Person person1) return this.personFacade.calculateNetBalance(person1) + "";
else if (node instanceof Expense expense1) return (expense1).getCost().toString() + ""; else if (node instanceof Expense expense1) return (expense1).getCost().toString() + "";
return ""; return "";
} }
@ -204,7 +200,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
public void beforeEnter(BeforeEnterEvent event) { public void beforeEnter(BeforeEnterEvent event) {
Optional<Long> personId = event.getRouteParameters().get(PERSON_ID).map(Long::parseLong); Optional<Long> personId = event.getRouteParameters().get(PERSON_ID).map(Long::parseLong);
if (personId.isPresent()) { if (personId.isPresent()) {
Optional<Person> personFromBackend = personService.get(personId.get()); Optional<Person> personFromBackend = personFacade.findById(personId.get());
if (personFromBackend.isPresent()) populateForm(personFromBackend.get()); if (personFromBackend.isPresent()) populateForm(personFromBackend.get());
else { else {
Notification.show( Notification.show(
@ -276,7 +272,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.getTreeData().addItem(null, person); grid.getTreeData().addItem(null, person);
// Fetch expenses for the current person // Fetch expenses for the current person
List<Expense> expenses = expenseService.findExpensesByPerson(person); List<Expense> expenses = expenseFacade.findExpensesByPerson(person);
// Add each expense as a child item under the person // Add each expense as a child item under the person
for (Expense expense : expenses) grid.getTreeData().addItem(person, expense); for (Expense expense : expenses) grid.getTreeData().addItem(person, expense);