forked from pierront/mylibrary-template
Feature/manage reviews #2
+68
@@ -0,0 +1,68 @@
|
||||
package fr.iut_fbleau.but3.dev62.mylibrary.review.repository;
|
||||
|
||||
import fr.iut_fbleau.but3.dev62.mylibrary.review.entity.Review;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ReviewRepository {
|
||||
|
||||
private final List<Review> reviews = new ArrayList<>();
|
||||
|
||||
public List<Review> findAll() {
|
||||
|
||||
return reviews;
|
||||
}
|
||||
|
||||
public void deleteAll() {
|
||||
|
||||
reviews.clear();
|
||||
}
|
||||
|
||||
public Review save(Review newReview) {
|
||||
Optional<Review> optionalReviewWithSameCustomerAndBookId = this.findByCustomerAndBookId(newReview.getCustomerId(), newReview.getBookId());
|
||||
optionalReviewWithSameCustomerAndBookId.ifPresentOrElse(reviews::remove, newReview::setRandomUUID);
|
||||
this.reviews.add(newReview);
|
||||
return newReview;
|
||||
}
|
||||
|
||||
public Optional<Review> findByCustomerId(UUID customerUUID) {
|
||||
return this.reviews.stream()
|
||||
.filter(review -> review.getCustomerId().equals(customerUUID))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public Optional<Review> findByBookId(UUID bookUUID) {
|
||||
return this.reviews.stream()
|
||||
.filter(review -> review.getBookId().equals(bookUUID))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public Optional<Review> findByCustomerAndBookId(UUID customerUUID, UUID bookUUID) {
|
||||
return this.reviews.stream()
|
||||
.filter(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public boolean existsByCustomerId(UUID customerUUID) {
|
||||
return this.reviews.stream()
|
||||
.anyMatch(review -> review.getCustomerId().equals(customerUUID));
|
||||
}
|
||||
|
||||
public boolean existsByBookId(UUID bookUUID) {
|
||||
return this.reviews.stream()
|
||||
.anyMatch(review -> review.getBookId().equals(bookUUID));
|
||||
}
|
||||
|
||||
public boolean existsByCustomerAndBookId(UUID customerUUID, UUID bookUUID) {
|
||||
return this.reviews.stream()
|
||||
.anyMatch(review -> review.getCustomerId().equals(customerUUID) && review.getBookId().equals(bookUUID));
|
||||
}
|
||||
|
||||
public void delete(Review review) {
|
||||
|
||||
this.reviews.remove(review);
|
||||
}
|
||||
}
|
||||
+12
-12
@@ -187,56 +187,56 @@ public class ReviewRepositoryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByCustomerId should return true when a review with customer ID exists")
|
||||
@DisplayName("existsByCustomerId should return true when a review with customer ID exists")
|
||||
void testExistsByCustomerIdExists() {
|
||||
boolean exists = repository.ExistsByCustomerId(review1.getCustomerId());
|
||||
boolean exists = repository.existsByCustomerId(review1.getCustomerId());
|
||||
|
||||
assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByCustomerId should return false when a review with customer ID doesn't exist")
|
||||
@DisplayName("existsByCustomerId should return false when a review with customer ID doesn't exist")
|
||||
void testExistsByCustomerIdNotExists() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
|
||||
boolean exists = repository.ExistsByCustomerId(nonExistentCustomerId);
|
||||
boolean exists = repository.existsByCustomerId(nonExistentCustomerId);
|
||||
|
||||
assertFalse(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByBookId should return true when a review with book ID exists")
|
||||
@DisplayName("existsByBookId should return true when a review with book ID exists")
|
||||
void testExistsByBookIdExists() {
|
||||
boolean exists = repository.ExistsByBookId(review1.getBookId());
|
||||
boolean exists = repository.existsByBookId(review1.getBookId());
|
||||
|
||||
assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByBookId should return false when a review with book ID doesn't exist")
|
||||
@DisplayName("existsByBookId should return false when a review with book ID doesn't exist")
|
||||
void testExistsByBookIdNotExists() {
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
|
||||
boolean exists = repository.ExistsByBookId(nonExistentBookId);
|
||||
boolean exists = repository.existsByBookId(nonExistentBookId);
|
||||
|
||||
assertFalse(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByCustomerAndBookId should return true when a review with customer and book ID exists")
|
||||
@DisplayName("existsByCustomerAndBookId should return true when a review with customer and book ID exists")
|
||||
void testExistsByCustomerAndBookIdExists() {
|
||||
boolean exists = repository.ExistsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId());
|
||||
boolean exists = repository.existsByCustomerAndBookId(review1.getCustomerId(), review1.getBookId());
|
||||
|
||||
assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ExistsByCustomerAndBookId should return false when customer and book ID doesn't exist")
|
||||
@DisplayName("existsByCustomerAndBookId should return false when customer and book ID doesn't exist")
|
||||
void testExistsByCustomerAndBookIdNotExists() {
|
||||
UUID nonExistentCustomerId = UUID.randomUUID();
|
||||
UUID nonExistentBookId = UUID.randomUUID();
|
||||
|
||||
boolean exists = repository.ExistsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
boolean exists = repository.existsByCustomerAndBookId(nonExistentCustomerId, nonExistentBookId);
|
||||
|
||||
assertFalse(exists);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user