diff --git a/src/main/java/com/application/munera/initializers/UserInitializer.java b/src/main/java/com/application/munera/initializers/UserInitializer.java index 019b016..735e8bc 100644 --- a/src/main/java/com/application/munera/initializers/UserInitializer.java +++ b/src/main/java/com/application/munera/initializers/UserInitializer.java @@ -26,7 +26,7 @@ public class UserInitializer { adminUser.setLastName(adminProperties.getLastName()); adminUser.setRoles(adminProperties.getRoles()); adminUser.setEmail(adminProperties.getEmail()); - userService.saveUserAndConnectedPerson(adminUser); + userService.saveOrUpdateUserAndConnectedPerson(adminUser); } } } diff --git a/src/main/java/com/application/munera/services/UserService.java b/src/main/java/com/application/munera/services/UserService.java index c37cfff..a313ab2 100644 --- a/src/main/java/com/application/munera/services/UserService.java +++ b/src/main/java/com/application/munera/services/UserService.java @@ -50,49 +50,56 @@ public class UserService { return null; } - /** - * Updates the user's data and its connected person entity - * @param user the user of which we update the data - */ @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); - } - - /** - * Saves a user and connected person entity - * @param user the user of which we update the data - */ - public void saveUserAndConnectedPerson(User user) { + public void saveOrUpdateUserAndConnectedPerson(User user) { // Check if the user already exists in the database final var existingUserOptional = userRepository.findByUsername(user.getUsername()); + User userToSave = getUser(user, existingUserOptional); - User existingUser; - - // Save the new user entity if he doesn't exist yet - existingUser = existingUserOptional.orElseGet(() -> userRepository.save(user)); + // Save the user entity + userRepository.save(userToSave); // Check if the associated person exists for the user - final var existingPerson = personRepository.findByUserId(existingUser.getId()); + final var existingPersonOptional = personRepository.findByUserId(userToSave.getId()); - if (existingPerson.isEmpty()) { + if (existingPersonOptional.isPresent()) { + // If person exists, update the person entity + Person personToUpdate = existingPersonOptional.get(); + personToUpdate.setFirstName(userToSave.getFirstName()); + personToUpdate.setLastName(userToSave.getLastName()); + personToUpdate.setUsername(user.getUsername()); + personToUpdate.setEmail(userToSave.getEmail()); + personRepository.save(personToUpdate); + } else { // If no person is associated with the user, create a new Person entity and link it to the User - Person person = new Person(); - person.setUsername(existingUser.getUsername()); - person.setFirstName(existingUser.getFirstName()); - person.setLastName(existingUser.getLastName()); - person.setEmail(existingUser.getEmail()); - person.setUserId(existingUser.getId()); - personRepository.save(person); + Person newPerson = new Person(); + newPerson.setFirstName(userToSave.getFirstName()); + newPerson.setLastName(userToSave.getLastName()); + newPerson.setEmail(userToSave.getEmail()); + newPerson.setUsername(userToSave.getUsername()); + newPerson.setUserId(userToSave.getId()); + personRepository.save(newPerson); } } + private User getUser(User user, Optional existingUserOptional) { + User userToSave; + + if (existingUserOptional.isPresent()) { + // If user exists, update the user entity + userToSave = existingUserOptional.get(); + userToSave.setFirstName(user.getFirstName()); + userToSave.setLastName(user.getLastName()); + userToSave.setEmail(user.getEmail()); + userToSave.setUsername(user.getUsername()); + userToSave.setPassword(user.getPassword()); + } else { + // If user does not exist, save the new user entity + userToSave = user; + } + return userToSave; + } + public Long count() { return this.userRepository.count(); } 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 de58130..9beae0e 100644 --- a/src/main/java/com/application/munera/views/settings/SettingsView.java +++ b/src/main/java/com/application/munera/views/settings/SettingsView.java @@ -104,7 +104,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver // TODO: implement String monthlyIncome = this.monthlyIncome.getValue(); - userService.updateUserAndConnectedPerson(loggedInUser); + userService.saveOrUpdateUserAndConnectedPerson(loggedInUser); } @Override diff --git a/src/main/java/com/application/munera/views/users/UsersView.java b/src/main/java/com/application/munera/views/users/UsersView.java index 21c0dd1..010b712 100644 --- a/src/main/java/com/application/munera/views/users/UsersView.java +++ b/src/main/java/com/application/munera/views/users/UsersView.java @@ -1,12 +1,7 @@ package com.application.munera.views.users; import com.application.munera.data.User; -import com.application.munera.facades.ExpenseFacade; -import com.application.munera.facades.PersonFacade; -import com.application.munera.services.ExpenseService; -import com.application.munera.services.PersonService; import com.application.munera.services.UserService; -import com.application.munera.services.ViewsService; import com.application.munera.views.MainLayout; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; @@ -30,14 +25,14 @@ import com.vaadin.flow.router.BeforeEnterEvent; import com.vaadin.flow.router.BeforeEnterObserver; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; -import jakarta.annotation.security.PermitAll; +import jakarta.annotation.security.RolesAllowed; import org.springframework.orm.ObjectOptimisticLockingFailureException; import java.util.Optional; @PageTitle("Users") -@PermitAll +@RolesAllowed("ADMIN") @Route(value = "users/:userID?/:action?(edit)", layout = MainLayout.class) @Uses(Icon.class) public class UsersView extends Div implements BeforeEnterObserver { @@ -62,7 +57,7 @@ public class UsersView extends Div implements BeforeEnterObserver { private PasswordField password; private EmailField email; - public UsersView(PersonService personService, ExpenseService expenseService, ViewsService viewsService, PersonFacade personFacade, ExpenseFacade expenseFacade, UserService userService) { + public UsersView(UserService userService) { this.userService = userService; addClassNames("expenses-view"); @@ -118,7 +113,7 @@ public class UsersView extends Div implements BeforeEnterObserver { try { if (this.user == null) this.user = new User(); binder.writeBean(this.user); - this.userService.saveUserAndConnectedPerson(this.user); + this.userService.saveOrUpdateUserAndConnectedPerson(this.user); clearForm(); refreshGrid(); Notification.show("Data updated");