77import org .springframework .data .domain .PageRequest ;
88import org .springframework .data .domain .Pageable ;
99import org .springframework .data .domain .Sort ;
10+ import org .springframework .data .jpa .domain .Specification ;
1011import org .springframework .stereotype .Service ;
1112import org .springframework .transaction .annotation .Transactional ;
1213import ru .practicum .ewm .category .model .Category ;
1617import ru .practicum .ewm .event .model .Event ;
1718import ru .practicum .ewm .event .model .EventState ;
1819import ru .practicum .ewm .event .repository .EventRepository ;
20+ import ru .practicum .ewm .event .repository .EventSpecifications ;
1921import ru .practicum .ewm .event .util .SortType ;
2022import ru .practicum .ewm .exception .ConflictException ;
2123import ru .practicum .ewm .exception .NotFoundException ;
@@ -112,15 +114,16 @@ public List<EventFullDto> getEventsForAdmin(AdminEventSearchParams params) {
112114 }
113115
114116 Pageable pageable = PageRequest .of (from / size , size , Sort .by ("id" ));
115- Page <Event > page = eventRepository .searchEventsAdmin (
116- params .getUsers (),
117- params .getStates (),
118- params .getCategories (),
119- params .getRangeStart (),
120- params .getRangeEnd (),
121- pageable );
122117
118+ Specification <Event > spec = Specification
119+ .where (EventSpecifications .hasInitiators (params .getUsers ()))
120+ .and (EventSpecifications .hasStates (params .getStates ()))
121+ .and (EventSpecifications .hasCategories (params .getCategories ()))
122+ .and (EventSpecifications .eventDateBetween (params .getRangeStart (), params .getRangeEnd ()));
123+
124+ Page <Event > page = eventRepository .findAll (spec , pageable );
123125 List <Event > events = page .getContent ();
126+
124127 List <Long > eventIds = events .stream ().map (Event ::getId ).collect (Collectors .toList ());
125128 Map <Long , Long > confirmedRequestsMap = eventRepository .countConfirmedRequestsBatch (eventIds );
126129
@@ -196,16 +199,19 @@ public List<EventShortDto> getEventsForPublic(PublicEventSearchParams params, Ht
196199 log .warn ("Не удалось отправить статистику: {}" , e .getMessage ());
197200 }
198201
199- Pageable pageable = PageRequest .of (from / size , size , Sort .by ("eventDate" ).ascending ());
200- Page <Event > page = eventRepository .searchEventsPublic (
201- params .getText (),
202- params .getCategories (),
203- params .getPaid (),
204- params .getRangeStart (),
205- params .getRangeEnd (),
206- pageable );
202+ Sort sort = Sort .by ("eventDate" ).ascending ();
203+ Pageable pageable = PageRequest .of (from / size , size , sort );
207204
205+ Specification <Event > spec = Specification
206+ .where (EventSpecifications .isPublished ())
207+ .and (EventSpecifications .textContains (params .getText ()))
208+ .and (EventSpecifications .hasCategories (params .getCategories ()))
209+ .and (EventSpecifications .paidEquals (params .getPaid ()))
210+ .and (EventSpecifications .eventDateBetween (params .getRangeStart (), params .getRangeEnd ()));
211+
212+ Page <Event > page = eventRepository .findAll (spec , pageable );
208213 List <Event > events = page .getContent ();
214+
209215 Map <Long , Long > confirmedRequestsMap = getConfirmedRequestsMap (events );
210216
211217 if (params .getOnlyAvailable ()) {
@@ -226,10 +232,10 @@ public List<EventShortDto> getEventsForPublic(PublicEventSearchParams params, Ht
226232 confirmedRequestsMap .getOrDefault (event .getId (), 0L )))
227233 .collect (Collectors .toList ());
228234
229- SortType sort = params .getSort ();
230- if (sort == SortType .VIEWS ) {
235+ SortType sortParam = params .getSort ();
236+ if (sortParam == SortType .VIEWS ) {
231237 result .sort (Comparator .comparing (EventShortDto ::getViews ).reversed ());
232- } else if (sort == SortType .EVENT_DATE ) {
238+ } else if (sortParam == SortType .EVENT_DATE ) {
233239 result .sort (Comparator .comparing (EventShortDto ::getEventDate ));
234240 }
235241
0 commit comments