doc: PersonFacade javadocs

This commit is contained in:
effe 2024-09-14 13:42:25 -04:00
parent 9f77593498
commit f5f764ee5b
2 changed files with 66 additions and 17 deletions

View file

@ -2,22 +2,70 @@ package com.application.munera.facades;
import com.application.munera.data.Expense; import com.application.munera.data.Expense;
import com.application.munera.data.Person; import com.application.munera.data.Person;
import com.application.munera.data.User;
import com.application.munera.services.ExpenseService; 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.Notification;
import com.vaadin.flow.component.notification.NotificationVariant; import com.vaadin.flow.component.notification.NotificationVariant;
import com.vaadin.flow.component.treegrid.TreeGrid; import com.vaadin.flow.component.treegrid.TreeGrid;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Objects;
@Component @Component
public class PersonFacade { public class PersonFacade {
private final ExpenseService expenseService; 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.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<Person> 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<Object> grid, Long userId) { public void setDebtPaid(Person person, TreeGrid<Object> grid, Long userId) {
try { try {
List<Expense> expenses = expenseService.findExpensesWherePayer(person).stream().toList(); List<Expense> 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<Object> grid, Long userId) { public void setCreditPaid(Person person, TreeGrid<Object> grid, Long userId) {
try { try {
List<Expense> expenses = expenseService.findExpensesWhereBeneficiary(person).stream().toList(); List<Expense> expenses = expenseService.findExpensesWhereBeneficiary(person).stream().toList();

View file

@ -3,7 +3,9 @@ package com.application.munera.services;
import com.application.munera.data.Expense; import com.application.munera.data.Expense;
import com.application.munera.data.Person; import com.application.munera.data.Person;
import com.application.munera.data.User; import com.application.munera.data.User;
import com.application.munera.repositories.ExpenseRepository;
import com.application.munera.repositories.PersonRepository; import com.application.munera.repositories.PersonRepository;
import com.application.munera.repositories.UserRepository;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
@ -17,14 +19,14 @@ import java.util.Optional;
@Service @Service
public class PersonService { public class PersonService {
private final UserService userService;
private final PersonRepository personRepository; 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.personRepository = personRepository;
this.expenseService = expenseService; this.userRepository = userRepository;
this.userService = userService; this.expenseRepository = expenseRepository;
} }
/** /**
@ -85,15 +87,6 @@ public class PersonService {
return (int) this.personRepository.count(); 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. * Updates a person in the repository.
@ -118,7 +111,7 @@ public class PersonService {
* @return the total debt amount * @return the total debt amount
*/ */
public BigDecimal calculateDebt(final Person person) { 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())) .filter(expense -> !expense.getBeneficiary().equals(person) && Boolean.FALSE.equals(expense.getIsPaid()))
.map(Expense::getCost) .map(Expense::getCost)
.reduce(BigDecimal.ZERO, BigDecimal::add); .reduce(BigDecimal.ZERO, BigDecimal::add);
@ -130,7 +123,7 @@ public class PersonService {
* @return the total credit amount * @return the total credit amount
*/ */
public BigDecimal calculateCredit(final Person person) { 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())) .filter(expense -> !expense.getPayer().equals(person) && Boolean.FALSE.equals(expense.getIsPaid()))
.map(Expense::getCost) .map(Expense::getCost)
.reduce(BigDecimal.ZERO, BigDecimal::add); .reduce(BigDecimal.ZERO, BigDecimal::add);