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)

View File

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

View File

@@ -36,7 +36,7 @@ public class Client {
@Field(name = "LOCATIONS")
private List<Location> locations;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "CLIENT_ID")
@DBRef
@Field(name = "FACTURES")
private List<Facture> factures;
}

View File

@@ -1,7 +1,10 @@
package fr.eni.demo.bo;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.util.Date;
@@ -12,22 +15,19 @@ import java.util.Date;
@ToString
@Builder
@Entity
@Table(name="Facture")
@Document(collection = "factures")
public class Facture {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "FACTURE_ID")
private Long id;
private String id;
@Column(name = "PRICE")
@Field(name = "PRICE")
private Double price;
@Column(name = "DATE_PAY")
@Field(name = "DATEPAY")
private Date datePay;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "CLIENT_ID", nullable = false)
@DBRef
@Field(name = "CLIENTS")
private Client client;
}

View File

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