feat: debt by user calculations
This commit is contained in:
parent
16fd808f25
commit
ba686ccdd0
5 changed files with 36 additions and 7 deletions
|
@ -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<Expense> creditorExpenses;
|
||||
|
|
|
@ -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<Expense, Long>,
|
||||
JpaSpecificationExecutor<Expense> {
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
}
|
||||
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);
|
||||
}
|
|
@ -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<Expense> findDebtByUser(final Person person) {
|
||||
return repository.findDebtorsExpensesByPersonId(person.getId());
|
||||
}
|
||||
|
||||
public List<Expense> findAll() {return repository.findAll();}
|
||||
|
||||
public Expense update(Expense entity) {
|
||||
|
|
|
@ -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<Person> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue