From 8fe920138e00a8a06e84d0622a18f8680a590b91 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Tue, 10 Sep 2024 19:14:41 +0200 Subject: [PATCH] refactor: changes --- .../application/munera/services/UserService.java | 7 ++++--- .../munera/views/expenses/ExpensesView.java | 1 - .../munera/views/settings/SettingsView.java | 14 ++++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java index 6ebb6e8..f1ab7f3 100644 --- a/src/main/java/com/application/munera/services/UserService.java +++ b/src/main/java/com/application/munera/services/UserService.java @@ -4,6 +4,7 @@ import com.application.munera.data.Person; import com.application.munera.data.User; import com.application.munera.repositories.PersonRepository; import com.application.munera.repositories.UserRepository; +import jakarta.transaction.Transactional; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; @@ -44,12 +45,14 @@ public class UserService { * Updates the user's data and its connected person entity * @param user the user of which we update the data */ - public void updateUser(User user) { + @Transactional + public void updateUserAndConnectedPerson(User user) { userRepository.save(user); final var person = personRepository.findByUserId(user.getId()) .orElseThrow(() -> new IllegalStateException("Associated Person not found")); person.setFirstName(user.getFirstName()); person.setLastName(user.getLastName()); + person.setEmail(user.getEmail()); personRepository.save(person); } @@ -58,8 +61,6 @@ public class UserService { * @param user the user of which we update the data */ public void saveUserAndConnectedPerson(User user) { - //TODO: look if this method can substitute the one above: updateUser, they seem to do similar things - // Check if the user already exists in the database final var existingUserOptional = userRepository.findByUsername(user.getUsername()); diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index d3c1b1c..b8cbcef 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -120,7 +120,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver { // Configure Form binder = new BeanValidationBinder<>(Expense.class); - // Bind fields. This is where you'd define e.g. validation rules binder.bindInstanceFields(this); binder.forField(name) diff --git a/src/main/java/com/application/munera/views/settings/SettingsView.java b/src/main/java/com/application/munera/views/settings/SettingsView.java index d27096b..063cc1c 100644 --- a/src/main/java/com/application/munera/views/settings/SettingsView.java +++ b/src/main/java/com/application/munera/views/settings/SettingsView.java @@ -8,6 +8,7 @@ import com.vaadin.flow.component.formlayout.FormLayout; import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.EmailField; import com.vaadin.flow.component.textfield.PasswordField; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.router.BeforeEnterEvent; @@ -30,6 +31,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver private TextField lastNameField; private PasswordField passwordField; private TextField monthlyIncomeField; + private EmailField emailField; @Autowired public SettingsView(UserService userService) { @@ -44,9 +46,10 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver firstNameField = new TextField("First Name"); lastNameField = new TextField("Last Name"); passwordField = new PasswordField("Password"); + emailField = new EmailField("Email"); monthlyIncomeField = new TextField("Monthly Income"); - formLayout.add(firstNameField, lastNameField, passwordField, monthlyIncomeField); + formLayout.add(firstNameField, lastNameField, passwordField, emailField, monthlyIncomeField); Button saveButton = new Button("Save", click -> saveUserData()); @@ -58,18 +61,17 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver loggedInUser.setFirstName(firstNameField.getValue()); loggedInUser.setLastName(lastNameField.getValue()); + loggedInUser.setEmail(emailField.getValue()); // Only update the password if it's not empty if (!passwordField.isEmpty()) { loggedInUser.setPassword(passwordField.getValue()); } - // Handle saving the monthly income separately if needed - // For now, we'll just print it out + // TODO: implement String monthlyIncome = monthlyIncomeField.getValue(); - System.out.println("Monthly Income: " + monthlyIncome); - userService.updateUser(loggedInUser); + userService.updateUserAndConnectedPerson(loggedInUser); Notification.show("User details updated successfully"); } @@ -78,6 +80,6 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found")); firstNameField.setValue(loggedInUser.getFirstName()); lastNameField.setValue(loggedInUser.getLastName()); - monthlyIncomeField.setValue(""); //TODO: implement monthly income + monthlyIncomeField.setValue(""); } }