forked from pierront/mylibrary-template
repository
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.entity.Avis;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor
|
||||
public final class AvisRepository {
|
||||
|
||||
private final List<Avis> avisList = new ArrayList<>();
|
||||
|
||||
public List<Avis> findAll() {
|
||||
return avisList;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
avisList.clear();
|
||||
}
|
||||
|
||||
public Avis save(Avis newAvis) {
|
||||
Optional<Avis> existing = this.findById(newAvis.getId());
|
||||
existing.ifPresentOrElse(avisList::remove, newAvis::setRandomUUID);
|
||||
this.avisList.add(newAvis);
|
||||
return newAvis;
|
||||
}
|
||||
|
||||
public Optional<Avis> findById(UUID uuid) {
|
||||
return this.avisList.stream()
|
||||
.filter(avis -> avis.getId().equals(uuid))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsById(UUID uuid) {
|
||||
return this.avisList.stream()
|
||||
.anyMatch(avis -> avis.getId().equals(uuid));
|
||||
}
|
||||
|
||||
public List<Avis> findByLivreId(UUID livreId) {
|
||||
return this.avisList.stream()
|
||||
.filter(avis -> avis.getLivreId().equals(livreId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<Avis> findByClientId(UUID clientId) {
|
||||
return this.avisList.stream()
|
||||
.filter(avis -> avis.getClientId().equals(clientId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void delete(Avis avis) {
|
||||
avisList.remove(avis);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.avis.validator;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.AvisInfo;
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.avis.exception.NotValidAvisException;
|
||||
|
||||
public final class AvisValidator {
|
||||
|
||||
public static final String CLIENT_ID_CANNOT_BE_NULL = "Client id cannot be null";
|
||||
public static final String LIVRE_ID_CANNOT_BE_NULL = "Livre id cannot be null";
|
||||
public static final String NOTE_MUST_BE_BETWEEN_1_AND_5 = "Note must be between 1 and 5";
|
||||
public static final String COMMENTAIRE_CANNOT_BE_BLANK = "Commentaire cannot be blank";
|
||||
public static final String DATE_ACHAT_CANNOT_BE_NULL = "Date achat cannot be null";
|
||||
|
||||
private AvisValidator() {
|
||||
}
|
||||
|
||||
public static void validate(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
validateClientId(avisInfo);
|
||||
validateLivreId(avisInfo);
|
||||
validateNote(avisInfo);
|
||||
validateCommentaire(avisInfo);
|
||||
validateDateAchat(avisInfo);
|
||||
}
|
||||
|
||||
private static void validateClientId(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.clientId() == null) {
|
||||
throw new NotValidAvisException(CLIENT_ID_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateLivreId(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.livreId() == null) {
|
||||
throw new NotValidAvisException(LIVRE_ID_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateNote(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.note() < 1 || avisInfo.note() > 5) {
|
||||
throw new NotValidAvisException(NOTE_MUST_BE_BETWEEN_1_AND_5);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCommentaire(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.commentaire() == null || avisInfo.commentaire().isBlank()) {
|
||||
throw new NotValidAvisException(COMMENTAIRE_CANNOT_BE_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDateAchat(AvisInfo avisInfo) throws NotValidAvisException {
|
||||
if (avisInfo.dateAchat() == null) {
|
||||
throw new NotValidAvisException(DATE_ACHAT_CANNOT_BE_NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user