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.setRoles(adminProperties.getRoles());
adminUser.setEmail(adminProperties.getEmail());
userService.saveUserAndConnectedPerson(adminUser);
userService.saveOrUpdateUserAndConnectedPerson(adminUser);
}
}
}

View file

@ -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<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() {
return this.userRepository.count();
}

View file

@ -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

View file

@ -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");