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

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.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);
}
}

View File

@@ -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);

View File

@@ -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);
}
}

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

View File

@@ -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
@@ -16,16 +18,20 @@ public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LOCATION_ID")
@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;
}

View File

@@ -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;
}

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;
@Repository
public interface LocationRepository extends JpaRepository<Location, Long> {
public interface LocationRepository extends JpaRepository<Location, Integer> {
}