diff --git a/src/main/java/com/application/munera/facades/PersonFacade.java b/src/main/java/com/application/munera/facades/PersonFacade.java index c946d80..b3bc7dd 100644 --- a/src/main/java/com/application/munera/facades/PersonFacade.java +++ b/src/main/java/com/application/munera/facades/PersonFacade.java @@ -2,22 +2,70 @@ package com.application.munera.facades; import com.application.munera.data.Expense; import com.application.munera.data.Person; +import com.application.munera.data.User; import com.application.munera.services.ExpenseService; +import com.application.munera.services.PersonService; +import com.application.munera.services.UserService; 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.math.BigDecimal; import java.util.List; +import java.util.Objects; @Component public class PersonFacade { private final ExpenseService expenseService; + private final UserService userService; + private final PersonService personService; - public PersonFacade(ExpenseService expenseService) { + public PersonFacade(ExpenseService expenseService, UserService userService, PersonService personService) { this.expenseService = expenseService; + this.userService = userService; + this.personService = personService; } + + /** + * Fetches the {@code Person} entity associated with the currently logged-in user. + * + * @return the {@code Person} entity of the logged-in user, or {@code null} if not found + */ + public Person getLoggedInPerson() { + final var user = userService.getLoggedInUser(); + return Objects.requireNonNull(personService.findByUsername(user.getUsername())); + } + + /** + * Finds all {@code Person} entities associated with the logged-in user, excluding the logged-in user. + * + * @param user the logged-in user whose associated persons are to be retrieved + * @return a {@code List} of {@code Person} entities excluding the logged-in user + */ + public List findAllExcludeLoggedUser(final User user) { + return this.personService.findAllExcludeLoggedUser(user); + } + + /** + * Calculates the net balance for a given {@code Person}. + * + * @param person the {@code Person} for whom the net balance is to be calculated + * @return the net balance as a {@code BigDecimal} + */ + public BigDecimal calculateNetBalance(final Person person) { + return this.personService.calculateNetBalance(person); + } + + /** + * Marks all expenses as paid for the given {@code Person} where the person is the payer. + * Updates the user interface to reflect the changes and provides notifications for success or failure. + * + * @param person the {@code Person} whose expenses are to be marked as paid + * @param grid the {@code TreeGrid} component to refresh after updating expenses + * @param userId the ID of the user performing the update + */ public void setDebtPaid(Person person, TreeGrid grid, Long userId) { try { List expenses = expenseService.findExpensesWherePayer(person).stream().toList(); @@ -35,6 +83,14 @@ public class PersonFacade { } } + /** + * Marks all expenses as paid for the given {@code Person} where the person is the beneficiary. + * Updates the user interface to reflect the changes and provides notifications for success or failure. + * + * @param person the {@code Person} whose expenses are to be marked as paid + * @param grid the {@code TreeGrid} component to refresh after updating expenses + * @param userId the ID of the user performing the update + */ public void setCreditPaid(Person person, TreeGrid grid, Long userId) { try { List expenses = expenseService.findExpensesWhereBeneficiary(person).stream().toList(); diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index 7a49eef..00d2591 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -3,7 +3,9 @@ package com.application.munera.services; import com.application.munera.data.Expense; import com.application.munera.data.Person; import com.application.munera.data.User; +import com.application.munera.repositories.ExpenseRepository; import com.application.munera.repositories.PersonRepository; +import com.application.munera.repositories.UserRepository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; @@ -17,14 +19,14 @@ import java.util.Optional; @Service public class PersonService { - private final UserService userService; private final PersonRepository personRepository; - private final ExpenseService expenseService; + private final UserRepository userRepository; + private final ExpenseRepository expenseRepository; - public PersonService(PersonRepository personRepository, ExpenseService expenseService, UserService userService) { + public PersonService(PersonRepository personRepository, ExpenseRepository expenseRepository, UserRepository userRepository) { this.personRepository = personRepository; - this.expenseService = expenseService; - this.userService = userService; + this.userRepository = userRepository; + this.expenseRepository = expenseRepository; } /** @@ -85,15 +87,6 @@ public class PersonService { return (int) this.personRepository.count(); } - /** - * Fetches the Person entity connected to the currently logged-in user. - * - * @return Person entity of the logged-in user, or null if not found. - */ - public Person getLoggedInPerson() { - final var user = userService.getLoggedInUser(); - return Objects.requireNonNull(personRepository.findByUsername(user.getUsername())); - } /** * Updates a person in the repository. @@ -118,7 +111,7 @@ public class PersonService { * @return the total debt amount */ public BigDecimal calculateDebt(final Person person) { - return this.expenseService.findExpensesWherePayer(person).stream() + return this.expenseRepository.findExpensesByPayer(person.getId()).stream() .filter(expense -> !expense.getBeneficiary().equals(person) && Boolean.FALSE.equals(expense.getIsPaid())) .map(Expense::getCost) .reduce(BigDecimal.ZERO, BigDecimal::add); @@ -130,7 +123,7 @@ public class PersonService { * @return the total credit amount */ public BigDecimal calculateCredit(final Person person) { - return this.expenseService.findExpensesWhereBeneficiary(person).stream() + return this.expenseRepository.findExpensesByBeneficiary(person.getId()).stream() .filter(expense -> !expense.getPayer().equals(person) && Boolean.FALSE.equals(expense.getIsPaid())) .map(Expense::getCost) .reduce(BigDecimal.ZERO, BigDecimal::add);