feat: debt by user calculations

This commit is contained in:
Filippo Ferrari 2024-06-04 13:12:34 +01:00
parent 16fd808f25
commit ba686ccdd0
5 changed files with 36 additions and 7 deletions

View file

@ -6,6 +6,7 @@ import jakarta.validation.constraints.Size;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.math.BigDecimal;
import java.util.Set; import java.util.Set;
@Entity @Entity
@ -32,6 +33,12 @@ public class Person {
@Column(name = "email") @Column(name = "email")
private String email; private String email;
@Column(name = "debt")
private BigDecimal debt;
@Column(name = "credit")
private BigDecimal credit;
// @JsonIgnore // @JsonIgnore
@ManyToMany(mappedBy = "creditors") @ManyToMany(mappedBy = "creditors")
private Set<Expense> creditorExpenses; private Set<Expense> creditorExpenses;

View file

@ -2,12 +2,20 @@ package com.application.munera.repositories;
import com.application.munera.data.Expense; 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.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface ExpenseRepository import java.util.Collection;
extends import java.util.Set;
JpaRepository<Expense, Long>,
JpaSpecificationExecutor<Expense> {
} public interface ExpenseRepository extends JpaRepository<Expense, Long>, JpaSpecificationExecutor<Expense> {
@Query("SELECT e FROM Expense e JOIN e.creditors c WHERE c.id = :personId")
Set<Expense> findCreditorsExpensesByPersonId(@Param("personId") Long personId);
@Query("SELECT e FROM Expense e JOIN e.debtors d WHERE d.id = :personId")
Set<Expense> findDebtorsExpensesByPersonId(@Param("personId") Long personId);
}

View file

@ -1,12 +1,14 @@
package com.application.munera.services; 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.repositories.ExpenseRepository; import com.application.munera.repositories.ExpenseRepository;
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;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -23,6 +25,10 @@ public class ExpenseService {
return repository.findById(id); return repository.findById(id);
} }
public Collection<Expense> findDebtByUser(final Person person) {
return repository.findDebtorsExpensesByPersonId(person.getId());
}
public List<Expense> findAll() {return repository.findAll();} public List<Expense> findAll() {return repository.findAll();}
public Expense update(Expense entity) { public Expense update(Expense entity) {

View file

@ -1,5 +1,6 @@
package com.application.munera.services; package com.application.munera.services;
import com.application.munera.data.Expense;
import com.application.munera.data.Person; import com.application.munera.data.Person;
import com.application.munera.repositories.PersonRepository; import com.application.munera.repositories.PersonRepository;
import org.springframework.data.domain.Page; 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.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -14,9 +16,11 @@ import java.util.Optional;
public class PersonService { public class PersonService {
private final PersonRepository personRepository; private final PersonRepository personRepository;
private final ExpenseService expenseService;
public PersonService(PersonRepository personRepository) { public PersonService(PersonRepository personRepository, ExpenseService expenseService) {
this.personRepository = personRepository; this.personRepository = personRepository;
this.expenseService = expenseService;
} }
public Optional<Person> get(Long id) { public Optional<Person> get(Long id) {
@ -46,4 +50,8 @@ public class PersonService {
public int count() { public int count() {
return (int) this.personRepository.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);
}
} }

View file

@ -69,8 +69,8 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.addColumn(Person::getFirstName).setHeader("First Name").setSortable(true); grid.addColumn(Person::getFirstName).setHeader("First Name").setSortable(true);
grid.addColumn(Person::getLastName).setHeader("Last Name").setSortable(true); grid.addColumn(Person::getLastName).setHeader("Last Name").setSortable(true);
grid.addColumn(Person::getEmail).setHeader("Email").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.getColumns().forEach(col -> col.setAutoWidth(true));
grid.setItems(query -> personService.list( grid.setItems(query -> personService.list(
PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query))) PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))
.stream()); .stream());