diff --git a/src/main/java/com/application/munera/data/Person.java b/src/main/java/com/application/munera/data/Person.java index 42cf0bc..1681c06 100644 --- a/src/main/java/com/application/munera/data/Person.java +++ b/src/main/java/com/application/munera/data/Person.java @@ -6,6 +6,7 @@ import jakarta.validation.constraints.Size; import lombok.Getter; import lombok.Setter; +import java.math.BigDecimal; import java.util.Set; @Entity @@ -32,6 +33,12 @@ public class Person { @Column(name = "email") private String email; + @Column(name = "debt") + private BigDecimal debt; + + @Column(name = "credit") + private BigDecimal credit; + // @JsonIgnore @ManyToMany(mappedBy = "creditors") private Set creditorExpenses; diff --git a/src/main/java/com/application/munera/repositories/ExpenseRepository.java b/src/main/java/com/application/munera/repositories/ExpenseRepository.java index 12391c1..a227c3a 100644 --- a/src/main/java/com/application/munera/repositories/ExpenseRepository.java +++ b/src/main/java/com/application/munera/repositories/ExpenseRepository.java @@ -2,12 +2,20 @@ package com.application.munera.repositories; import com.application.munera.data.Expense; +import com.application.munera.data.Person; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; -public interface ExpenseRepository - extends - JpaRepository, - JpaSpecificationExecutor { +import java.util.Collection; +import java.util.Set; -} +public interface ExpenseRepository extends JpaRepository, JpaSpecificationExecutor { + + @Query("SELECT e FROM Expense e JOIN e.creditors c WHERE c.id = :personId") + Set findCreditorsExpensesByPersonId(@Param("personId") Long personId); + + @Query("SELECT e FROM Expense e JOIN e.debtors d WHERE d.id = :personId") + Set findDebtorsExpensesByPersonId(@Param("personId") Long personId); +} \ No newline at end of file diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index 6defec3..da4c036 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -1,12 +1,14 @@ package com.application.munera.services; import com.application.munera.data.Expense; +import com.application.munera.data.Person; import com.application.munera.repositories.ExpenseRepository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; +import java.util.Collection; import java.util.List; import java.util.Optional; @@ -23,6 +25,10 @@ public class ExpenseService { return repository.findById(id); } + public Collection findDebtByUser(final Person person) { + return repository.findDebtorsExpensesByPersonId(person.getId()); + } + public List findAll() {return repository.findAll();} public Expense update(Expense entity) { diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index 76031ea..5893925 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -1,5 +1,6 @@ package com.application.munera.services; +import com.application.munera.data.Expense; import com.application.munera.data.Person; import com.application.munera.repositories.PersonRepository; import org.springframework.data.domain.Page; @@ -7,6 +8,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; +import java.math.BigDecimal; import java.util.List; import java.util.Optional; @@ -14,9 +16,11 @@ import java.util.Optional; public class PersonService { private final PersonRepository personRepository; + private final ExpenseService expenseService; - public PersonService(PersonRepository personRepository) { + public PersonService(PersonRepository personRepository, ExpenseService expenseService) { this.personRepository = personRepository; + this.expenseService = expenseService; } public Optional get(Long id) { @@ -46,4 +50,8 @@ public class PersonService { public int count() { return (int) this.personRepository.count(); } + + public BigDecimal calculateDebt(final Person person){ + return this.expenseService.findDebtByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add); + } } diff --git a/src/main/java/com/application/munera/views/expenses/PeopleView.java b/src/main/java/com/application/munera/views/expenses/PeopleView.java index 3d34587..2234958 100644 --- a/src/main/java/com/application/munera/views/expenses/PeopleView.java +++ b/src/main/java/com/application/munera/views/expenses/PeopleView.java @@ -69,8 +69,8 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addColumn(Person::getFirstName).setHeader("First Name").setSortable(true); grid.addColumn(Person::getLastName).setHeader("Last Name").setSortable(true); grid.addColumn(Person::getEmail).setHeader("Email").setSortable(true); + grid.addColumn(personService::calculateDebt).setHeader("Debt").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); - grid.setItems(query -> personService.list( PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query))) .stream());