Add Location Entity, Service and Repository. Add Test to add a location line for a client wich rent a game

This commit is contained in:
Olivier PARPAILLON
2025-07-09 15:47:13 +02:00
parent e633ec5a5f
commit 3fd8990079
12 changed files with 139 additions and 34 deletions

4
gradlew.bat vendored
View File

@@ -49,7 +49,7 @@ echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2 echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2 echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2 echo adresse of your Java installation. 1>&2
goto fail goto fail
@@ -63,7 +63,7 @@ echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2 echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2 echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2 echo adresse of your Java installation. 1>&2
goto fail goto fail

View File

@@ -0,0 +1,17 @@
package fr.eni.demo.bll;
import fr.eni.demo.bo.Adresse;
import fr.eni.demo.dal.AdresseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AdresseService {
@Autowired
AdresseRepository adresseRepository;
public void add(Adresse adresse) {
adresseRepository.save(adresse);
}
}

View File

@@ -5,6 +5,8 @@ import fr.eni.demo.dal.ClientRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional;
@Service @Service
public class ClientService { public class ClientService {
@@ -14,4 +16,8 @@ public class ClientService {
public void add(Client client) { public void add(Client client) {
clientRepository.save(client); clientRepository.save(client);
} }
public Optional<Client> findById(Long clientId) {
return clientRepository.findById(clientId);
}
} }

View File

@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
public class LocationService { public class LocationService {
@Autowired @Autowired
LocationRepository locationRepository; private LocationRepository locationRepository;
public void add(Location location) { public void add(Location location) {
locationRepository.save(location); locationRepository.save(location);

View File

@@ -5,6 +5,8 @@ import fr.eni.demo.dal.StockRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional;
@Service @Service
public class StockService { public class StockService {
@@ -14,4 +16,8 @@ public class StockService {
public void add(Stock stock) { public void add(Stock stock) {
stockRepository.save(stock); stockRepository.save(stock);
} }
public Optional<Stock> findById(Long gameId) {
return stockRepository.findById(gameId);
}
} }

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="ADRESSES")
public class Adresse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ADRESSE_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

@@ -3,11 +3,13 @@ package fr.eni.demo.bo;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import java.util.List;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Getter @Getter
@Setter @Setter
@ToString @ToString(exclude = "locations")
@Builder @Builder
@Entity @Entity
@@ -28,6 +30,10 @@ public class Client {
private String email; private String email;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, optional = false) @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "LOCATION_ID") @JoinColumn(name = "ADRESSE_ID")
private Location location; private Adresse adresse;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "CLIENT_ID")
private List<Location> locations;
} }

View File

@@ -3,11 +3,13 @@ package fr.eni.demo.bo;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import java.util.Date;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Getter @Getter
@Setter @Setter
@ToString @ToString(exclude = {"client", "stock"})
@Builder @Builder
@Entity @Entity
@@ -16,16 +18,20 @@ public class Location {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LOCATION_ID") @Column(name="LOCATION_ID")
private Integer id; private Integer id;
@Column(name = "STREET",nullable = false, length = 250) @Column(name = "LOCATION_START_DATE", nullable = false)
private String rue; private Date startDate;
@Column(name = "POSTAL_CODE",nullable = false, length = 5) @Column(name = "LOCATION_END_DATE")
private String codePostal; private Date endDate;
@Column(name = "CITY",nullable = false, length = 150) @ManyToOne(fetch = FetchType.LAZY)
private String ville; @JoinColumn(name = "CLIENT_ID", nullable = false)
private Client client;
} @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GAME_ID", nullable = false)
private Stock stock;
}

View File

@@ -34,7 +34,15 @@ public class Stock {
@Column(name="GAME_DAILY_PRICE", nullable = false) @Column(name="GAME_DAILY_PRICE", nullable = false)
private Double dailyPrice; private Double dailyPrice;
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval = true) @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name="GAME_ID") @JoinTable(
name = "STOCK_GAME_TYPE",
joinColumns = @JoinColumn(name = "GAME_ID"), // Clé étrangère vers Stock
inverseJoinColumns = @JoinColumn(name = "GAME_TYPE_ID") // Clé étrangère vers GameType
)
private List<GameType> gameType; private List<GameType> gameType;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "GAME_ID")
private List<Location> locations;
} }

View File

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

View File

@@ -5,5 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface LocationRepository extends JpaRepository<Location, Long> { public interface LocationRepository extends JpaRepository<Location, Integer> {
} }

View File

@@ -1,20 +1,17 @@
package fr.eni.demo; package fr.eni.demo;
import fr.eni.demo.bll.ClientService; import fr.eni.demo.bll.*;
import fr.eni.demo.bll.GameTypeService; import fr.eni.demo.bo.*;
import fr.eni.demo.bll.LocationService;
import fr.eni.demo.bll.StockService;
import fr.eni.demo.bo.Client;
import fr.eni.demo.bo.GameType;
import fr.eni.demo.bo.Location;
import fr.eni.demo.bo.Stock;
import org.junit.jupiter.api.DisplayName; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.sql.Date;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
@SpringBootTest @SpringBootTest
class DemoApplicationTests { class DemoApplicationTests {
@@ -22,11 +19,13 @@ class DemoApplicationTests {
@Autowired @Autowired
private ClientService clientService; private ClientService clientService;
@Autowired @Autowired
private LocationService locationService; private AdresseService adresseService;
@Autowired @Autowired
private GameTypeService gameTypeService; private GameTypeService gameTypeService;
@Autowired @Autowired
private StockService stockService; private StockService stockService;
@Autowired
private LocationService locationService;
// DEPREACTED CAUSE : Cant add Client without Location // DEPREACTED CAUSE : Cant add Client without Location
// @Test // @Test
@@ -57,7 +56,7 @@ class DemoApplicationTests {
@Test @Test
@DisplayName("-- Test add Client with Location --") @DisplayName("-- Test add Client with Location --")
void testAddClientWithLocation() { void testAddClientWithAdresse() {
// Création du client // Création du client
Client client = new Client(); Client client = new Client();
client.setEmail("julien@test.fr"); client.setEmail("julien@test.fr");
@@ -65,17 +64,17 @@ class DemoApplicationTests {
client.setPrenom("Julien"); client.setPrenom("Julien");
//Création de la location //Création de la location
Location location = new Location(); Adresse adresse = new Adresse();
location.setRue("666 Rue des Enfers"); adresse.setRue("666 Rue des Enfers");
location.setCodePostal("44000"); adresse.setCodePostal("44000");
location.setVille("Nantes"); adresse.setVille("Nantes");
//Ajout de la location au client //Ajout de la location au client
client.setLocation(location); client.setAdresse(adresse);
clientService.add(client); clientService.add(client);
System.out.println(client); System.out.println(client);
System.out.println(location); System.out.println(adresse);
} }
@Test @Test
@@ -127,4 +126,21 @@ class DemoApplicationTests {
System.out.println(game); System.out.println(game);
} }
@Test
@DisplayName("-- Test add location game to a client --")
void testAddLocationGame() {
// Find a client by his ID
Optional<Client> client = clientService.findById(1L);
// Find a Game by his ID
Optional<Stock> game = stockService.findById(1L);
// Create the Location line for this client and the game
Location gameLocation = new Location();
gameLocation.setStartDate(Date.valueOf(LocalDate.of(2025, 7, 8)));
gameLocation.setClient(client.get());
gameLocation.setStock(game.get());
locationService.add(gameLocation);
System.out.println(gameLocation);
}
} }