diff --git a/src/main/java/com/application/munera/StartupInitializer.java b/src/main/java/com/application/munera/StartupInitializer.java new file mode 100644 index 0000000..302917d --- /dev/null +++ b/src/main/java/com/application/munera/StartupInitializer.java @@ -0,0 +1,41 @@ +package com.application.munera; + +import com.application.munera.data.Person; +import com.application.munera.repositories.PersonRepository; +import com.application.munera.repositories.UserRepository; +import jakarta.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + + +@Component +public class StartupInitializer { + + private final UserRepository userRepository; + private final PersonRepository personRepository; + + @Autowired + public StartupInitializer(UserRepository userRepository, PersonRepository personRepository) { + this.userRepository = userRepository; + this.personRepository = personRepository; + } + + /** + * Initializes Person entities for all existing users. + */ + @PostConstruct + public void initializePersonEntities() { + userRepository.findAll().forEach(user -> { + // Check if Person entity exists for the user by userId + if (personRepository.findByUserId(user.getId()).isEmpty()) { + // Create and save the Person entity + Person newPerson = new Person(); + newPerson.setFirstName(user.getFirstName()); + newPerson.setLastName(user.getLastName()); + newPerson.setUsername(user.getUsername()); + newPerson.setUserId(user.getId()); + personRepository.save(newPerson); + } + }); + } +} diff --git a/src/main/java/com/application/munera/data/Person.java b/src/main/java/com/application/munera/data/Person.java index 0638e4d..5277eb1 100644 --- a/src/main/java/com/application/munera/data/Person.java +++ b/src/main/java/com/application/munera/data/Person.java @@ -50,6 +50,12 @@ public class Person extends AbstractEntity { @ManyToMany(mappedBy = "participants") private Set events; + @Column(name = "Username", unique = true, nullable = false) + private String username; // This field will link to the User entity + + @Column(name = "UserId", unique = true) + private Long userId; // Reference to the User entity + @Override public boolean equals(Object obj) { if (obj == this) { diff --git a/src/main/java/com/application/munera/data/User.java b/src/main/java/com/application/munera/data/User.java index 71eb624..4dc59e7 100644 --- a/src/main/java/com/application/munera/data/User.java +++ b/src/main/java/com/application/munera/data/User.java @@ -24,7 +24,7 @@ public class User { private String lastName; @Size(max = 100) - @Column(name = "username", nullable = false) + @Column(name = "username",unique = true, nullable = false) private String username; @Size(max = 100) diff --git a/src/main/java/com/application/munera/repositories/PersonRepository.java b/src/main/java/com/application/munera/repositories/PersonRepository.java index 39954b6..8eb0feb 100644 --- a/src/main/java/com/application/munera/repositories/PersonRepository.java +++ b/src/main/java/com/application/munera/repositories/PersonRepository.java @@ -3,6 +3,14 @@ package com.application.munera.repositories; import com.application.munera.data.Person; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; +import java.util.Optional; public interface PersonRepository extends JpaRepository, JpaSpecificationExecutor { + Optional findByUserId(Long userId); + + @Query("SELECT p FROM Person p WHERE p.userId IS NULL") + List findAllExcludeUser(); } diff --git a/src/main/java/com/application/munera/services/PersonService.java b/src/main/java/com/application/munera/services/PersonService.java index 4d5c9c5..a0893c6 100644 --- a/src/main/java/com/application/munera/services/PersonService.java +++ b/src/main/java/com/application/munera/services/PersonService.java @@ -40,6 +40,15 @@ public class PersonService { return this.personRepository.findAll(); } + + /** + * Finds all people excluding the users'ones. + * @return a collection of all persons + */ + public List findAllExcludeUsers() { + return this.personRepository.findAllExcludeUser(); + } + /** * Lists all persons with pagination. * @param pageable the pagination information diff --git a/src/main/java/com/application/munera/views/dashboard/DashboardView.java b/src/main/java/com/application/munera/views/dashboard/DashboardView.java index 29ad37e..b1dc027 100644 --- a/src/main/java/com/application/munera/views/dashboard/DashboardView.java +++ b/src/main/java/com/application/munera/views/dashboard/DashboardView.java @@ -189,7 +189,7 @@ public class DashboardView extends Div { } private String generateNegativeColumnChartScript() { - final var people = personService.findAll().stream() + final var people = personService.findAllExcludeUsers().stream() .filter(person -> personService.calculateNetBalance(person).compareTo(BigDecimal.ZERO) != 0) .toList(); if (people.isEmpty()) return generatePlaceholderChartScript("bottomLeftChart", "No Data Available"); 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 0e25096..3a9daf6 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -152,22 +152,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { } }); - //TODO: THIS NEEDS TO BE IMPLEMENTED BUT FOR THE SINGLE PERSON NOW, STILL NEEDED -// // Event listeners that will remove the selected creditors from the debtors list and vice versa -// // Done so that the user cant create an expense with the same person as creditor and debtor -// payer.addValueChangeListener(event -> { -// Person selectedDebtors = event.getValue(); -// final var creditorsSet = new HashSet<>(personService.findAll()); -// creditorsSet.removeIf(creditorsSet.contains(selectedDebtors)); -// payer.setItems(creditorsSet); -// }); -// -// creditors.addValueChangeListener(event -> { -// Set selectedCreditors = event.getValue(); -// final var debtorsSet = new HashSet<>(personService.findAll()); -// debtorsSet.removeIf(selectedCreditors::contains); -// debtors.setItems(debtorsSet); -// }); + cancel.addClickListener(e -> { clearForm(); diff --git a/src/main/java/com/application/munera/views/people/PeopleView.java b/src/main/java/com/application/munera/views/people/PeopleView.java index 4f5f3a2..09aba16 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -93,7 +93,7 @@ public class PeopleView extends Div implements BeforeEnterObserver { } })).setHeader("Actions"); - List people = personService.findAll(); + List people = personService.findAllExcludeUsers(); this.setGridData(people);