modification ajout clients

This commit is contained in:
mepiphana2023
2025-07-16 11:02:25 +02:00
parent 1c3d4d9a94
commit 9bb9124617
4 changed files with 24 additions and 2 deletions

View File

@@ -6,4 +6,5 @@ public interface AdresseService {
Adresse add(Adresse adresse); Adresse add(Adresse adresse);
Adresse findById(Long id); Adresse findById(Long id);
Adresse findAdresseByClientId(Long clientId); 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.Adresse;
import fr.eni.demo.bo.Client; import fr.eni.demo.bo.Client;
import fr.eni.demo.bo.Location; import fr.eni.demo.bo.Location;
import fr.eni.demo.dal.AdresseRepository;
import fr.eni.demo.dal.ClientRepository; import fr.eni.demo.dal.ClientRepository;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired; 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 org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@@ -15,11 +18,14 @@ import java.util.Optional;
public class ClientServiceImpl implements ClientService { public class ClientServiceImpl implements ClientService {
private ClientRepository clientRepository; private ClientRepository clientRepository;
private AdresseRepository adresseRepository;
public ClientServiceImpl(ClientRepository clientRepository) { public ClientServiceImpl(ClientRepository clientRepository, AdresseRepository adresseRepository) {
this.clientRepository = clientRepository; this.clientRepository = clientRepository;
this.adresseRepository = adresseRepository;
} }
@Override @Override
public void add(Client client) { public void add(Client client) {
if (client == null) { if (client == null) {
@@ -28,9 +34,23 @@ public class ClientServiceImpl implements ClientService {
if (client.getAdresse() == null) { if (client.getAdresse() == null) {
throw new IllegalArgumentException("Adresse mandatory"); 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); clientRepository.save(client);
} }
@Override @Override
public Client findById(Long clientId) { public Client findById(Long clientId) {
return clientRepository.findById(clientId) return clientRepository.findById(clientId)

View File

@@ -15,7 +15,7 @@ import org.springframework.data.mongodb.core.mapping.Field;
public class Adresse { public class Adresse {
@Id @Id
private Integer id; private String id;
@Field(name = "STREET") @Field(name = "STREET")
private String rue; private String rue;

View File

@@ -6,4 +6,5 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface AdresseRepository extends MongoRepository<Adresse, Long> { public interface AdresseRepository extends MongoRepository<Adresse, Long> {
} }