Skip to content

Commit

Permalink
Fixed validations in /search dto
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorTrokhymchuk committed Apr 19, 2024
1 parent 5d257c2 commit 81fbc99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public List<AccommodationDto> getAll(Pageable pageable) {
@GetMapping("/search")
@Operation(summary = "Get all accommodations with parameters",
description = "Get a page of all available accommodations with parameters")
public List<AccommodationDto> search(Pageable pageable, AccommodationSearchDto requestDto) {
public List<AccommodationDto> search(Pageable pageable,
@Valid AccommodationSearchDto requestDto) {
return accommodationService.search(pageable, requestDto);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,24 +36,28 @@ public Specification<Accommodation> 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));
};
}
}

0 comments on commit 81fbc99

Please sign in to comment.