This commit is contained in:
Olivier PARPAILLON
2025-07-16 11:30:03 +02:00
6 changed files with 36 additions and 14 deletions

View File

@@ -6,4 +6,5 @@ public interface AdresseService {
Adresse add(Adresse adresse);
Adresse findById(Long id);
Adresse findAdresseByClientId(Long clientId);
}

View File

@@ -3,9 +3,12 @@ package fr.eni.demo.bll;
import fr.eni.demo.bo.Adresse;
import fr.eni.demo.bo.Client;
import fr.eni.demo.bo.Location;
import fr.eni.demo.dal.AdresseRepository;
import fr.eni.demo.dal.ClientRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -15,11 +18,14 @@ import java.util.Optional;
public class ClientServiceImpl implements ClientService {
private ClientRepository clientRepository;
private AdresseRepository adresseRepository;
public ClientServiceImpl(ClientRepository clientRepository) {
public ClientServiceImpl(ClientRepository clientRepository, AdresseRepository adresseRepository) {
this.clientRepository = clientRepository;
this.adresseRepository = adresseRepository;
}
@Override
public void add(Client client) {
if (client == null) {
@@ -28,9 +34,23 @@ public class ClientServiceImpl implements ClientService {
if (client.getAdresse() == null) {
throw new IllegalArgumentException("Adresse mandatory");
}
Adresse adresse = client.getAdresse();
ExampleMatcher matcher = ExampleMatcher.matchingAll()
.withIgnoreNullValues()
.withIgnoreCase();
Example<Adresse> example = Example.of(adresse, matcher);
Optional<Adresse> existingAdresse = adresseRepository.findOne(example);
client.setAdresse(existingAdresse.orElseGet(() -> adresseRepository.save(adresse)));
clientRepository.save(client);
}
@Override
public Client findById(Long clientId) {
return clientRepository.findById(clientId)