diff --git a/src/main/java/org/example/bookingappliation/controller/AccommodationController.java b/src/main/java/org/example/bookingappliation/controller/AccommodationController.java index 7bc1e76..4323ac4 100644 --- a/src/main/java/org/example/bookingappliation/controller/AccommodationController.java +++ b/src/main/java/org/example/bookingappliation/controller/AccommodationController.java @@ -39,7 +39,8 @@ public List getAll(Pageable pageable) { @GetMapping("/search") @Operation(summary = "Get all accommodations with parameters", description = "Get a page of all available accommodations with parameters") - public List search(Pageable pageable, AccommodationSearchDto requestDto) { + public List search(Pageable pageable, + @Valid AccommodationSearchDto requestDto) { return accommodationService.search(pageable, requestDto); } diff --git a/src/main/java/org/example/bookingappliation/repository/accommodation/specefications/CheckDateSpecificationProvider.java b/src/main/java/org/example/bookingappliation/repository/accommodation/specefications/CheckDateSpecificationProvider.java index 3fcdef2..fe46825 100644 --- a/src/main/java/org/example/bookingappliation/repository/accommodation/specefications/CheckDateSpecificationProvider.java +++ b/src/main/java/org/example/bookingappliation/repository/accommodation/specefications/CheckDateSpecificationProvider.java @@ -1,9 +1,13 @@ package org.example.bookingappliation.repository.accommodation.specefications; +import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.Join; import jakarta.persistence.criteria.JoinType; import jakarta.persistence.criteria.Predicate; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + import lombok.RequiredArgsConstructor; import org.example.bookingappliation.model.accommodation.Accommodation; import org.example.bookingappliation.model.booking.Booking; @@ -32,24 +36,28 @@ public Specification getSpecification(LocalDate[] params) { LocalDate checkInDate = params[CHECK_IN_DATE_POSITION]; LocalDate checkOutDate = params[CHECK_OUT_DATE_POSITION]; - Predicate canceledOrExpiredBooking = criteriaBuilder.or( - criteriaBuilder.equal(bookings.get("status").get("name"), "CANCEL"), - criteriaBuilder.equal(bookings.get("status").get("name"), "EXPIRED") + Predicate pendingOrConfirmedStatus = criteriaBuilder.or( + criteriaBuilder.equal(bookings.get("status").get("name"), "PENDING"), + criteriaBuilder.equal(bookings.get("status").get("name"), "CONFIRMED")); + + Predicate checkDatesWithStatusPredicate = criteriaBuilder.or( + criteriaBuilder.and( + pendingOrConfirmedStatus, + criteriaBuilder.between( + bookings.get("checkDates").get("checkInDate"), + checkInDate, checkOutDate) + ), + criteriaBuilder.and( + pendingOrConfirmedStatus, + criteriaBuilder.between( + bookings.get("checkDates").get("checkOutDate"), + checkInDate, checkOutDate) + ) ); - Predicate checkInDatePredicate = criteriaBuilder.between( - bookings.get("checkDates").get("checkInDate"), - checkInDate, checkOutDate); - - Predicate checkOutDatePredicate = criteriaBuilder.between( - bookings.get("checkDates").get("checkOutDate"), - checkInDate, checkOutDate); - - Predicate noBooking = criteriaBuilder.isEmpty(root.get("bookings")); - Predicate datePredicate = criteriaBuilder.or(noBooking, - criteriaBuilder.not(criteriaBuilder.and(checkInDatePredicate, - checkOutDatePredicate))); - Predicate statusPredicate = criteriaBuilder.or(canceledOrExpiredBooking, noBooking); - return criteriaBuilder.and(statusPredicate, datePredicate); + + return criteriaBuilder.or( + criteriaBuilder.isEmpty(root.get("bookings")), + criteriaBuilder.not(checkDatesWithStatusPredicate)); }; } }