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:
4
gradlew.bat
vendored
4
gradlew.bat
vendored
@@ -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. 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
|
||||
|
||||
@@ -63,7 +63,7 @@ echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||
echo. 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
|
||||
|
||||
|
||||
17
src/main/java/fr/eni/demo/bll/AdresseService.java
Normal file
17
src/main/java/fr/eni/demo/bll/AdresseService.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import fr.eni.demo.dal.ClientRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class ClientService {
|
||||
|
||||
@@ -14,4 +16,8 @@ public class ClientService {
|
||||
public void add(Client client) {
|
||||
clientRepository.save(client);
|
||||
}
|
||||
|
||||
public Optional<Client> findById(Long clientId) {
|
||||
return clientRepository.findById(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
|
||||
public class LocationService {
|
||||
|
||||
@Autowired
|
||||
LocationRepository locationRepository;
|
||||
private LocationRepository locationRepository;
|
||||
|
||||
public void add(Location location) {
|
||||
locationRepository.save(location);
|
||||
|
||||
@@ -5,6 +5,8 @@ import fr.eni.demo.dal.StockRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class StockService {
|
||||
|
||||
@@ -14,4 +16,8 @@ public class StockService {
|
||||
public void add(Stock stock) {
|
||||
stockRepository.save(stock);
|
||||
}
|
||||
|
||||
public Optional<Stock> findById(Long gameId) {
|
||||
return stockRepository.findById(gameId);
|
||||
}
|
||||
}
|
||||
|
||||
31
src/main/java/fr/eni/demo/bo/Adresse.java
Normal file
31
src/main/java/fr/eni/demo/bo/Adresse.java
Normal 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;
|
||||
|
||||
}
|
||||
@@ -3,11 +3,13 @@ package fr.eni.demo.bo;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@ToString(exclude = "locations")
|
||||
@Builder
|
||||
|
||||
@Entity
|
||||
@@ -28,6 +30,10 @@ public class Client {
|
||||
private String email;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, optional = false)
|
||||
@JoinColumn(name = "LOCATION_ID")
|
||||
private Location location;
|
||||
@JoinColumn(name = "ADRESSE_ID")
|
||||
private Adresse adresse;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "CLIENT_ID")
|
||||
private List<Location> locations;
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@ package fr.eni.demo.bo;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@ToString(exclude = {"client", "stock"})
|
||||
@Builder
|
||||
|
||||
@Entity
|
||||
@@ -19,13 +21,17 @@ public class Location {
|
||||
@Column(name="LOCATION_ID")
|
||||
private Integer id;
|
||||
|
||||
@Column(name = "STREET",nullable = false, length = 250)
|
||||
private String rue;
|
||||
@Column(name = "LOCATION_START_DATE", nullable = false)
|
||||
private Date startDate;
|
||||
|
||||
@Column(name = "POSTAL_CODE",nullable = false, length = 5)
|
||||
private String codePostal;
|
||||
@Column(name = "LOCATION_END_DATE")
|
||||
private Date endDate;
|
||||
|
||||
@Column(name = "CITY",nullable = false, length = 150)
|
||||
private String ville;
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "CLIENT_ID", nullable = false)
|
||||
private Client client;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "GAME_ID", nullable = false)
|
||||
private Stock stock;
|
||||
}
|
||||
@@ -34,7 +34,15 @@ public class Stock {
|
||||
@Column(name="GAME_DAILY_PRICE", nullable = false)
|
||||
private Double dailyPrice;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval = true)
|
||||
@JoinColumn(name="GAME_ID")
|
||||
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
|
||||
@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;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "GAME_ID")
|
||||
private List<Location> locations;
|
||||
}
|
||||
|
||||
9
src/main/java/fr/eni/demo/dal/AdresseRepository.java
Normal file
9
src/main/java/fr/eni/demo/dal/AdresseRepository.java
Normal 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> {
|
||||
}
|
||||
@@ -5,5 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface LocationRepository extends JpaRepository<Location, Long> {
|
||||
public interface LocationRepository extends JpaRepository<Location, Integer> {
|
||||
}
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
package fr.eni.demo;
|
||||
|
||||
import fr.eni.demo.bll.ClientService;
|
||||
import fr.eni.demo.bll.GameTypeService;
|
||||
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 fr.eni.demo.bll.*;
|
||||
import fr.eni.demo.bo.*;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.sql.Date;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@SpringBootTest
|
||||
class DemoApplicationTests {
|
||||
@@ -22,11 +19,13 @@ class DemoApplicationTests {
|
||||
@Autowired
|
||||
private ClientService clientService;
|
||||
@Autowired
|
||||
private LocationService locationService;
|
||||
private AdresseService adresseService;
|
||||
@Autowired
|
||||
private GameTypeService gameTypeService;
|
||||
@Autowired
|
||||
private StockService stockService;
|
||||
@Autowired
|
||||
private LocationService locationService;
|
||||
|
||||
// DEPREACTED CAUSE : Cant add Client without Location
|
||||
// @Test
|
||||
@@ -57,7 +56,7 @@ class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("-- Test add Client with Location --")
|
||||
void testAddClientWithLocation() {
|
||||
void testAddClientWithAdresse() {
|
||||
// Création du client
|
||||
Client client = new Client();
|
||||
client.setEmail("julien@test.fr");
|
||||
@@ -65,17 +64,17 @@ class DemoApplicationTests {
|
||||
client.setPrenom("Julien");
|
||||
|
||||
//Création de la location
|
||||
Location location = new Location();
|
||||
location.setRue("666 Rue des Enfers");
|
||||
location.setCodePostal("44000");
|
||||
location.setVille("Nantes");
|
||||
Adresse adresse = new Adresse();
|
||||
adresse.setRue("666 Rue des Enfers");
|
||||
adresse.setCodePostal("44000");
|
||||
adresse.setVille("Nantes");
|
||||
|
||||
//Ajout de la location au client
|
||||
client.setLocation(location);
|
||||
client.setAdresse(adresse);
|
||||
clientService.add(client);
|
||||
|
||||
System.out.println(client);
|
||||
System.out.println(location);
|
||||
System.out.println(adresse);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -127,4 +126,21 @@ class DemoApplicationTests {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user