From 97439065bc0b6f30ce64f4d03cc1b94ac0503e8d Mon Sep 17 00:00:00 2001 From: Filippo Ferrari Date: Tue, 4 Jun 2024 13:35:16 +0100 Subject: [PATCH] feat: credit by user calculations --- .../java/com/application/munera/services/ExpenseService.java | 3 +++ .../java/com/application/munera/services/PersonService.java | 4 ++++ .../com/application/munera/views/expenses/PeopleView.java | 1 + 3 files changed, 8 insertions(+) diff --git a/src/main/java/com/application/munera/services/ExpenseService.java b/src/main/java/com/application/munera/services/ExpenseService.java index da4c036..d63db89 100644 --- a/src/main/java/com/application/munera/services/ExpenseService.java +++ b/src/main/java/com/application/munera/services/ExpenseService.java @@ -29,6 +29,9 @@ public class ExpenseService { return repository.findDebtorsExpensesByPersonId(person.getId()); } + public Collection findCreditByUser(final Person person) { + return repository.findCreditorsExpensesByPersonId(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 5893925..097840a 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -54,4 +54,8 @@ public class PersonService { public BigDecimal calculateDebt(final Person person){ return this.expenseService.findDebtByUser(person).stream().map(Expense::getCost).reduce(BigDecimal.ZERO, BigDecimal::add); } + + public BigDecimal calculateCredit(final Person person) { + return this.expenseService.findCreditByUser(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 2234958..9ca9255 100644 --- a/src/main/java/com/application/munera/views/expenses/PeopleView.java +++ b/src/main/java/com/application/munera/views/expenses/PeopleView.java @@ -70,6 +70,7 @@ public class PeopleView extends Div implements BeforeEnterObserver { 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.addColumn(personService::calculateCredit).setHeader("Credit").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.setItems(query -> personService.list( PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))