refactor: changes

This commit is contained in:
filippo-ferrari 2024-09-10 19:14:41 +02:00
parent 11ab5e4b55
commit 8fe920138e
3 changed files with 12 additions and 10 deletions

View file

@ -4,6 +4,7 @@ import com.application.munera.data.Person;
import com.application.munera.data.User; import com.application.munera.data.User;
import com.application.munera.repositories.PersonRepository; import com.application.munera.repositories.PersonRepository;
import com.application.munera.repositories.UserRepository; import com.application.munera.repositories.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -44,12 +45,14 @@ public class UserService {
* Updates the user's data and its connected person entity * Updates the user's data and its connected person entity
* @param user the user of which we update the data * @param user the user of which we update the data
*/ */
public void updateUser(User user) { @Transactional
public void updateUserAndConnectedPerson(User user) {
userRepository.save(user); userRepository.save(user);
final var person = personRepository.findByUserId(user.getId()) final var person = personRepository.findByUserId(user.getId())
.orElseThrow(() -> new IllegalStateException("Associated Person not found")); .orElseThrow(() -> new IllegalStateException("Associated Person not found"));
person.setFirstName(user.getFirstName()); person.setFirstName(user.getFirstName());
person.setLastName(user.getLastName()); person.setLastName(user.getLastName());
person.setEmail(user.getEmail());
personRepository.save(person); personRepository.save(person);
} }
@ -58,8 +61,6 @@ public class UserService {
* @param user the user of which we update the data * @param user the user of which we update the data
*/ */
public void saveUserAndConnectedPerson(User user) { 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 // Check if the user already exists in the database
final var existingUserOptional = userRepository.findByUsername(user.getUsername()); final var existingUserOptional = userRepository.findByUsername(user.getUsername());

View file

@ -120,7 +120,6 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
// Configure Form // Configure Form
binder = new BeanValidationBinder<>(Expense.class); binder = new BeanValidationBinder<>(Expense.class);
// Bind fields. This is where you'd define e.g. validation rules // Bind fields. This is where you'd define e.g. validation rules
binder.bindInstanceFields(this); binder.bindInstanceFields(this);
binder.forField(name) binder.forField(name)

View file

@ -8,6 +8,7 @@ import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout; 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.PasswordField;
import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.BeforeEnterEvent; import com.vaadin.flow.router.BeforeEnterEvent;
@ -30,6 +31,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
private TextField lastNameField; private TextField lastNameField;
private PasswordField passwordField; private PasswordField passwordField;
private TextField monthlyIncomeField; private TextField monthlyIncomeField;
private EmailField emailField;
@Autowired @Autowired
public SettingsView(UserService userService) { public SettingsView(UserService userService) {
@ -44,9 +46,10 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
firstNameField = new TextField("First Name"); firstNameField = new TextField("First Name");
lastNameField = new TextField("Last Name"); lastNameField = new TextField("Last Name");
passwordField = new PasswordField("Password"); passwordField = new PasswordField("Password");
emailField = new EmailField("Email");
monthlyIncomeField = new TextField("Monthly Income"); 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()); Button saveButton = new Button("Save", click -> saveUserData());
@ -58,18 +61,17 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
loggedInUser.setFirstName(firstNameField.getValue()); loggedInUser.setFirstName(firstNameField.getValue());
loggedInUser.setLastName(lastNameField.getValue()); loggedInUser.setLastName(lastNameField.getValue());
loggedInUser.setEmail(emailField.getValue());
// Only update the password if it's not empty // Only update the password if it's not empty
if (!passwordField.isEmpty()) { if (!passwordField.isEmpty()) {
loggedInUser.setPassword(passwordField.getValue()); loggedInUser.setPassword(passwordField.getValue());
} }
// Handle saving the monthly income separately if needed // TODO: implement
// For now, we'll just print it out
String monthlyIncome = monthlyIncomeField.getValue(); String monthlyIncome = monthlyIncomeField.getValue();
System.out.println("Monthly Income: " + monthlyIncome);
userService.updateUser(loggedInUser); userService.updateUserAndConnectedPerson(loggedInUser);
Notification.show("User details updated successfully"); 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")); final var loggedInUser = userService.getLoggedInUser().orElseThrow(() -> new UsernameNotFoundException("User not found"));
firstNameField.setValue(loggedInUser.getFirstName()); firstNameField.setValue(loggedInUser.getFirstName());
lastNameField.setValue(loggedInUser.getLastName()); lastNameField.setValue(loggedInUser.getLastName());
monthlyIncomeField.setValue(""); //TODO: implement monthly income monthlyIncomeField.setValue("");
} }
} }