refactor: ExpensesView

This commit is contained in:
effe 2024-09-14 16:15:31 -04:00
parent d67359ff11
commit 7af47b86a5
3 changed files with 27 additions and 23 deletions

View file

@ -39,6 +39,15 @@ public class PersonFacade {
return Objects.requireNonNull(personService.findByUsername(user.getUsername())); return Objects.requireNonNull(personService.findByUsername(user.getUsername()));
} }
/**
* Fetches all the people related to the user
* @param userId the id of the user related to the people
* @return the list of people found related to the id of the user
*/
public List<Person> findAllByUserId(Long userId) {
return this.personService.findAllByUserId(userId);
}
/** /**
* Finds a {@link Person} entity by its ID. * Finds a {@link Person} entity by its ID.
* *

View file

@ -35,7 +35,7 @@ public class PersonService {
} }
/** /**
* Finds all persons. * Finds all people.
* @return a collection of all persons * @return a collection of all persons
*/ */
public List<Person> findAllByUserId(Long userId) { public List<Person> findAllByUserId(Long userId) {

View file

@ -1,8 +1,14 @@
package com.application.munera.views.expenses; package com.application.munera.views.expenses;
import com.application.munera.data.*; import com.application.munera.data.Category;
import com.application.munera.security.SecurityUtils; import com.application.munera.data.Expense;
import com.application.munera.services.*; import com.application.munera.data.PeriodUnit;
import com.application.munera.data.Person;
import com.application.munera.facades.PersonFacade;
import com.application.munera.services.CategoryService;
import com.application.munera.services.ExpenseService;
import com.application.munera.services.UserService;
import com.application.munera.services.ViewsService;
import com.application.munera.views.MainLayout; import com.application.munera.views.MainLayout;
import com.vaadin.flow.component.UI; import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
@ -30,8 +36,6 @@ import com.vaadin.flow.router.*;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.vaadin.klaudeta.PaginatedGrid; import org.vaadin.klaudeta.PaginatedGrid;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -59,8 +63,8 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
private Long userId; private Long userId;
private final ExpenseService expenseService; private final ExpenseService expenseService;
private final PersonFacade personFacade;
private final CategoryService categoryService; private final CategoryService categoryService;
private final PersonService personService;
private final ViewsService viewsService; private final ViewsService viewsService;
private final UserService userService; private final UserService userService;
private TextField name; private TextField name;
@ -76,13 +80,13 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
private ComboBox<Person> beneficiary; private ComboBox<Person> beneficiary;
@Autowired @Autowired
public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, ViewsService viewsService, UserService userService) { public ExpensesView(ExpenseService expenseService, CategoryService categoryService, ViewsService viewsService, UserService userService, PersonFacade personFacade) {
this.expenseService = expenseService; this.expenseService = expenseService;
this.categoryService = categoryService; this.categoryService = categoryService;
this.personService = personService;
this.viewsService = viewsService; this.viewsService = viewsService;
this.userService = userService; this.userService = userService;
this.userId = userService.getLoggedInUser().getId(); this.personFacade = personFacade;
this.userId = this.userService.getLoggedInUser().getId();
addClassNames("expenses-view"); addClassNames("expenses-view");
// Create UI // Create UI
@ -242,7 +246,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
Div editorDiv = new Div(); Div editorDiv = new Div();
editorDiv.setClassName("editor"); editorDiv.setClassName("editor");
editorLayoutDiv.add(editorDiv); editorLayoutDiv.add(editorDiv);
final var people = this.personService.findAllByUserId(userId); final var people = this.personFacade.findAllByUserId(userId);
FormLayout formLayout = new FormLayout(); FormLayout formLayout = new FormLayout();
name = new TextField("Name"); name = new TextField("Name");
@ -310,18 +314,9 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
periodInterval.setVisible(isPeriodicChecked); periodInterval.setVisible(isPeriodicChecked);
} }
//TODO: check and improve this mess pls
private void initializeComboBoxes() { private void initializeComboBoxes() {
// Fetch the logged-in user's Person entity final var loggedInPerson = this.personFacade.getLoggedInPerson();
UserDetails userDetails = SecurityUtils.getLoggedInUserDetails();
if (userDetails != null) {
String username = userDetails.getUsername();
final var user = this.userService.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("User not found"));
final var loggedInPerson = personService.findByUsername(user.getUsername());
// Set default values for payer and beneficiary ComboBoxes
payer.setValue(loggedInPerson); payer.setValue(loggedInPerson);
beneficiary.setValue(loggedInPerson); beneficiary.setValue(loggedInPerson);
}
} }
} }