From 32033fbef554ff11d78d8e9d861fe88d7825e00b Mon Sep 17 00:00:00 2001 From: vnglnk <128087718+holotsvan@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:17:39 +0200 Subject: [PATCH] fix data range filter (#7859) --- .../repository/impl/EventSearchRepoImpl.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/dao/src/main/java/greencity/repository/impl/EventSearchRepoImpl.java b/dao/src/main/java/greencity/repository/impl/EventSearchRepoImpl.java index cc1e563bb..e7b45c0b6 100644 --- a/dao/src/main/java/greencity/repository/impl/EventSearchRepoImpl.java +++ b/dao/src/main/java/greencity/repository/impl/EventSearchRepoImpl.java @@ -215,12 +215,24 @@ private void addDatePredicate(FilterEventDto filterEventDto, Root eventRo ZonedDateTime from = filterEventDto.getFrom(); ZonedDateTime to = filterEventDto.getTo(); ListJoin datesJoin = eventRoot.join(Event_.dates, JoinType.LEFT); - if (from != null) { - predicates.add(criteriaBuilder.greaterThanOrEqualTo(datesJoin.get(EventDateLocation_.START_DATE), from)); - } - if (to != null) { - predicates.add(criteriaBuilder.lessThanOrEqualTo(datesJoin.get(EventDateLocation_.START_DATE), to)); + Predicate finalPredicate = criteriaBuilder.conjunction(); + + if (from != null && to != null) { + Predicate dateRangePredicate = criteriaBuilder.and( + criteriaBuilder.lessThanOrEqualTo(datesJoin.get(EventDateLocation_.START_DATE), to), + criteriaBuilder.greaterThanOrEqualTo(datesJoin.get(EventDateLocation_.FINISH_DATE), from)); + finalPredicate = criteriaBuilder.and(finalPredicate, dateRangePredicate); + } else if (from != null) { + Predicate startDatePredicate = + criteriaBuilder.greaterThanOrEqualTo(datesJoin.get(EventDateLocation_.START_DATE), from); + finalPredicate = criteriaBuilder.and(finalPredicate, startDatePredicate); + } else if (to != null) { + Predicate finishDatePredicate = + criteriaBuilder.lessThanOrEqualTo(datesJoin.get(EventDateLocation_.FINISH_DATE), to); + finalPredicate = criteriaBuilder.and(finalPredicate, finishDatePredicate); } + + predicates.add(finalPredicate); } private void addEventsLikePredicate(String searchingText, Root root, List predicates) {