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

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

View File

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

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> {
} }