22
33import lombok .RequiredArgsConstructor ;
44import org .springframework .stereotype .Service ;
5+ import ru .practicum .shareit .booking .dto .BookingMapper ;
56import ru .practicum .shareit .booking .dto .BookingShortDto ;
67import ru .practicum .shareit .booking .model .Booking ;
78import ru .practicum .shareit .booking .model .BookingStatus ;
@@ -64,49 +65,29 @@ public ItemResponseDto updateItem(Long itemId, Long ownerId, ItemUpdateRequest u
6465 public ItemDetailedDto getItemById (Long itemId , Long userId ) {
6566 Item item = itemRepository .findById (itemId )
6667 .orElseThrow (() -> new NotFoundException ("Вещь не найдена" ));
67- ItemDetailedDto dto = new ItemDetailedDto ();
68- dto .setId (item .getId ());
69- dto .setName (item .getName ());
70- dto .setDescription (item .getDescription ());
71- dto .setAvailable (item .getAvailable ());
72- dto .setRequestId (item .getRequest () != null ? item .getRequest ().getId () : null );
7368
7469 LocalDateTime now = LocalDateTime .now ();
7570 boolean isOwner = item .getOwner ().getId ().equals (userId );
7671
72+ BookingShortDto lastBooking = null ;
73+ BookingShortDto nextBooking = null ;
74+
7775 if (isOwner ) {
7876 List <Booking > lastBookings = bookingRepository .findLastBooking (itemId , now );
7977 if (!lastBookings .isEmpty ()) {
80- Booking last = lastBookings .get (0 );
81- BookingShortDto lastDto = new BookingShortDto ();
82- lastDto .setId (last .getId ());
83- lastDto .setStart (last .getStart ());
84- lastDto .setEnd (last .getEnd ());
85- lastDto .setBookerId (last .getBooker ().getId ());
86- dto .setLastBooking (lastDto );
78+ lastBooking = BookingMapper .toShortDto (lastBookings .get (0 ));
8779 }
88-
8980 List <Booking > nextBookings = bookingRepository .findNextBooking (itemId , now );
9081 if (!nextBookings .isEmpty ()) {
91- Booking next = nextBookings .get (0 );
92- BookingShortDto nextDto = new BookingShortDto ();
93- nextDto .setId (next .getId ());
94- nextDto .setStart (next .getStart ());
95- nextDto .setEnd (next .getEnd ());
96- nextDto .setBookerId (next .getBooker ().getId ());
97- dto .setNextBooking (nextDto );
82+ nextBooking = BookingMapper .toShortDto (nextBookings .get (0 ));
9883 }
99- } else {
100- dto .setLastBooking (null );
101- dto .setNextBooking (null );
10284 }
10385
10486 List <CommentResponseDto > comments = commentRepository .findByItemIdOrderByCreatedDesc (itemId ).stream ()
10587 .map (CommentMapper ::toResponseDto )
10688 .collect (Collectors .toList ());
107- dto .setComments (comments );
10889
109- return dto ;
90+ return ItemMapper . toDetailedDto ( item , lastBooking , nextBooking , comments ) ;
11091 }
11192
11293 @ Override
@@ -116,9 +97,33 @@ public List<ItemWithBookingsDto> getItemsByOwner(Long ownerId) {
11697 }
11798
11899 List <Item > items = itemRepository .findByOwnerId (ownerId );
119- LocalDateTime now = LocalDateTime .now ();
100+ if (items .isEmpty ()) {
101+ return Collections .emptyList ();
102+ }
120103
104+ LocalDateTime now = LocalDateTime .now ();
121105 List <Long > itemIds = items .stream ().map (Item ::getId ).collect (Collectors .toList ());
106+
107+ List <Booking > lastBookings = bookingRepository .findAllLastBookings (itemIds , now );
108+ Map <Long , BookingShortDto > lastBookingMap = lastBookings .stream ()
109+ .collect (Collectors .groupingBy (
110+ b -> b .getItem ().getId (),
111+ Collectors .collectingAndThen (
112+ Collectors .toList (),
113+ list -> BookingMapper .toShortDto (list .get (0 ))
114+ )
115+ ));
116+
117+ List <Booking > nextBookings = bookingRepository .findAllNextBookings (itemIds , now );
118+ Map <Long , BookingShortDto > nextBookingMap = nextBookings .stream ()
119+ .collect (Collectors .groupingBy (
120+ b -> b .getItem ().getId (),
121+ Collectors .collectingAndThen (
122+ Collectors .toList (),
123+ list -> BookingMapper .toShortDto (list .get (0 ))
124+ )
125+ ));
126+
122127 List <Comment > comments = commentRepository .findByItemIdIn (itemIds );
123128 Map <Long , List <CommentResponseDto >> commentsByItemId = comments .stream ()
124129 .collect (Collectors .groupingBy (
@@ -127,40 +132,12 @@ public List<ItemWithBookingsDto> getItemsByOwner(Long ownerId) {
127132 ));
128133
129134 return items .stream ()
130- .map (item -> {
131- ItemWithBookingsDto dto = new ItemWithBookingsDto ();
132- dto .setId (item .getId ());
133- dto .setName (item .getName ());
134- dto .setDescription (item .getDescription ());
135- dto .setAvailable (item .getAvailable ());
136- dto .setRequestId (item .getRequest () != null ? item .getRequest ().getId () : null );
137-
138- List <Booking > lastBookings = bookingRepository .findLastBooking (item .getId (), now );
139- if (!lastBookings .isEmpty ()) {
140- Booking last = lastBookings .get (0 );
141- BookingShortDto lastDto = new BookingShortDto ();
142- lastDto .setId (last .getId ());
143- lastDto .setStart (last .getStart ());
144- lastDto .setEnd (last .getEnd ());
145- lastDto .setBookerId (last .getBooker ().getId ());
146- dto .setLastBooking (lastDto );
147- }
148-
149- List <Booking > nextBookings = bookingRepository .findNextBooking (item .getId (), now );
150- if (!nextBookings .isEmpty ()) {
151- Booking next = nextBookings .get (0 );
152- BookingShortDto nextDto = new BookingShortDto ();
153- nextDto .setId (next .getId ());
154- nextDto .setStart (next .getStart ());
155- nextDto .setEnd (next .getEnd ());
156- nextDto .setBookerId (next .getBooker ().getId ());
157- dto .setNextBooking (nextDto );
158- }
159-
160- dto .setComments (commentsByItemId .getOrDefault (item .getId (), Collections .emptyList ()));
161-
162- return dto ;
163- })
135+ .map (item -> ItemMapper .toWithBookingsDto (
136+ item ,
137+ lastBookingMap .get (item .getId ()),
138+ nextBookingMap .get (item .getId ()),
139+ commentsByItemId .getOrDefault (item .getId (), Collections .emptyList ())
140+ ))
164141 .collect (Collectors .toList ());
165142 }
166143
@@ -182,20 +159,16 @@ public CommentResponseDto addComment(Long itemId, Long authorId, CommentCreateRe
182159 .orElseThrow (() -> new NotFoundException ("Пользователь с id " + authorId + " не найден" ));
183160
184161 LocalDateTime now = LocalDateTime .now ();
185- List <Booking > completed = bookingRepository .findByBookerIdAndItemIdAndStatus (authorId , itemId , BookingStatus .APPROVED );
186- boolean hasCompletedBooking = completed .stream ()
162+ List <Booking > relevantBookings = bookingRepository .findByBookerIdAndItemIdAndStatusIn (
163+ authorId , itemId , List .of (BookingStatus .APPROVED , BookingStatus .WAITING ));
164+ boolean hasCompletedBooking = relevantBookings .stream ()
187165 .anyMatch (b -> !b .getEnd ().isAfter (now ));
188166
189167 if (!hasCompletedBooking ) {
190168 throw new ValidationException ("Пользователь не может оставить отзыв, так как не брал вещь в аренду или аренда ещё не завершена" );
191169 }
192170
193- Comment comment = new Comment ();
194- comment .setText (request .getText ());
195- comment .setItem (item );
196- comment .setAuthor (author );
197- comment .setCreated (now );
198-
171+ Comment comment = CommentMapper .fromCreateRequest (request , item , author , now );
199172 Comment saved = commentRepository .save (comment );
200173 return CommentMapper .toResponseDto (saved );
201174 }
0 commit comments