From a99837e14e8b7b9e1c6005dee826caeee3831074 Mon Sep 17 00:00:00 2001 From: effe Date: Tue, 10 Sep 2024 10:42:27 -0400 Subject: [PATCH] refactor: moved view methods to facade --- .../munera/facades/ExpenseFacade.java | 24 ++++++++ .../munera/facades/PersonFacade.java | 54 ++++++++++++++++++ .../munera/views/people/PeopleView.java | 56 ++++--------------- 3 files changed, 88 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/application/munera/facades/ExpenseFacade.java create mode 100644 src/main/java/com/application/munera/facades/PersonFacade.java diff --git a/src/main/java/com/application/munera/facades/ExpenseFacade.java b/src/main/java/com/application/munera/facades/ExpenseFacade.java new file mode 100644 index 0000000..09fa021 --- /dev/null +++ b/src/main/java/com/application/munera/facades/ExpenseFacade.java @@ -0,0 +1,24 @@ +package com.application.munera.facades; + +import com.application.munera.data.Expense; +import com.application.munera.services.ExpenseService; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.treegrid.TreeGrid; +import org.springframework.stereotype.Component; + +@Component +public class ExpenseFacade { + public final ExpenseService expenseService; + + public ExpenseFacade(ExpenseService expenseService) { + this.expenseService = expenseService; + } + + public void setExpensePaid(Expense expense, TreeGrid grid) { + expense.setIsPaid(true); + this.expenseService.update(expense); + Notification.show("Expense " + expense.getName() + " set as paid" ); + grid.select(null); + grid.getDataProvider().refreshAll(); + } +} \ No newline at end of file diff --git a/src/main/java/com/application/munera/facades/PersonFacade.java b/src/main/java/com/application/munera/facades/PersonFacade.java new file mode 100644 index 0000000..aabf821 --- /dev/null +++ b/src/main/java/com/application/munera/facades/PersonFacade.java @@ -0,0 +1,54 @@ +package com.application.munera.facades; + +import com.application.munera.data.Expense; +import com.application.munera.data.Person; +import com.application.munera.services.ExpenseService; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.notification.NotificationVariant; +import com.vaadin.flow.component.treegrid.TreeGrid; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class PersonFacade { + + private final ExpenseService expenseService; + + public PersonFacade(ExpenseService expenseService) { + this.expenseService = expenseService; + } + public void setDebtPaid(Person person, TreeGrid grid) { + try { + List expenses = expenseService.findExpensesWherePayer(person).stream().toList(); + for (Expense expense : expenses) { + expense.setIsPaid(true); + expenseService.update(expense); + } + Notification.show("All expenses marked as paid for " + person.getFirstName() + " " + person.getLastName()); + grid.select(null); + grid.getDataProvider().refreshAll(); + } catch (Exception e) { + Notification n = Notification.show("Error marking expenses as paid: " + e.getMessage()); + n.setPosition(Notification.Position.MIDDLE); + n.addThemeVariants(NotificationVariant.LUMO_ERROR); + } + } + + public void setCreditPaid(Person person, TreeGrid grid) { + try { + List expenses = expenseService.findExpensesWhereBeneficiary(person).stream().toList(); + for (Expense expense : expenses) { + expense.setIsPaid(true); + expenseService.update(expense); + } + Notification.show("All expenses marked as paid for " + person.getFirstName() + " " + person.getLastName()); + grid.select(null); + grid.getDataProvider().refreshAll(); + } catch (Exception e) { + Notification n = Notification.show("Error marking expenses as paid: " + e.getMessage()); + n.setPosition(Notification.Position.MIDDLE); + n.addThemeVariants(NotificationVariant.LUMO_ERROR); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/application/munera/views/people/PeopleView.java b/src/main/java/com/application/munera/views/people/PeopleView.java index 88c32d2..21a46e5 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -2,6 +2,8 @@ package com.application.munera.views.people; import com.application.munera.data.Expense; import com.application.munera.data.Person; +import com.application.munera.facades.ExpenseFacade; +import com.application.munera.facades.PersonFacade; import com.application.munera.services.ExpenseService; import com.application.munera.services.PersonService; import com.application.munera.services.ViewsService; @@ -55,16 +57,20 @@ public class PeopleView extends Div implements BeforeEnterObserver { private Person person; private final PersonService personService; + private final PersonFacade personFacade; + private final ExpenseFacade expenseFacade; private final ExpenseService expenseService; private final ViewsService viewsService; private TextField firstName; private TextField lastName; private EmailField email; - public PeopleView(PersonService personService, ExpenseService expenseService, ViewsService viewsService) { + public PeopleView(PersonService personService, ExpenseService expenseService, ViewsService viewsService, PersonFacade personFacade, ExpenseFacade expenseFacade) { this.personService = personService; this.expenseService = expenseService; this.viewsService = viewsService; + this.personFacade = personFacade; + this.expenseFacade = expenseFacade; addClassNames("expenses-view"); // Create UI @@ -85,11 +91,11 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addColumn(new ComponentRenderer<>(persona -> { if (persona instanceof Person) { - Button setDebtPaidButton = new Button("Set all debt as paid", event -> setDebtPaid((Person) persona)); + Button setDebtPaidButton = new Button("Set all debt as paid", event -> this.personFacade.setDebtPaid((Person) persona, grid)); setDebtPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY); return setDebtPaidButton; } else if (persona instanceof Expense) { - Button setExpensePaidButton = new Button("Set as paid", event -> setExpensePaid((Expense) persona)); + Button setExpensePaidButton = new Button("Set as paid", event -> this.expenseFacade.setExpensePaid((Expense) persona, grid)); setExpensePaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL); return setExpensePaidButton; } else return new Span(); @@ -97,7 +103,7 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addColumn(new ComponentRenderer<>(persona -> { if (persona instanceof Person) { - Button setCreditPaidButton = new Button("Set all credit as paid", event -> setCreditPaid((Person) persona)); + Button setCreditPaidButton = new Button("Set all credit as paid", event -> this.personFacade.setCreditPaid((Person) persona, grid)); setCreditPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY); return setCreditPaidButton; } else return new Span(); @@ -264,46 +270,4 @@ public class PeopleView extends Div implements BeforeEnterObserver { for (Expense expense : expenses) grid.getTreeData().addItem(user, expense); } } - - //TODO: this needs to be in the person service? - private void setDebtPaid(Person person) { - try { - List expenses = expenseService.findExpensesWherePayer(person).stream().toList(); - for (Expense expense : expenses) { - expense.setIsPaid(true); - expenseService.update(expense); - } - Notification.show("All expenses marked as paid for " + person.getFirstName() + " " + person.getLastName()); - refreshGrid(); - } catch (Exception e) { - Notification n = Notification.show("Error marking expenses as paid: " + e.getMessage()); - n.setPosition(Position.MIDDLE); - n.addThemeVariants(NotificationVariant.LUMO_ERROR); - } - } - - //TODO: this needs to be in the expense service? - private void setExpensePaid(Expense expense) { - expense.setIsPaid(true); - this.expenseService.update(expense); - Notification.show("Expense " + expense.getName() + " set as paid" ); - refreshGrid(); - } - - //TODO: this needs to be in the person service? - private void setCreditPaid(Person person) { - try { - List expenses = expenseService.findExpensesWhereBeneficiary(person).stream().toList(); - for (Expense expense : expenses) { - expense.setIsPaid(true); - expenseService.update(expense); - } - Notification.show("All expenses marked as paid for " + person.getFirstName() + " " + person.getLastName()); - refreshGrid(); - } catch (Exception e) { - Notification n = Notification.show("Error marking expenses as paid: " + e.getMessage()); - n.setPosition(Position.MIDDLE); - n.addThemeVariants(NotificationVariant.LUMO_ERROR); - } - } } \ No newline at end of file