main #2
src
main
java
fr
iut_fbleau
but3
dev62
mylibrary
abonnement
avis
book
commande
test
java
fr
iut_fbleau
but3
dev62
mylibrary
abonnement
converter
entity
exception
repository
usecase
validator
avis
converter
entity
exception
repository
usecase
validator
book
converter
entity
exception
repository
usecase
validator
commande
converter
entity
exception
repository
usecase
validator
features
resources
@@ -0,0 +1,48 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.dto;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public class AbonnementDTO {
|
||||||
|
|
||||||
|
private final String clientId;
|
||||||
|
private final int duree;
|
||||||
|
private final String modePaiement;
|
||||||
|
private final LocalDate dateDebut;
|
||||||
|
private final LocalDate dateFin;
|
||||||
|
private final BigDecimal montantMensuel;
|
||||||
|
|
||||||
|
public AbonnementDTO(String clientId, int duree, String modePaiement,
|
||||||
|
LocalDate dateDebut, LocalDate dateFin, BigDecimal montantMensuel) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.duree = duree;
|
||||||
|
this.modePaiement = modePaiement;
|
||||||
|
this.dateDebut = dateDebut;
|
||||||
|
this.dateFin = dateFin;
|
||||||
|
this.montantMensuel = montantMensuel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDuree() {
|
||||||
|
return duree;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModePaiement() {
|
||||||
|
return modePaiement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateDebut() {
|
||||||
|
return dateDebut;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateFin() {
|
||||||
|
return dateFin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getMontantMensuel() {
|
||||||
|
return montantMensuel;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,26 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.dto;
|
||||||
|
|
||||||
|
public class AbonnementInfo {
|
||||||
|
|
||||||
|
private final String clientId;
|
||||||
|
private final int duree;
|
||||||
|
private final String modePaiement;
|
||||||
|
|
||||||
|
public AbonnementInfo(String clientId, int duree, String modePaiement) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.duree = duree;
|
||||||
|
this.modePaiement = modePaiement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDuree() {
|
||||||
|
return duree;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModePaiement() {
|
||||||
|
return modePaiement;
|
||||||
|
}
|
||||||
|
}
|
41
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/converter/AbonnementConverter.java
Normal file
41
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/converter/AbonnementConverter.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.dto.AbonnementDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.dto.AbonnementInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
|
||||||
|
public class AbonnementConverter {
|
||||||
|
|
||||||
|
public AbonnementDTO toDTO(Abonnement abonnement) {
|
||||||
|
if (abonnement == null) return null;
|
||||||
|
return new AbonnementDTO(
|
||||||
|
abonnement.getClientId(),
|
||||||
|
abonnement.getDuree(),
|
||||||
|
abonnement.getModePaiement(),
|
||||||
|
abonnement.getDateDebut(),
|
||||||
|
abonnement.getDateFin(),
|
||||||
|
abonnement.getMontantMensuel()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Abonnement toDomain(AbonnementDTO dto) {
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new Abonnement(
|
||||||
|
dto.getClientId(),
|
||||||
|
dto.getDuree(),
|
||||||
|
dto.getModePaiement(),
|
||||||
|
dto.getDateDebut(),
|
||||||
|
dto.getDateFin(),
|
||||||
|
dto.getMontantMensuel()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbonnementInfo toInfo(Abonnement abonnement) {
|
||||||
|
if (abonnement == null) return null;
|
||||||
|
return new AbonnementInfo(
|
||||||
|
abonnement.getClientId(),
|
||||||
|
abonnement.getDuree(),
|
||||||
|
abonnement.getModePaiement()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,77 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Abonnement {
|
||||||
|
|
||||||
|
private final String clientId;
|
||||||
|
private final int duree;
|
||||||
|
private final String modePaiement;
|
||||||
|
private final LocalDate dateDebut;
|
||||||
|
private final LocalDate dateFin;
|
||||||
|
private final BigDecimal montantMensuel;
|
||||||
|
|
||||||
|
public Abonnement(String clientId, int duree, String modePaiement, LocalDate dateDebut, LocalDate dateFin, BigDecimal montantMensuel) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.duree = duree;
|
||||||
|
this.modePaiement = modePaiement;
|
||||||
|
this.dateDebut = dateDebut;
|
||||||
|
this.dateFin = dateFin;
|
||||||
|
this.montantMensuel = montantMensuel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDuree() {
|
||||||
|
return duree;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModePaiement() {
|
||||||
|
return modePaiement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateDebut() {
|
||||||
|
return dateDebut;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateFin() {
|
||||||
|
return dateFin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getMontantMensuel() {
|
||||||
|
return montantMensuel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof Abonnement that)) return false;
|
||||||
|
return duree == that.duree &&
|
||||||
|
Objects.equals(clientId, that.clientId) &&
|
||||||
|
Objects.equals(modePaiement, that.modePaiement) &&
|
||||||
|
Objects.equals(dateDebut, that.dateDebut) &&
|
||||||
|
Objects.equals(dateFin, that.dateFin) &&
|
||||||
|
Objects.equals(montantMensuel, that.montantMensuel);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(clientId, duree, modePaiement, dateDebut, dateFin, montantMensuel);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Abonnement{" +
|
||||||
|
"clientId='" + clientId + '\'' +
|
||||||
|
", duree=" + duree +
|
||||||
|
", modePaiement='" + modePaiement + '\'' +
|
||||||
|
", dateDebut=" + dateDebut +
|
||||||
|
", dateFin=" + dateFin +
|
||||||
|
", montantMensuel=" + montantMensuel +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/AbonnementNotFoundException.java
Normal file
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/AbonnementNotFoundException.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception;
|
||||||
|
|
||||||
|
public class AbonnementNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public AbonnementNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbonnementNotFoundException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/NotValidAbonnementException.java
Normal file
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/NotValidAbonnementException.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception;
|
||||||
|
|
||||||
|
public class NotValidAbonnementException extends RuntimeException {
|
||||||
|
|
||||||
|
public NotValidAbonnementException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotValidAbonnementException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
17
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/repository/AbonnementRepository.java
Normal file
17
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/repository/AbonnementRepository.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface AbonnementRepository {
|
||||||
|
|
||||||
|
Abonnement save(Abonnement abonnement);
|
||||||
|
|
||||||
|
Optional<Abonnement> findByClientId(String clientId);
|
||||||
|
|
||||||
|
void deleteByClientId(String clientId);
|
||||||
|
|
||||||
|
List<Abonnement> findAll();
|
||||||
|
}
|
44
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/usecase/AbonnementUseCase.java
Normal file
44
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/usecase/AbonnementUseCase.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception.AbonnementNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.repository.AbonnementRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.validator.AbonnementValidator;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public class AbonnementUseCase {
|
||||||
|
|
||||||
|
private final AbonnementRepository repository;
|
||||||
|
private final AbonnementValidator validator;
|
||||||
|
|
||||||
|
public AbonnementUseCase(AbonnementRepository repository, AbonnementValidator validator) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.validator = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Abonnement creerAbonnement(String clientId, int duree, String modePaiement, LocalDate dateDebutSouhaitee) {
|
||||||
|
LocalDate dateFin = dateDebutSouhaitee.plusMonths(duree);
|
||||||
|
BigDecimal montant = calculerMontantMensuel(duree);
|
||||||
|
|
||||||
|
Abonnement abonnement = new Abonnement(clientId, duree, modePaiement.toUpperCase(), dateDebutSouhaitee, dateFin, montant);
|
||||||
|
|
||||||
|
validator.valider(abonnement);
|
||||||
|
return repository.save(abonnement);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Abonnement trouverParClientId(String clientId) {
|
||||||
|
return repository.findByClientId(clientId)
|
||||||
|
.orElseThrow(() -> new AbonnementNotFoundException("Aucun abonnement pour ce client"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private BigDecimal calculerMontantMensuel(int duree) {
|
||||||
|
return switch (duree) {
|
||||||
|
case 3 -> new BigDecimal("15.00");
|
||||||
|
case 6 -> new BigDecimal("12.50");
|
||||||
|
case 12 -> new BigDecimal("10.00");
|
||||||
|
default -> throw new IllegalArgumentException("Durée d’abonnement non prise en charge : " + duree);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
39
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/validator/AbonnementValidator.java
Normal file
39
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/validator/AbonnementValidator.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception.NotValidAbonnementException;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class AbonnementValidator {
|
||||||
|
|
||||||
|
private static final Set<Integer> DUREES_VALIDES = Set.of(3, 6, 12);
|
||||||
|
private static final Set<String> MODES_VALIDES = Set.of("CB", "PAYPAL");
|
||||||
|
|
||||||
|
public void valider(Abonnement abonnement) {
|
||||||
|
if (abonnement == null) {
|
||||||
|
throw new NotValidAbonnementException("Abonnement null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (abonnement.getClientId() == null || abonnement.getClientId().isBlank()) {
|
||||||
|
throw new NotValidAbonnementException("Client ID manquant ou vide");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!DUREES_VALIDES.contains(abonnement.getDuree())) {
|
||||||
|
throw new NotValidAbonnementException("La durée doit être 3, 6 ou 12 mois");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (abonnement.getModePaiement() == null || !MODES_VALIDES.contains(abonnement.getModePaiement().toUpperCase())) {
|
||||||
|
throw new NotValidAbonnementException("Mode de paiement invalide (CB ou PAYPAL attendu)");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (abonnement.getDateDebut() == null) {
|
||||||
|
throw new NotValidAbonnementException("La date de début est obligatoire");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (abonnement.getMontantMensuel() == null || abonnement.getMontantMensuel().compareTo(BigDecimal.ZERO) <= 0) {
|
||||||
|
throw new NotValidAbonnementException("Le montant mensuel doit être positif");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,64 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.dto;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
public class AvisDTO {
|
||||||
|
|
||||||
|
private String clientId;
|
||||||
|
private String livreId;
|
||||||
|
private int note;
|
||||||
|
private String commentaire;
|
||||||
|
private LocalDate dateAchat;
|
||||||
|
|
||||||
|
public AvisDTO() {
|
||||||
|
// constructeur par défaut
|
||||||
|
}
|
||||||
|
|
||||||
|
public AvisDTO(String clientId, String livreId, int note, String commentaire, LocalDate dateAchat) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.livreId = livreId;
|
||||||
|
this.note = note;
|
||||||
|
this.commentaire = commentaire;
|
||||||
|
this.dateAchat = dateAchat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLivreId() {
|
||||||
|
return livreId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNote() {
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommentaire() {
|
||||||
|
return commentaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateAchat() {
|
||||||
|
return dateAchat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClientId(String clientId) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLivreId(String livreId) {
|
||||||
|
this.livreId = livreId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNote(int note) {
|
||||||
|
this.note = note;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommentaire(String commentaire) {
|
||||||
|
this.commentaire = commentaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateAchat(LocalDate dateAchat) {
|
||||||
|
this.dateAchat = dateAchat;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,40 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.dto;
|
||||||
|
|
||||||
|
public class AvisInfo {
|
||||||
|
|
||||||
|
private String clientId;
|
||||||
|
private int note;
|
||||||
|
private String commentaire;
|
||||||
|
|
||||||
|
public AvisInfo() {}
|
||||||
|
|
||||||
|
public AvisInfo(String clientId, int note, String commentaire) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.note = note;
|
||||||
|
this.commentaire = commentaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNote() {
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommentaire() {
|
||||||
|
return commentaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClientId(String clientId) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNote(int note) {
|
||||||
|
this.note = note;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommentaire(String commentaire) {
|
||||||
|
this.commentaire = commentaire;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,39 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.dto.AvisDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.dto.AvisInfo;
|
||||||
|
|
||||||
|
public class AvisConverter {
|
||||||
|
|
||||||
|
public AvisDTO toDTO(Avis avis) {
|
||||||
|
if (avis == null) return null;
|
||||||
|
return new AvisDTO(
|
||||||
|
avis.getClientId(),
|
||||||
|
avis.getLivreId(),
|
||||||
|
avis.getNote(),
|
||||||
|
avis.getCommentaire(),
|
||||||
|
avis.getDateAchat()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Avis toDomain(AvisDTO dto) {
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new Avis(
|
||||||
|
dto.getClientId(),
|
||||||
|
dto.getLivreId(),
|
||||||
|
dto.getNote(),
|
||||||
|
dto.getCommentaire(),
|
||||||
|
dto.getDateAchat()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AvisInfo toInfo(Avis avis) {
|
||||||
|
if (avis == null) return null;
|
||||||
|
return new AvisInfo(
|
||||||
|
avis.getClientId(),
|
||||||
|
avis.getNote(),
|
||||||
|
avis.getCommentaire()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,76 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.entity;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Avis {
|
||||||
|
|
||||||
|
private final String clientId;
|
||||||
|
private final String livreId;
|
||||||
|
private int note;
|
||||||
|
private String commentaire;
|
||||||
|
private final LocalDate dateAchat;
|
||||||
|
|
||||||
|
public Avis(String clientId, String livreId, int note, String commentaire, LocalDate dateAchat) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.livreId = livreId;
|
||||||
|
this.note = note;
|
||||||
|
this.commentaire = commentaire;
|
||||||
|
this.dateAchat = dateAchat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLivreId() {
|
||||||
|
return livreId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNote() {
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommentaire() {
|
||||||
|
return commentaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateAchat() {
|
||||||
|
return dateAchat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNote(int note) {
|
||||||
|
this.note = note;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommentaire(String commentaire) {
|
||||||
|
this.commentaire = commentaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof Avis avis)) return false;
|
||||||
|
return Objects.equals(clientId, avis.clientId)
|
||||||
|
&& Objects.equals(livreId, avis.livreId)
|
||||||
|
&& note == avis.note
|
||||||
|
&& Objects.equals(commentaire, avis.commentaire)
|
||||||
|
&& Objects.equals(dateAchat, avis.dateAchat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(clientId, livreId, note, commentaire, dateAchat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Avis{" +
|
||||||
|
"clientId='" + clientId + '\'' +
|
||||||
|
", livreId='" + livreId + '\'' +
|
||||||
|
", note=" + note +
|
||||||
|
", commentaire='" + commentaire + '\'' +
|
||||||
|
", dateAchat=" + dateAchat +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/avis/exception/AvisNotFoundException.java
Normal file
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/avis/exception/AvisNotFoundException.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.exception;
|
||||||
|
|
||||||
|
public class AvisNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public AvisNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AvisNotFoundException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/avis/exception/NotValidAvisException.java
Normal file
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/avis/exception/NotValidAvisException.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.exception;
|
||||||
|
|
||||||
|
public class NotValidAvisException extends RuntimeException {
|
||||||
|
|
||||||
|
public NotValidAvisException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotValidAvisException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface AvisRepository {
|
||||||
|
Avis save(Avis avis);
|
||||||
|
Optional<Avis> findByClientAndLivre(String clientId, String livreId);
|
||||||
|
void deleteByClientAndLivre(String clientId, String livreId);
|
||||||
|
List<Avis> findAll();
|
||||||
|
List<Avis> findByLivre(String livreId);
|
||||||
|
List<Avis> findByClient(String clientId);
|
||||||
|
}
|
@@ -0,0 +1,60 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.exception.AvisNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.exception.NotValidAvisException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.repository.AvisRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.validator.AvisValidator;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class AvisUseCase {
|
||||||
|
|
||||||
|
private final AvisRepository repository;
|
||||||
|
private final AvisValidator validator;
|
||||||
|
|
||||||
|
// Simule les achats dans un vrai système : clientId + "_" + livreId
|
||||||
|
private final Set<String> achatsValidés = new HashSet<>();
|
||||||
|
|
||||||
|
public AvisUseCase(AvisRepository repository, AvisValidator validator) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.validator = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ajouterAvis(Avis avis) {
|
||||||
|
validator.valider(avis);
|
||||||
|
String clé = avis.getClientId() + "_" + avis.getLivreId();
|
||||||
|
if (!achatsValidés.contains(clé)) {
|
||||||
|
throw new NotValidAvisException("Le client ne peut évaluer un livre qu’il n’a pas acheté");
|
||||||
|
}
|
||||||
|
repository.save(avis);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void modifierAvis(String clientId, String livreId, int nouvelleNote, String nouveauCommentaire) {
|
||||||
|
Avis existant = repository.findByClientAndLivre(clientId, livreId)
|
||||||
|
.orElseThrow(() -> new AvisNotFoundException("Aucun avis trouvé pour ce client et ce livre."));
|
||||||
|
|
||||||
|
existant.setNote(nouvelleNote);
|
||||||
|
existant.setCommentaire(nouveauCommentaire);
|
||||||
|
|
||||||
|
validator.valider(existant);
|
||||||
|
repository.save(existant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void supprimerAvis(String clientId, String livreId) {
|
||||||
|
repository.deleteByClientAndLivre(clientId, livreId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Avis> recupererAvisParLivre(String livreId) {
|
||||||
|
return repository.findByLivre(livreId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Avis> recupererAvisParClient(String clientId) {
|
||||||
|
return repository.findByClient(clientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pour les tests uniquement
|
||||||
|
public void simulerAchat(String clientId, String livreId) {
|
||||||
|
achatsValidés.add(clientId + "_" + livreId);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,33 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.exception.NotValidAvisException;
|
||||||
|
|
||||||
|
public class AvisValidator {
|
||||||
|
|
||||||
|
public void valider(Avis avis) {
|
||||||
|
if (avis == null) {
|
||||||
|
throw new NotValidAvisException("L'avis ne peut pas être null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (avis.getNote() < 1 || avis.getNote() > 5) {
|
||||||
|
throw new NotValidAvisException("La note doit être entre 1 et 5");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (avis.getCommentaire() == null || avis.getCommentaire().isBlank()) {
|
||||||
|
throw new NotValidAvisException("Le commentaire ne peut pas être vide");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (avis.getClientId() == null || avis.getClientId().isBlank()) {
|
||||||
|
throw new NotValidAvisException("Le client est obligatoire");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (avis.getLivreId() == null || avis.getLivreId().isBlank()) {
|
||||||
|
throw new NotValidAvisException("Le livre est obligatoire");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (avis.getDateAchat() == null) {
|
||||||
|
throw new NotValidAvisException("La date d'achat est obligatoire");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,74 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.Category;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BookDTO {
|
||||||
|
private String isbn;
|
||||||
|
private String title;
|
||||||
|
private String author;
|
||||||
|
private String publisher;
|
||||||
|
private LocalDate publicationDate;
|
||||||
|
private double price;
|
||||||
|
private int stock;
|
||||||
|
private List<Category> categories;
|
||||||
|
private String description;
|
||||||
|
private String language;
|
||||||
|
|
||||||
|
public BookDTO(String isbn, String title, String author, String publisher,
|
||||||
|
LocalDate publicationDate, double price, int stock,
|
||||||
|
List<Category> categories, String description, String language) {
|
||||||
|
this.isbn = isbn;
|
||||||
|
this.title = title;
|
||||||
|
this.author = author;
|
||||||
|
this.publisher = publisher;
|
||||||
|
this.publicationDate = publicationDate;
|
||||||
|
this.price = price;
|
||||||
|
this.stock = stock;
|
||||||
|
this.categories = categories;
|
||||||
|
this.description = description;
|
||||||
|
this.language = language;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIsbn() {
|
||||||
|
return isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublisher() {
|
||||||
|
return publisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getPublicationDate() {
|
||||||
|
return publicationDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStock() {
|
||||||
|
return stock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Category> getCategories() {
|
||||||
|
return categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,74 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.Category;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BookInfo {
|
||||||
|
private String isbn;
|
||||||
|
private String title;
|
||||||
|
private String author;
|
||||||
|
private String publisher;
|
||||||
|
private LocalDate publicationDate;
|
||||||
|
private double price;
|
||||||
|
private int stock;
|
||||||
|
private List<Category> categories;
|
||||||
|
private String description;
|
||||||
|
private String language;
|
||||||
|
|
||||||
|
public BookInfo(String isbn, String title, String author, String publisher,
|
||||||
|
LocalDate publicationDate, double price, int stock,
|
||||||
|
List<Category> categories, String description, String language) {
|
||||||
|
this.isbn = isbn;
|
||||||
|
this.title = title;
|
||||||
|
this.author = author;
|
||||||
|
this.publisher = publisher;
|
||||||
|
this.publicationDate = publicationDate;
|
||||||
|
this.price = price;
|
||||||
|
this.stock = stock;
|
||||||
|
this.categories = categories;
|
||||||
|
this.description = description;
|
||||||
|
this.language = language;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIsbn() {
|
||||||
|
return isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublisher() {
|
||||||
|
return publisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getPublicationDate() {
|
||||||
|
return publicationDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStock() {
|
||||||
|
return stock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Category> getCategories() {
|
||||||
|
return categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book;
|
||||||
|
|
||||||
|
public enum Category {
|
||||||
|
ROMAN,
|
||||||
|
DRAME,
|
||||||
|
SCIENCE_FICTION,
|
||||||
|
JEUNESSE,
|
||||||
|
PHILOSOPHIE,
|
||||||
|
POESIE,
|
||||||
|
CLASSIQUE,
|
||||||
|
SCIENCE
|
||||||
|
}
|
@@ -0,0 +1,37 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.*;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
|
||||||
|
public class BookConverter {
|
||||||
|
|
||||||
|
public static BookDTO toDTO(Book book) {
|
||||||
|
return new BookDTO(
|
||||||
|
book.getIsbn(),
|
||||||
|
book.getTitle(),
|
||||||
|
book.getAuthor(),
|
||||||
|
book.getPublisher(),
|
||||||
|
book.getPublicationDate(),
|
||||||
|
book.getPrice(),
|
||||||
|
book.getStock(),
|
||||||
|
book.getCategories(),
|
||||||
|
book.getDescription(),
|
||||||
|
book.getLanguage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Book fromInfo(BookInfo info) {
|
||||||
|
return Book.builder()
|
||||||
|
.isbn(info.getIsbn())
|
||||||
|
.title(info.getTitle())
|
||||||
|
.author(info.getAuthor())
|
||||||
|
.publisher(info.getPublisher())
|
||||||
|
.publicationDate(info.getPublicationDate())
|
||||||
|
.price(info.getPrice())
|
||||||
|
.stock(info.getStock())
|
||||||
|
.categories(info.getCategories())
|
||||||
|
.description(info.getDescription())
|
||||||
|
.language(info.getLanguage())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,125 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.entity;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.Category;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
private String isbn;
|
||||||
|
private String title;
|
||||||
|
private String author;
|
||||||
|
private String publisher;
|
||||||
|
private LocalDate publicationDate;
|
||||||
|
private double price;
|
||||||
|
private int stock;
|
||||||
|
private List<Category> categories;
|
||||||
|
private String description;
|
||||||
|
private String language;
|
||||||
|
|
||||||
|
private Book() {
|
||||||
|
// constructeur privé pour builder
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIsbn() {
|
||||||
|
return isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublisher() {
|
||||||
|
return publisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getPublicationDate() {
|
||||||
|
return publicationDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStock() {
|
||||||
|
return stock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Category> getCategories() {
|
||||||
|
return categories;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Builder builder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private final Book book = new Book();
|
||||||
|
|
||||||
|
public Builder isbn(String isbn) {
|
||||||
|
book.isbn = isbn;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder title(String title) {
|
||||||
|
book.title = title;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder author(String author) {
|
||||||
|
book.author = author;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder publisher(String publisher) {
|
||||||
|
book.publisher = publisher;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder publicationDate(LocalDate publicationDate) {
|
||||||
|
book.publicationDate = publicationDate;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder price(double price) {
|
||||||
|
book.price = price;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder stock(int stock) {
|
||||||
|
book.stock = stock;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder categories(List<Category> categories) {
|
||||||
|
book.categories = categories;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder description(String description) {
|
||||||
|
book.description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder language(String language) {
|
||||||
|
book.language = language;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book build() {
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java
Normal file
11
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class BookNotFoundException extends RuntimeException {
|
||||||
|
public BookNotFoundException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/InvalidIsbnException.java
Normal file
11
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/InvalidIsbnException.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class InvalidIsbnException extends RuntimeException {
|
||||||
|
public InvalidIsbnException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidIsbnException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java
Normal file
11
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class NotValidBookException extends RuntimeException {
|
||||||
|
public NotValidBookException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotValidBookException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,13 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface BookRepository {
|
||||||
|
Optional<Book> findByIsbn(String isbn);
|
||||||
|
Book save(Book book);
|
||||||
|
void delete(Book book);
|
||||||
|
List<Book> findByTitleContaining(String title);
|
||||||
|
}
|
@@ -0,0 +1,98 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.*;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.BookNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.NotValidBookException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.repository.BookRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.validator.BookValidator;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class BookUseCase {
|
||||||
|
|
||||||
|
private final BookRepository repository;
|
||||||
|
|
||||||
|
public BookUseCase(BookRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String registerBook(BookInfo info) throws NotValidBookException {
|
||||||
|
if (!BookValidator.isValid(info)) {
|
||||||
|
throw new NotValidBookException("Informations du livre invalides");
|
||||||
|
}
|
||||||
|
Book book = BookConverter.fromInfo(info);
|
||||||
|
return repository.save(book).getIsbn();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookDTO findBookByIsbn(String isbn) {
|
||||||
|
return repository.findByIsbn(isbn)
|
||||||
|
.map(BookConverter::toDTO)
|
||||||
|
.orElseThrow(() -> new BookNotFoundException("Livre non trouvé avec ISBN: " + isbn));
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookDTO updateBook(String isbn, BookInfo info) throws NotValidBookException, BookNotFoundException {
|
||||||
|
if (!BookValidator.isValid(info)) {
|
||||||
|
throw new NotValidBookException("Informations du livre invalides");
|
||||||
|
}
|
||||||
|
|
||||||
|
Book existing = repository.findByIsbn(isbn)
|
||||||
|
.orElseThrow(() -> new BookNotFoundException("Livre introuvable"));
|
||||||
|
|
||||||
|
Book updated = BookConverter.fromInfo(info);
|
||||||
|
return BookConverter.toDTO(repository.save(updated));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteBook(String isbn) throws BookNotFoundException {
|
||||||
|
Book book = repository.findByIsbn(isbn)
|
||||||
|
.orElseThrow(() -> new BookNotFoundException("Livre introuvable"));
|
||||||
|
repository.delete(book);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int addStock(String isbn, int quantity) {
|
||||||
|
Book book = repository.findByIsbn(isbn)
|
||||||
|
.orElseThrow(() -> new BookNotFoundException("Livre introuvable"));
|
||||||
|
book = Book.builder()
|
||||||
|
.isbn(book.getIsbn())
|
||||||
|
.title(book.getTitle())
|
||||||
|
.author(book.getAuthor())
|
||||||
|
.publisher(book.getPublisher())
|
||||||
|
.publicationDate(book.getPublicationDate())
|
||||||
|
.price(book.getPrice())
|
||||||
|
.stock(book.getStock() + quantity)
|
||||||
|
.categories(book.getCategories())
|
||||||
|
.description(book.getDescription())
|
||||||
|
.language(book.getLanguage())
|
||||||
|
.build();
|
||||||
|
repository.save(book);
|
||||||
|
return book.getStock();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int removeStock(String isbn, int quantity) {
|
||||||
|
Book book = repository.findByIsbn(isbn)
|
||||||
|
.orElseThrow(() -> new BookNotFoundException("Livre introuvable"));
|
||||||
|
book = Book.builder()
|
||||||
|
.isbn(book.getIsbn())
|
||||||
|
.title(book.getTitle())
|
||||||
|
.author(book.getAuthor())
|
||||||
|
.publisher(book.getPublisher())
|
||||||
|
.publicationDate(book.getPublicationDate())
|
||||||
|
.price(book.getPrice())
|
||||||
|
.stock(book.getStock() - quantity)
|
||||||
|
.categories(book.getCategories())
|
||||||
|
.description(book.getDescription())
|
||||||
|
.language(book.getLanguage())
|
||||||
|
.build();
|
||||||
|
repository.save(book);
|
||||||
|
return book.getStock();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BookDTO> findBooksByTitleContaining(String title) {
|
||||||
|
return repository.findByTitleContaining(title).stream()
|
||||||
|
.map(BookConverter::toDTO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,21 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.book.BookInfo;
|
||||||
|
|
||||||
|
public class BookValidator {
|
||||||
|
|
||||||
|
public static boolean isValid(BookInfo info) {
|
||||||
|
if (info == null) return false;
|
||||||
|
if (info.getIsbn() == null || !info.getIsbn().matches("\\\\d{13}")) return false;
|
||||||
|
if (info.getTitle() == null || info.getTitle().isBlank()) return false;
|
||||||
|
if (info.getAuthor() == null || info.getAuthor().isBlank()) return false;
|
||||||
|
if (info.getPublisher() == null || info.getPublisher().isBlank()) return false;
|
||||||
|
if (info.getPublicationDate() == null) return false;
|
||||||
|
if (info.getPrice() <= 0) return false;
|
||||||
|
if (info.getStock() < 0) return false;
|
||||||
|
if (info.getCategories() == null || info.getCategories().isEmpty()) return false;
|
||||||
|
if (info.getDescription() == null || info.getDescription().isBlank()) return false;
|
||||||
|
if (info.getLanguage() == null || info.getLanguage().isBlank()) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,54 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.dto;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.AdresseLivraison;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.LigneCommande;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class CommandeDTO {
|
||||||
|
|
||||||
|
private final String clientId;
|
||||||
|
private final String methodePaiement;
|
||||||
|
private final List<LigneCommande> lignes;
|
||||||
|
private final AdresseLivraison adresseLivraison;
|
||||||
|
|
||||||
|
public CommandeDTO(String clientId, String methodePaiement, List<LigneCommande> lignes, AdresseLivraison adresseLivraison) {
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.methodePaiement = methodePaiement;
|
||||||
|
this.lignes = lignes;
|
||||||
|
this.adresseLivraison = adresseLivraison;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMethodePaiement() {
|
||||||
|
return methodePaiement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<LigneCommande> getLignes() {
|
||||||
|
return lignes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AdresseLivraison getAdresseLivraison() {
|
||||||
|
return adresseLivraison;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof CommandeDTO)) return false;
|
||||||
|
CommandeDTO that = (CommandeDTO) o;
|
||||||
|
return Objects.equals(clientId, that.clientId)
|
||||||
|
&& Objects.equals(methodePaiement, that.methodePaiement)
|
||||||
|
&& Objects.equals(lignes, that.lignes)
|
||||||
|
&& Objects.equals(adresseLivraison, that.adresseLivraison);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(clientId, methodePaiement, lignes, adresseLivraison);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,59 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.dto;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class CommandeInfo {
|
||||||
|
|
||||||
|
private final String commandeId;
|
||||||
|
private final String clientId;
|
||||||
|
private final BigDecimal total;
|
||||||
|
private final String methodePaiement;
|
||||||
|
private final LocalDate dateCommande;
|
||||||
|
|
||||||
|
public CommandeInfo(String commandeId, String clientId, BigDecimal total, String methodePaiement, LocalDate dateCommande) {
|
||||||
|
this.commandeId = commandeId;
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.total = total;
|
||||||
|
this.methodePaiement = methodePaiement;
|
||||||
|
this.dateCommande = dateCommande;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommandeId() {
|
||||||
|
return commandeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getTotal() {
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMethodePaiement() {
|
||||||
|
return methodePaiement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateCommande() {
|
||||||
|
return dateCommande;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof CommandeInfo)) return false;
|
||||||
|
CommandeInfo that = (CommandeInfo) o;
|
||||||
|
return Objects.equals(commandeId, that.commandeId)
|
||||||
|
&& Objects.equals(clientId, that.clientId)
|
||||||
|
&& Objects.equals(total, that.total)
|
||||||
|
&& Objects.equals(methodePaiement, that.methodePaiement)
|
||||||
|
&& Objects.equals(dateCommande, that.dateCommande);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(commandeId, clientId, total, methodePaiement, dateCommande);
|
||||||
|
}
|
||||||
|
}
|
66
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/converter/CommandeConverter.java
Normal file
66
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/converter/CommandeConverter.java
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.dto.CommandeDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.dto.CommandeInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CommandeConverter {
|
||||||
|
|
||||||
|
public CommandeDTO toDTO(Commande commande) {
|
||||||
|
if (commande == null) return null;
|
||||||
|
|
||||||
|
return new CommandeDTO(
|
||||||
|
commande.getClientId(),
|
||||||
|
commande.getMethodePaiement(),
|
||||||
|
commande.getLignes(),
|
||||||
|
commande.getAdresseLivraison()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandeInfo toInfo(Commande commande) {
|
||||||
|
if (commande == null) return null;
|
||||||
|
|
||||||
|
return new CommandeInfo(
|
||||||
|
commande.getCommandeId(),
|
||||||
|
commande.getClientId(),
|
||||||
|
commande.getTotal(),
|
||||||
|
commande.getMethodePaiement(),
|
||||||
|
commande.getDateCommande()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> toMap(Commande commande) {
|
||||||
|
if (commande == null) return null;
|
||||||
|
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("id", commande.getCommandeId());
|
||||||
|
map.put("clientId", commande.getClientId());
|
||||||
|
map.put("date", commande.getDateCommande());
|
||||||
|
map.put("prixTotal", commande.getTotal());
|
||||||
|
map.put("methodePaiement", commande.getMethodePaiement());
|
||||||
|
|
||||||
|
Map<String, String> adresse = new HashMap<>();
|
||||||
|
adresse.put("rue", commande.getAdresseLivraison().getRue());
|
||||||
|
adresse.put("ville", commande.getAdresseLivraison().getVille());
|
||||||
|
adresse.put("codePostal", commande.getAdresseLivraison().getCodePostal());
|
||||||
|
adresse.put("pays", commande.getAdresseLivraison().getPays());
|
||||||
|
map.put("adresseLivraison", adresse);
|
||||||
|
|
||||||
|
List<Map<String, Object>> lignes = commande.getLignes().stream()
|
||||||
|
.map(l -> {
|
||||||
|
Map<String, Object> ligneMap = new HashMap<>();
|
||||||
|
ligneMap.put("isbn", l.getIsbn());
|
||||||
|
ligneMap.put("quantite", l.getQuantite());
|
||||||
|
ligneMap.put("prixUnitaire", l.getPrixUnitaire());
|
||||||
|
return ligneMap;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
map.put("lignes", lignes);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
55
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/entity/AdresseLivraison.java
Normal file
55
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/entity/AdresseLivraison.java
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class AdresseLivraison {
|
||||||
|
|
||||||
|
private final String rue;
|
||||||
|
private final String ville;
|
||||||
|
private final String codePostal;
|
||||||
|
private final String pays;
|
||||||
|
|
||||||
|
public AdresseLivraison(String rue, String ville, String codePostal, String pays) {
|
||||||
|
this.rue = rue;
|
||||||
|
this.ville = ville;
|
||||||
|
this.codePostal = codePostal;
|
||||||
|
this.pays = pays;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRue() {
|
||||||
|
return rue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVille() {
|
||||||
|
return ville;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCodePostal() {
|
||||||
|
return codePostal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPays() {
|
||||||
|
return pays;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof AdresseLivraison)) return false;
|
||||||
|
AdresseLivraison that = (AdresseLivraison) o;
|
||||||
|
return Objects.equals(rue, that.rue) &&
|
||||||
|
Objects.equals(ville, that.ville) &&
|
||||||
|
Objects.equals(codePostal, that.codePostal) &&
|
||||||
|
Objects.equals(pays, that.pays);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(rue, ville, codePostal, pays);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return rue + ", " + codePostal + " " + ville + ", " + pays;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,81 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Commande {
|
||||||
|
|
||||||
|
private final String commandeId;
|
||||||
|
private final String clientId;
|
||||||
|
private final List<LigneCommande> lignes;
|
||||||
|
private final AdresseLivraison adresseLivraison;
|
||||||
|
private final BigDecimal total;
|
||||||
|
private final String methodePaiement;
|
||||||
|
private final LocalDate dateCommande;
|
||||||
|
|
||||||
|
public Commande(String commandeId, String clientId, List<LigneCommande> lignes,
|
||||||
|
AdresseLivraison adresseLivraison, BigDecimal total,
|
||||||
|
String methodePaiement, LocalDate dateCommande) {
|
||||||
|
this.commandeId = commandeId;
|
||||||
|
this.clientId = clientId;
|
||||||
|
this.lignes = lignes;
|
||||||
|
this.adresseLivraison = adresseLivraison;
|
||||||
|
this.total = total;
|
||||||
|
this.methodePaiement = methodePaiement;
|
||||||
|
this.dateCommande = dateCommande;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommandeId() {
|
||||||
|
return commandeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClientId() {
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<LigneCommande> getLignes() {
|
||||||
|
return lignes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AdresseLivraison getAdresseLivraison() {
|
||||||
|
return adresseLivraison;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getTotal() {
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMethodePaiement() {
|
||||||
|
return methodePaiement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDateCommande() {
|
||||||
|
return dateCommande;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof Commande)) return false;
|
||||||
|
Commande commande = (Commande) o;
|
||||||
|
return Objects.equals(commandeId, commande.commandeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(commandeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Commande{" +
|
||||||
|
"commandeId='" + commandeId + '\'' +
|
||||||
|
", clientId='" + clientId + '\'' +
|
||||||
|
", total=" + total +
|
||||||
|
", paiement='" + methodePaiement + '\'' +
|
||||||
|
", date=" + dateCommande +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,53 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class LigneCommande {
|
||||||
|
|
||||||
|
private final String isbn;
|
||||||
|
private final int quantite;
|
||||||
|
private final BigDecimal prixUnitaire;
|
||||||
|
|
||||||
|
public LigneCommande(String isbn, int quantite, BigDecimal prixUnitaire) {
|
||||||
|
this.isbn = isbn;
|
||||||
|
this.quantite = quantite;
|
||||||
|
this.prixUnitaire = prixUnitaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIsbn() {
|
||||||
|
return isbn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getQuantite() {
|
||||||
|
return quantite;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getPrixUnitaire() {
|
||||||
|
return prixUnitaire;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof LigneCommande)) return false;
|
||||||
|
LigneCommande that = (LigneCommande) o;
|
||||||
|
return quantite == that.quantite &&
|
||||||
|
Objects.equals(isbn, that.isbn) &&
|
||||||
|
Objects.equals(prixUnitaire, that.prixUnitaire);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(isbn, quantite, prixUnitaire);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "LigneCommande{" +
|
||||||
|
"isbn='" + isbn + '\'' +
|
||||||
|
", quantite=" + quantite +
|
||||||
|
", prixUnitaire=" + prixUnitaire +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/exception/CommandeNotFoundException.java
Normal file
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/exception/CommandeNotFoundException.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||||
|
|
||||||
|
public class CommandeNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public CommandeNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandeNotFoundException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/exception/NotValidCommandeException.java
Normal file
12
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/exception/NotValidCommandeException.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
|
||||||
|
|
||||||
|
public class NotValidCommandeException extends RuntimeException {
|
||||||
|
|
||||||
|
public NotValidCommandeException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotValidCommandeException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
17
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/repository/CommandeRepository.java
Normal file
17
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/repository/CommandeRepository.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface CommandeRepository {
|
||||||
|
|
||||||
|
Commande save(Commande commande);
|
||||||
|
|
||||||
|
Optional<Commande> findById(String commandeId);
|
||||||
|
|
||||||
|
List<Commande> findByClientId(String clientId);
|
||||||
|
|
||||||
|
List<Commande> findAll();
|
||||||
|
}
|
119
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/usecase/CommandeUseCase.java
Normal file
119
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/usecase/CommandeUseCase.java
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.*;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.CommandeNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.repository.CommandeRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.validator.CommandeValidator;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.converter.CommandeConverter;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class CommandeUseCase {
|
||||||
|
|
||||||
|
private final CommandeRepository repository;
|
||||||
|
private final CommandeValidator validator;
|
||||||
|
private final Map<String, Integer> stockParLivre;
|
||||||
|
private final Map<String, Integer> pointsFideliteParClient;
|
||||||
|
|
||||||
|
public CommandeUseCase(
|
||||||
|
CommandeRepository repository,
|
||||||
|
CommandeValidator validator,
|
||||||
|
Map<String, Integer> stockParLivre,
|
||||||
|
Map<String, Integer> pointsFideliteParClient
|
||||||
|
) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.validator = validator;
|
||||||
|
this.stockParLivre = stockParLivre;
|
||||||
|
this.pointsFideliteParClient = pointsFideliteParClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Commande creerCommande(String clientId, String methodePaiement,
|
||||||
|
List<LigneCommande> lignes, AdresseLivraison adresse) {
|
||||||
|
|
||||||
|
if (!pointsFideliteParClient.containsKey(clientId)) {
|
||||||
|
throw new NotValidCommandeException("Le client n'existe pas");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérifier le stock pour chaque livre
|
||||||
|
for (LigneCommande ligne : lignes) {
|
||||||
|
int stockDispo = stockParLivre.getOrDefault(ligne.getIsbn(), 0);
|
||||||
|
if (ligne.getQuantite() > stockDispo) {
|
||||||
|
throw new NotValidCommandeException("Stock insuffisant pour le livre : " + ligne.getIsbn());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcul du total
|
||||||
|
BigDecimal total = lignes.stream()
|
||||||
|
.map(l -> l.getPrixUnitaire().multiply(BigDecimal.valueOf(l.getQuantite())))
|
||||||
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
|
|
||||||
|
// Déduire points de fidélité si nécessaire
|
||||||
|
if ("LOYALTY_POINTS".equalsIgnoreCase(methodePaiement)) {
|
||||||
|
int pointsDispo = pointsFideliteParClient.get(clientId);
|
||||||
|
int pointsRequis = total.multiply(BigDecimal.valueOf(20)).intValue(); // 1€ = 20pts
|
||||||
|
|
||||||
|
if (pointsRequis > pointsDispo) {
|
||||||
|
throw new NotValidCommandeException("Pas assez de points fidélité pour payer cette commande");
|
||||||
|
}
|
||||||
|
pointsFideliteParClient.put(clientId, pointsDispo - pointsRequis);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Décrémenter le stock
|
||||||
|
for (LigneCommande ligne : lignes) {
|
||||||
|
stockParLivre.put(ligne.getIsbn(),
|
||||||
|
stockParLivre.get(ligne.getIsbn()) - ligne.getQuantite());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Générer et valider commande
|
||||||
|
String id = UUID.randomUUID().toString();
|
||||||
|
Commande commande = new Commande(
|
||||||
|
id,
|
||||||
|
clientId,
|
||||||
|
lignes,
|
||||||
|
adresse,
|
||||||
|
total,
|
||||||
|
methodePaiement,
|
||||||
|
LocalDate.now()
|
||||||
|
);
|
||||||
|
|
||||||
|
validator.valider(commande);
|
||||||
|
return repository.save(commande);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Commande trouverParId(String commandeId) {
|
||||||
|
return repository.findById(commandeId)
|
||||||
|
.orElseThrow(() -> new CommandeNotFoundException("Commande non trouvée : " + commandeId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Commande> trouverParClientId(String clientId) {
|
||||||
|
if (!pointsFideliteParClient.containsKey(clientId)) {
|
||||||
|
throw new NotValidCommandeException("Client non trouvé");
|
||||||
|
}
|
||||||
|
return repository.findByClientId(clientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> recupererCommandeParId(String commandeId) {
|
||||||
|
Optional<Commande> optCommande = repository.findById(commandeId);
|
||||||
|
if (optCommande.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("Commande non trouvée");
|
||||||
|
}
|
||||||
|
return CommandeConverter.toMap(trouverParId(commandeId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Map<String, Object>> recupererCommandesParClient(String clientId) {
|
||||||
|
if (!pointsFideliteParClient.containsKey(clientId)) {
|
||||||
|
throw new NotValidCommandeException("Client non trouvé");
|
||||||
|
}
|
||||||
|
return repository.findByClientId(clientId).stream()
|
||||||
|
.map(CommandeConverter::toMap)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandeRepository getRepository() {
|
||||||
|
return this.repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
61
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/validator/CommandeValidator.java
Normal file
61
src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/commande/validator/CommandeValidator.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.commande.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.*;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class CommandeValidator {
|
||||||
|
|
||||||
|
private static final Set<String> MODES_VALIDES = Set.of("CREDIT_CARD", "LOYALTY_POINTS");
|
||||||
|
|
||||||
|
public void valider(Commande commande) {
|
||||||
|
if (commande == null) {
|
||||||
|
throw new NotValidCommandeException("Commande null");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commande.getClientId() == null || commande.getClientId().isBlank()) {
|
||||||
|
throw new NotValidCommandeException("Client ID manquant");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<LigneCommande> lignes = commande.getLignes();
|
||||||
|
if (lignes == null || lignes.isEmpty()) {
|
||||||
|
throw new NotValidCommandeException("La commande doit contenir au moins un livre");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (LigneCommande ligne : lignes) {
|
||||||
|
if (ligne.getIsbn() == null || ligne.getIsbn().isBlank()) {
|
||||||
|
throw new NotValidCommandeException("ISBN manquant dans une ligne de commande");
|
||||||
|
}
|
||||||
|
if (ligne.getQuantite() <= 0) {
|
||||||
|
throw new NotValidCommandeException("Quantité invalide pour un livre");
|
||||||
|
}
|
||||||
|
if (ligne.getPrixUnitaire() == null || ligne.getPrixUnitaire().compareTo(BigDecimal.ZERO) <= 0) {
|
||||||
|
throw new NotValidCommandeException("Prix unitaire invalide pour un livre");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AdresseLivraison adresse = commande.getAdresseLivraison();
|
||||||
|
if (adresse == null ||
|
||||||
|
adresse.getRue() == null || adresse.getRue().isBlank() ||
|
||||||
|
adresse.getVille() == null || adresse.getVille().isBlank() ||
|
||||||
|
adresse.getCodePostal() == null || adresse.getCodePostal().isBlank() ||
|
||||||
|
adresse.getPays() == null || adresse.getPays().isBlank()) {
|
||||||
|
throw new NotValidCommandeException("Adresse de livraison incomplète");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commande.getMethodePaiement() == null || !MODES_VALIDES.contains(commande.getMethodePaiement().toUpperCase())) {
|
||||||
|
throw new NotValidCommandeException("Mode de paiement invalide");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commande.getTotal() == null || commande.getTotal().compareTo(BigDecimal.ZERO) <= 0) {
|
||||||
|
throw new NotValidCommandeException("Total invalide");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commande.getDateCommande() == null) {
|
||||||
|
throw new NotValidCommandeException("Date de commande manquante");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
68
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/converter/AbonnementConverterTest.java
Normal file
68
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/converter/AbonnementConverterTest.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.dto.AbonnementDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.dto.AbonnementInfo;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AbonnementConverterTest {
|
||||||
|
|
||||||
|
private AbonnementConverter converter;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
converter = new AbonnementConverter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToDTO() {
|
||||||
|
Abonnement a = new Abonnement("client-1", 12, "CB", LocalDate.of(2025, 1, 1), LocalDate.of(2026, 1, 1), new BigDecimal("10.00"));
|
||||||
|
AbonnementDTO dto = converter.toDTO(a);
|
||||||
|
|
||||||
|
assertNotNull(dto);
|
||||||
|
assertEquals("client-1", dto.getClientId());
|
||||||
|
assertEquals(12, dto.getDuree());
|
||||||
|
assertEquals("CB", dto.getModePaiement());
|
||||||
|
assertEquals(LocalDate.of(2025, 1, 1), dto.getDateDebut());
|
||||||
|
assertEquals(LocalDate.of(2026, 1, 1), dto.getDateFin());
|
||||||
|
assertEquals(new BigDecimal("10.00"), dto.getMontantMensuel());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToDomain() {
|
||||||
|
AbonnementDTO dto = new AbonnementDTO("client-2", 6, "PAYPAL", LocalDate.of(2025, 3, 1), LocalDate.of(2025, 9, 1), new BigDecimal("8.50"));
|
||||||
|
Abonnement a = converter.toDomain(dto);
|
||||||
|
|
||||||
|
assertNotNull(a);
|
||||||
|
assertEquals("client-2", a.getClientId());
|
||||||
|
assertEquals(6, a.getDuree());
|
||||||
|
assertEquals("PAYPAL", a.getModePaiement());
|
||||||
|
assertEquals(LocalDate.of(2025, 3, 1), a.getDateDebut());
|
||||||
|
assertEquals(LocalDate.of(2025, 9, 1), a.getDateFin());
|
||||||
|
assertEquals(new BigDecimal("8.50"), a.getMontantMensuel());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToInfo() {
|
||||||
|
Abonnement a = new Abonnement("client-3", 3, "CB", LocalDate.of(2025, 5, 1), LocalDate.of(2025, 8, 1), new BigDecimal("12.00"));
|
||||||
|
AbonnementInfo info = converter.toInfo(a);
|
||||||
|
|
||||||
|
assertNotNull(info);
|
||||||
|
assertEquals("client-3", info.getClientId());
|
||||||
|
assertEquals(3, info.getDuree());
|
||||||
|
assertEquals("CB", info.getModePaiement());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNullInputs() {
|
||||||
|
assertNull(converter.toDTO(null));
|
||||||
|
assertNull(converter.toDomain(null));
|
||||||
|
assertNull(converter.toInfo(null));
|
||||||
|
}
|
||||||
|
}
|
44
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/entity/AbonnementTest.java
Normal file
44
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/entity/AbonnementTest.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AbonnementTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructeurEtGetters() {
|
||||||
|
LocalDate debut = LocalDate.of(2025, 6, 1);
|
||||||
|
LocalDate fin = debut.plusMonths(6);
|
||||||
|
Abonnement a = new Abonnement("client-1", 6, "CB", debut, fin, new BigDecimal("10.00"));
|
||||||
|
|
||||||
|
assertEquals("client-1", a.getClientId());
|
||||||
|
assertEquals(6, a.getDuree());
|
||||||
|
assertEquals("CB", a.getModePaiement());
|
||||||
|
assertEquals(debut, a.getDateDebut());
|
||||||
|
assertEquals(fin, a.getDateFin());
|
||||||
|
assertEquals(new BigDecimal("10.00"), a.getMontantMensuel());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEqualsEtHashCode() {
|
||||||
|
LocalDate debut = LocalDate.of(2025, 1, 1);
|
||||||
|
Abonnement a1 = new Abonnement("client-2", 3, "PAYPAL", debut, debut.plusMonths(3), new BigDecimal("9.99"));
|
||||||
|
Abonnement a2 = new Abonnement("client-2", 3, "PAYPAL", debut, debut.plusMonths(3), new BigDecimal("9.99"));
|
||||||
|
Abonnement a3 = new Abonnement("autre", 12, "CB", debut, debut.plusMonths(12), new BigDecimal("15.00"));
|
||||||
|
|
||||||
|
assertEquals(a1, a2);
|
||||||
|
assertEquals(a1.hashCode(), a2.hashCode());
|
||||||
|
assertNotEquals(a1, a3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToStringNonVide() {
|
||||||
|
Abonnement a = new Abonnement("client-4", 12, "CB", LocalDate.now(), LocalDate.now().plusMonths(12), new BigDecimal("13.99"));
|
||||||
|
assertNotNull(a.toString());
|
||||||
|
assertFalse(a.toString().isBlank());
|
||||||
|
}
|
||||||
|
}
|
28
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/AbonnementNotFoundExceptionTest.java
Normal file
28
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/AbonnementNotFoundExceptionTest.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AbonnementNotFoundExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructeurAvecMessage() {
|
||||||
|
AbonnementNotFoundException e = new AbonnementNotFoundException("Abonnement introuvable");
|
||||||
|
assertEquals("Abonnement introuvable", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructeurAvecCause() {
|
||||||
|
Throwable cause = new RuntimeException("Cause d'origine");
|
||||||
|
AbonnementNotFoundException e = new AbonnementNotFoundException("Erreur", cause);
|
||||||
|
assertEquals("Erreur", e.getMessage());
|
||||||
|
assertEquals(cause, e.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEstRuntimeException() {
|
||||||
|
AbonnementNotFoundException e = new AbonnementNotFoundException("test");
|
||||||
|
assertTrue(e instanceof RuntimeException);
|
||||||
|
}
|
||||||
|
}
|
28
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/NotValidAbonnementExceptionTest.java
Normal file
28
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/exception/NotValidAbonnementExceptionTest.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class NotValidAbonnementExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructeurAvecMessage() {
|
||||||
|
NotValidAbonnementException e = new NotValidAbonnementException("Abonnement invalide");
|
||||||
|
assertEquals("Abonnement invalide", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructeurAvecCause() {
|
||||||
|
Throwable cause = new IllegalArgumentException("champ manquant");
|
||||||
|
NotValidAbonnementException e = new NotValidAbonnementException("Erreur de validation", cause);
|
||||||
|
assertEquals("Erreur de validation", e.getMessage());
|
||||||
|
assertEquals(cause, e.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEstRuntimeException() {
|
||||||
|
NotValidAbonnementException e = new NotValidAbonnementException("test");
|
||||||
|
assertTrue(e instanceof RuntimeException);
|
||||||
|
}
|
||||||
|
}
|
60
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/repository/AbonnementRepositoryTest.java
Normal file
60
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/repository/AbonnementRepositoryTest.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AbonnementRepositoryTest {
|
||||||
|
|
||||||
|
private AbonnementRepository repository;
|
||||||
|
private Abonnement a1, a2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
repository = new InMemoryAbonnementRepository();
|
||||||
|
|
||||||
|
a1 = new Abonnement("client-1", 12, "CB", LocalDate.of(2025, 1, 1), LocalDate.of(2026, 1, 1), new BigDecimal("10.0"));
|
||||||
|
a2 = new Abonnement("client-2", 6, "PAYPAL", LocalDate.of(2025, 2, 1), LocalDate.of(2025, 8, 1), new BigDecimal("12.5"));
|
||||||
|
|
||||||
|
repository.save(a1);
|
||||||
|
repository.save(a2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindByClientId() {
|
||||||
|
Optional<Abonnement> found = repository.findByClientId("client-1");
|
||||||
|
assertTrue(found.isPresent());
|
||||||
|
assertEquals("CB", found.get().getModePaiement());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSaveOverridesExisting() {
|
||||||
|
Abonnement updated = new Abonnement("client-1", 3, "PAYPAL", LocalDate.of(2025, 3, 1), LocalDate.of(2025, 6, 1), new BigDecimal("8.0"));
|
||||||
|
repository.save(updated);
|
||||||
|
|
||||||
|
Optional<Abonnement> found = repository.findByClientId("client-1");
|
||||||
|
assertTrue(found.isPresent());
|
||||||
|
assertEquals(3, found.get().getDuree());
|
||||||
|
assertEquals("PAYPAL", found.get().getModePaiement());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAll() {
|
||||||
|
List<Abonnement> all = repository.findAll();
|
||||||
|
assertEquals(2, all.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteByClientId() {
|
||||||
|
repository.deleteByClientId("client-2");
|
||||||
|
assertFalse(repository.findByClientId("client-2").isPresent());
|
||||||
|
assertEquals(1, repository.findAll().size());
|
||||||
|
}
|
||||||
|
}
|
31
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/repository/InMemoryAbonnementRepository.java
Normal file
31
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/repository/InMemoryAbonnementRepository.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.repository;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class InMemoryAbonnementRepository implements AbonnementRepository {
|
||||||
|
|
||||||
|
private final Map<String, Abonnement> abonnementsParClientId = new HashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Abonnement save(Abonnement abonnement) {
|
||||||
|
abonnementsParClientId.put(abonnement.getClientId(), abonnement);
|
||||||
|
return abonnement;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Abonnement> findByClientId(String clientId) {
|
||||||
|
return Optional.ofNullable(abonnementsParClientId.get(clientId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteByClientId(String clientId) {
|
||||||
|
abonnementsParClientId.remove(clientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Abonnement> findAll() {
|
||||||
|
return new ArrayList<>(abonnementsParClientId.values());
|
||||||
|
}
|
||||||
|
}
|
65
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/usecase/AbonnementUseCaseTest.java
Normal file
65
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/usecase/AbonnementUseCaseTest.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.usecase;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception.AbonnementNotFoundException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception.NotValidAbonnementException;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.repository.InMemoryAbonnementRepository;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.validator.AbonnementValidator;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AbonnementUseCaseTest {
|
||||||
|
|
||||||
|
private AbonnementUseCase useCase;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
useCase = new AbonnementUseCase(
|
||||||
|
new InMemoryAbonnementRepository(),
|
||||||
|
new AbonnementValidator()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreerAbonnementValideFonctionne() {
|
||||||
|
LocalDate dateDebut = LocalDate.of(2025, 6, 1);
|
||||||
|
Abonnement a = useCase.creerAbonnement("client-1", 12, "CB", dateDebut);
|
||||||
|
|
||||||
|
assertNotNull(a);
|
||||||
|
assertEquals("client-1", a.getClientId());
|
||||||
|
assertEquals(12, a.getDuree());
|
||||||
|
assertEquals("CB", a.getModePaiement());
|
||||||
|
assertEquals(dateDebut, a.getDateDebut());
|
||||||
|
assertEquals(dateDebut.plusMonths(12), a.getDateFin());
|
||||||
|
assertTrue(a.getMontantMensuel().compareTo(BigDecimal.ZERO) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreerAbonnementAvecDureeInvalide() {
|
||||||
|
LocalDate date = LocalDate.now();
|
||||||
|
assertThrows(NotValidAbonnementException.class, () ->
|
||||||
|
useCase.creerAbonnement("client-2", 5, "CB", date)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTrouverParClientIdFonctionne() {
|
||||||
|
Abonnement a = useCase.creerAbonnement("client-3", 6, "PAYPAL", LocalDate.now());
|
||||||
|
Abonnement trouvé = useCase.trouverParClientId("client-3");
|
||||||
|
|
||||||
|
assertEquals(a.getClientId(), trouvé.getClientId());
|
||||||
|
assertEquals(a.getDuree(), trouvé.getDuree());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTrouverParClientIdInexistantÉchoue() {
|
||||||
|
assertThrows(AbonnementNotFoundException.class, () ->
|
||||||
|
useCase.trouverParClientId("inexistant-uuid")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
60
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/validator/AbonnementValidatorTest.java
Normal file
60
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/abonnement/validator/AbonnementValidatorTest.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.abonnement.validator;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.entity.Abonnement;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.abonnement.exception.NotValidAbonnementException;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AbonnementValidatorTest {
|
||||||
|
|
||||||
|
private AbonnementValidator validator;
|
||||||
|
private final String clientId = "client-1";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
validator = new AbonnementValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAbonnementValideNeLèvePasException() {
|
||||||
|
Abonnement a = new Abonnement(clientId, 6, "CB", LocalDate.now(), LocalDate.now().plusMonths(6), new BigDecimal("9.99"));
|
||||||
|
assertDoesNotThrow(() -> validator.valider(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClientIdInvalide() {
|
||||||
|
Abonnement a = new Abonnement(null, 6, "CB", LocalDate.now(), LocalDate.now().plusMonths(6), new BigDecimal("9.99"));
|
||||||
|
assertThrows(NotValidAbonnementException.class, () -> validator.valider(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDureeInvalide() {
|
||||||
|
Abonnement a = new Abonnement(clientId, 5, "CB", LocalDate.now(), LocalDate.now().plusMonths(5), new BigDecimal("9.99"));
|
||||||
|
NotValidAbonnementException e = assertThrows(NotValidAbonnementException.class, () -> validator.valider(a));
|
||||||
|
assertTrue(e.getMessage().contains("durée"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testModePaiementInvalide() {
|
||||||
|
Abonnement a = new Abonnement(clientId, 6, "CHEQUE", LocalDate.now(), LocalDate.now().plusMonths(6), new BigDecimal("9.99"));
|
||||||
|
NotValidAbonnementException e = assertThrows(NotValidAbonnementException.class, () -> validator.valider(a));
|
||||||
|
assertTrue(e.getMessage().contains("mode de paiement"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDateDebutNulle() {
|
||||||
|
Abonnement a = new Abonnement(clientId, 6, "CB", null, LocalDate.now().plusMonths(6), new BigDecimal("9.99"));
|
||||||
|
assertThrows(NotValidAbonnementException.class, () -> validator.valider(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMontantNégatifOuNul() {
|
||||||
|
Abonnement a = new Abonnement(clientId, 6, "CB", LocalDate.now(), LocalDate.now().plusMonths(6), BigDecimal.ZERO);
|
||||||
|
assertThrows(NotValidAbonnementException.class, () -> validator.valider(a));
|
||||||
|
}
|
||||||
|
}
|
73
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/avis/converter/AvisConverterTest.java
Normal file
73
src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/avis/converter/AvisConverterTest.java
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.converter;
|
||||||
|
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.dto.AvisDTO;
|
||||||
|
import fr.iut_fbleau.but3.dev62.mylibrary.avis.dto.AvisInfo;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AvisConverterTest {
|
||||||
|
|
||||||
|
private AvisConverter converter;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
converter = new AvisConverter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToDTO() {
|
||||||
|
Avis avis = new Avis("client-1", "livre-1", 4, "Bon livre", LocalDate.of(2023, 1, 1));
|
||||||
|
AvisDTO dto = converter.toDTO(avis);
|
||||||
|
|
||||||
|
assertNotNull(dto);
|
||||||
|
assertEquals("client-1", dto.getClientId());
|
||||||
|
assertEquals("livre-1", dto.getLivreId());
|
||||||
|
assertEquals(4, dto.getNote());
|
||||||
|
assertEquals("Bon livre", dto.getCommentaire());
|
||||||
|
assertEquals(LocalDate.of(2023, 1, 1), dto.getDateAchat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToDomain() {
|
||||||
|
AvisDTO dto = new AvisDTO("client-1", "livre-1", 5, "Excellent", LocalDate.of(2023, 2, 1));
|
||||||
|
Avis avis = converter.toDomain(dto);
|
||||||
|
|
||||||
|
assertNotNull(avis);
|
||||||
|
assertEquals("client-1", avis.getClientId());
|
||||||
|
assertEquals("livre-1", avis.getLivreId());
|
||||||
|
assertEquals(5, avis.getNote());
|
||||||
|
assertEquals("Excellent", avis.getCommentaire());
|
||||||
|
assertEquals(LocalDate.of(2023, 2, 1), avis.getDateAchat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToInfo() {
|
||||||
|
Avis avis = new Avis("client-2", "livre-9", 3, "Pas mal", LocalDate.of(2022, 10, 10));
|
||||||
|
AvisInfo info = converter.toInfo(avis);
|
||||||
|
|
||||||
|
assertNotNull(info);
|
||||||
|
assertEquals("client-2", info.getClientId());
|
||||||
|
assertEquals(3, info.getNote());
|
||||||
|
assertEquals("Pas mal", info.getCommentaire());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToDTO_NullInput() {
|
||||||
|
assertNull(converter.toDTO(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToDomain_NullInput() {
|
||||||
|
assertNull(converter.toDomain(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToInfo_NullInput() {
|
||||||
|
assertNull(converter.toInfo(null));
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,46 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.avis.entity;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class AvisTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructeurEtGetters() {
|
||||||
|
String clientId = "client-1";
|
||||||
|
String livreId = "livre-1";
|
||||||
|
int note = 5;
|
||||||
|
String commentaire = "Excellent livre !";
|
||||||
|
LocalDate dateAchat = LocalDate.of(2023, 6, 1);
|
||||||
|
|
||||||
|
Avis avis = new Avis(clientId, livreId, note, commentaire, dateAchat);
|
||||||
|
|
||||||
|
assertEquals(clientId, avis.getClientId());
|
||||||
|
assertEquals(livreId, avis.getLivreId());
|
||||||
|
assertEquals(note, avis.getNote());
|
||||||
|
assertEquals(commentaire, avis.getCommentaire());
|
||||||
|
assertEquals(dateAchat, avis.getDateAchat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEqualsEtHashCode() {
|
||||||
|
Avis a1 = new Avis("c1", "l1", 4, "Bien", LocalDate.of(2023, 1, 1));
|
||||||
|
Avis a2 = new Avis("c1", "l1", 4, "Bien", LocalDate.of(2023, 1, 1));
|
||||||
|
Avis a3 = new Avis("c2", "l2", 3, "Bof", LocalDate.of(2023, 2, 2));
|
||||||
|
|
||||||
|
assertEquals(a1, a2);
|
||||||
|
assertEquals(a1.hashCode(), a2.hashCode());
|
||||||
|
|
||||||
|
assertNotEquals(a1, a3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToStringNonVide() {
|
||||||
|
Avis avis = new Avis("client", "livre", 5, "Très bon", LocalDate.now());
|
||||||
|
assertNotNull(avis.toString());
|
||||||
|
assertFalse(avis.toString().isBlank());
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user