Skip to content

Commit

Permalink
fix: переделал sql запрос к бд на поиск last и next booking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolaev-Java committed Aug 4, 2024
1 parent 8dc1079 commit 5adcb0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
20 changes: 12 additions & 8 deletions src/main/java/ru/practicum/shareit/booking/BookingRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ public interface BookingRepository extends JpaRepository<Booking, Long>, Queryds
Optional<Booking> findByItemNextBooking(Long itemId, LocalDateTime now);

@Query(value = """
select b from Booking as b where b.item.id in ?1
and b.status='APPROVED'
and b.startTime < ?2
order by b.startTime desc
select b from Booking as b
join(select b.item.id as item, max(b.startTime) as firstStartTime
from Booking as b
where b.startTime < ?2 and b.status='APPROVED'
group by b.item.id) as subquery on b.item.id=subquery.item and b.startTime = subquery.firstStartTime
where b.item.id in ?1
""")
List<Booking> findAllByItemsLastBooking(List<Long> itemIds, LocalDateTime now);

@Query(value = """
select b from Booking as b where b.item.id in ?1
and b.status='APPROVED'
and b.startTime > ?2
order by b.startTime asc
select b from Booking as b
join(select b.item.id as item, min(b.startTime) as firstStartTime
from Booking as b
where b.startTime > ?2 and b.status='APPROVED'
group by b.item.id) as subquery on b.item.id=subquery.item and b.startTime = subquery.firstStartTime
where b.item.id in ?1
""")
List<Booking> findAllByItemsNextBooking(List<Long> itemIds, LocalDateTime now);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,17 @@ public List<ItemInfoDto> getAllOfOwner(Long userId) {
.findAllByItemsLastBooking(new ArrayList<>(itemMap.keySet()), LocalDateTime.now());
List<Booking> nextBookings = bookingRepository
.findAllByItemsNextBooking(new ArrayList<>(itemMap.keySet()), LocalDateTime.now());
Map<Long, List<Booking>> lastBookingsByItem = lastBookings.stream()
.collect(Collectors.groupingBy(booking -> booking.getItem().getId()));
Map<Long, List<Booking>> nextBookingsByItem = nextBookings.stream()
.collect(Collectors.groupingBy(booking -> booking.getItem().getId()));
Map<Long, Booking> lastBookingByItem = lastBookingsByItem.keySet().stream()
.map(id -> lastBookingsByItem.get(id).getFirst())
.collect(Collectors.toMap(booking -> booking.getItem().getId(), item -> item));
Map<Long, Booking> nextBookingByItem = nextBookingsByItem.keySet().stream()
.map(id -> nextBookingsByItem.get(id).getFirst())
.collect(Collectors.toMap(booking -> booking.getItem().getId(), item -> item));
Map<Long, Booking> next = nextBookings.stream()
.collect(Collectors.toMap(booking -> booking.getItem().getId(), booking -> booking));
Map<Long, Booking> last = lastBookings.stream()
.collect(Collectors.toMap(booking -> booking.getItem().getId(), booking -> booking));
List<Comment> comments = commentRepository.findAllByItemIdIn(new ArrayList<>(itemMap.keySet()));
Map<Long, List<Comment>> commentsByItem = comments.stream()
.collect(Collectors.groupingBy(comment -> comment.getItem().getId()));
return itemMap.keySet().stream()
.map(id -> itemMapper.toInfoDto(itemMap.get(id),
lastBookingByItem.get(id),
nextBookingByItem.get(id),
last.get(id),
next.get(id),
commentsByItem.get(id)))
.toList();
}
Expand Down

0 comments on commit 5adcb0c

Please sign in to comment.