Create Service, Entity and Repository for Client and Location. Client add test to BDD done

This commit is contained in:
Olivier PARPAILLON
2025-07-09 10:00:21 +02:00
parent c226657ca6
commit b2fffa9f9f
11 changed files with 158 additions and 11 deletions

View File

@@ -0,0 +1,25 @@
package fr.eni.demo.bll;
import fr.eni.demo.bo.Client;
import fr.eni.demo.dal.ClientRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ClientService {
@Autowired
private ClientRepository clientRepository;
public void add(Client client) {
if (client.getPrenom() == null) {
Client clientTest = new Client();
clientTest.setPrenom("Olivier");
clientTest.setNom("Parpaillon");
clientTest.setEmail("olivier@test.fr");
clientRepository.save(clientTest);
} else {
clientRepository.save(client);
}
}
}

View File

@@ -3,14 +3,14 @@ package fr.eni.demo.bll;
import java.util.List; import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import fr.eni.demo.bo.Employe; import fr.eni.demo.bo.Employe;
import fr.eni.demo.dal.EmployeDAO; import fr.eni.demo.dal.EmployeRepository;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
//Permet de faire injecter la couche DAL associée //Permet de faire injecter la couche DAL associée
@AllArgsConstructor @AllArgsConstructor
@Service @Service
public class EmployeServiceImpl implements EmployeService { public class EmployeServiceImpl implements EmployeService {
private EmployeDAO employeRepository; private EmployeRepository employeRepository;
@Override @Override
public void ajouter(Employe employe) { public void ajouter(Employe employe) {

View File

@@ -0,0 +1,25 @@
package fr.eni.demo.bll;
import fr.eni.demo.bo.Location;
import fr.eni.demo.dal.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LocationService {
@Autowired
LocationRepository locationRepository;
public void add(Location location) {
if (location.getRue() == null || location.getCodePostal() == null || location.getVille() == null) {
Location locationTest = new Location();
locationTest.setRue("18 Rue de la Paix");
locationTest.setCodePostal("75000");
locationTest.setVille("Paris");
locationRepository.save(locationTest);
} else {
locationRepository.save(location);
}
}
}

View File

@@ -1,4 +0,0 @@
package fr.eni.demo.bll;
class TestEmployeService {
}

View File

@@ -0,0 +1,33 @@
package fr.eni.demo.bo;
import jakarta.persistence.*;
import lombok.*;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Builder
@Entity
@Table(name="CLIENTS")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CLIENT_ID")
private Integer id;
@Column(name= "LAST_NAME", nullable = false, length = 90)
private String nom;
@Column(name= "FIRST_NAME", nullable = false, length = 150)
private String prenom;
@Column(nullable = false, unique = true)
private String email;
@OneToOne
@JoinColumn(name = "LOCATION_ID")
private Location location;
}

View File

@@ -0,0 +1,31 @@
package fr.eni.demo.bo;
import jakarta.persistence.*;
import lombok.*;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Builder
@Entity
@Table(name="LOCATIONS")
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LOCATION_ID")
private Integer id;
@Column(name = "STREET",nullable = false, length = 250)
private String rue;
@Column(name = "POSTAL_CODE",nullable = false, length = 5)
private String codePostal;
@Column(name = "CITY",nullable = false, length = 150)
private String ville;
}

View File

@@ -0,0 +1,9 @@
package fr.eni.demo.dal;
import fr.eni.demo.bo.Client;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ClientRepository extends JpaRepository<Client, Integer> {
}

View File

@@ -3,7 +3,7 @@ package fr.eni.demo.dal;
import java.util.List; import java.util.List;
import fr.eni.demo.bo.Employe; import fr.eni.demo.bo.Employe;
public interface EmployeDAO { public interface EmployeRepository {
void create(Employe employe); void create(Employe employe);
Employe read(Integer id); Employe read(Integer id);

View File

@@ -6,7 +6,7 @@ import fr.eni.demo.bo.Employe;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public class EmployeDAOImpl implements EmployeDAO { public class EmployeRepositoryImpl implements EmployeRepository {
private List<Employe> employes = new ArrayList<>(); private List<Employe> employes = new ArrayList<>();
@Override @Override

View File

@@ -0,0 +1,9 @@
package fr.eni.demo.dal;
import fr.eni.demo.bo.Location;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface LocationRepository extends JpaRepository<Location, Long> {
}

View File

@@ -1,13 +1,32 @@
package fr.eni.demo; package fr.eni.demo;
import fr.eni.demo.bll.ClientService;
import fr.eni.demo.bll.LocationService;
import fr.eni.demo.bo.Client;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest
class DemoApplicationTests { class DemoApplicationTests {
@Test @Autowired
void contextLoads() { private ClientService clientService;
}
@Autowired
private LocationService locationService;
@Test
@DisplayName("-- Test add Client --")
void testAddClient() {
//Création de l'objet client
Client client = new Client();
client.setEmail("olivier@test.fr");
client.setNom("Parpaillon");
client.setPrenom("Olivier");
clientService.add(client);
}
} }