fix: major changes

This commit is contained in:
filippo-ferrari 2024-09-08 16:33:03 +02:00
parent 50a0325341
commit ed8d4290b9
8 changed files with 68 additions and 19 deletions

View file

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

View file

@ -50,6 +50,12 @@ public class Person extends AbstractEntity {
@ManyToMany(mappedBy = "participants")
private Set<Event> 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) {

View file

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

View file

@ -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<Person, Long>, JpaSpecificationExecutor<Person> {
Optional<Person> findByUserId(Long userId);
@Query("SELECT p FROM Person p WHERE p.userId IS NULL")
List<Person> findAllExcludeUser();
}

View file

@ -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<Person> findAllExcludeUsers() {
return this.personRepository.findAllExcludeUser();
}
/**
* Lists all persons with pagination.
* @param pageable the pagination information

View file

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

View file

@ -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<Person> selectedCreditors = event.getValue();
// final var debtorsSet = new HashSet<>(personService.findAll());
// debtorsSet.removeIf(selectedCreditors::contains);
// debtors.setItems(debtorsSet);
// });
cancel.addClickListener(e -> {
clearForm();

View file

@ -93,7 +93,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
}
})).setHeader("Actions");
List<Person> people = personService.findAll();
List<Person> people = personService.findAllExcludeUsers();
this.setGridData(people);