fix: UserService and UsersView

This commit is contained in:
effe 2024-09-13 13:50:08 -04:00
parent 01f8c3b69f
commit 59fe0b3bb7
4 changed files with 45 additions and 43 deletions

View file

@ -26,7 +26,7 @@ public class UserInitializer {
adminUser.setLastName(adminProperties.getLastName()); adminUser.setLastName(adminProperties.getLastName());
adminUser.setRoles(adminProperties.getRoles()); adminUser.setRoles(adminProperties.getRoles());
adminUser.setEmail(adminProperties.getEmail()); adminUser.setEmail(adminProperties.getEmail());
userService.saveUserAndConnectedPerson(adminUser); userService.saveOrUpdateUserAndConnectedPerson(adminUser);
} }
} }
} }

View file

@ -50,49 +50,56 @@ public class UserService {
return null; return null;
} }
/**
* Updates the user's data and its connected person entity
* @param user the user of which we update the data
*/
@Transactional @Transactional
public void updateUserAndConnectedPerson(User user) { public void saveOrUpdateUserAndConnectedPerson(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) {
// 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());
User userToSave = getUser(user, existingUserOptional);
User existingUser; // Save the user entity
userRepository.save(userToSave);
// Save the new user entity if he doesn't exist yet
existingUser = existingUserOptional.orElseGet(() -> userRepository.save(user));
// Check if the associated person exists for the user // 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 // If no person is associated with the user, create a new Person entity and link it to the User
Person person = new Person(); Person newPerson = new Person();
person.setUsername(existingUser.getUsername()); newPerson.setFirstName(userToSave.getFirstName());
person.setFirstName(existingUser.getFirstName()); newPerson.setLastName(userToSave.getLastName());
person.setLastName(existingUser.getLastName()); newPerson.setEmail(userToSave.getEmail());
person.setEmail(existingUser.getEmail()); newPerson.setUsername(userToSave.getUsername());
person.setUserId(existingUser.getId()); newPerson.setUserId(userToSave.getId());
personRepository.save(person); personRepository.save(newPerson);
} }
} }
private User getUser(User user, Optional<User> 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() { public Long count() {
return this.userRepository.count(); return this.userRepository.count();
} }

View file

@ -104,7 +104,7 @@ public class SettingsView extends VerticalLayout implements BeforeEnterObserver
// TODO: implement // TODO: implement
String monthlyIncome = this.monthlyIncome.getValue(); String monthlyIncome = this.monthlyIncome.getValue();
userService.updateUserAndConnectedPerson(loggedInUser); userService.saveOrUpdateUserAndConnectedPerson(loggedInUser);
} }
@Override @Override

View file

@ -1,12 +1,7 @@
package com.application.munera.views.users; package com.application.munera.views.users;
import com.application.munera.data.User; 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.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,14 +25,14 @@ import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver; import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.RolesAllowed;
import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.orm.ObjectOptimisticLockingFailureException;
import java.util.Optional; import java.util.Optional;
@PageTitle("Users") @PageTitle("Users")
@PermitAll @RolesAllowed("ADMIN")
@Route(value = "users/:userID?/:action?(edit)", layout = MainLayout.class) @Route(value = "users/:userID?/:action?(edit)", layout = MainLayout.class)
@Uses(Icon.class) @Uses(Icon.class)
public class UsersView extends Div implements BeforeEnterObserver { public class UsersView extends Div implements BeforeEnterObserver {
@ -62,7 +57,7 @@ public class UsersView extends Div implements BeforeEnterObserver {
private PasswordField password; private PasswordField password;
private EmailField email; private EmailField email;
public UsersView(PersonService personService, ExpenseService expenseService, ViewsService viewsService, PersonFacade personFacade, ExpenseFacade expenseFacade, UserService userService) { public UsersView(UserService userService) {
this.userService = userService; this.userService = userService;
addClassNames("expenses-view"); addClassNames("expenses-view");
@ -118,7 +113,7 @@ public class UsersView extends Div implements BeforeEnterObserver {
try { try {
if (this.user == null) this.user = new User(); if (this.user == null) this.user = new User();
binder.writeBean(this.user); binder.writeBean(this.user);
this.userService.saveUserAndConnectedPerson(this.user); this.userService.saveOrUpdateUserAndConnectedPerson(this.user);
clearForm(); clearForm();
refreshGrid(); refreshGrid();
Notification.show("Data updated"); Notification.show("Data updated");