diff --git a/src/main/java/alfio/controller/api/EventPublicApiController.java b/src/main/java/alfio/controller/api/EventPublicApiController.java index 02bd511c15..aaa62c1fb3 100644 --- a/src/main/java/alfio/controller/api/EventPublicApiController.java +++ b/src/main/java/alfio/controller/api/EventPublicApiController.java @@ -84,7 +84,7 @@ public ResponseEntity handleGenericException(RuntimeException e) { @RequestMapping("/events") public ResponseEntity> getEvents(HttpServletRequest request) { List events = eventManager.getPublishedEvents().stream() - .map(e -> new EventListItem(e, request.getContextPath(), descriptionsLoader.eventDescriptions())) + .map(e -> new EventListItem(e, request.getContextPath(), descriptionsLoader.eventDescriptions().load(e))) .collect(Collectors.toList()); return new ResponseEntity<>(events, getCorsHeaders(), HttpStatus.OK); } @@ -98,7 +98,7 @@ public ResponseEntity getEvent(@PathVariable("shortName") String sh .map(c -> buildPublicCategory(c, e)) .collect(Collectors.toList()); Organization organization = organizationRepository.getById(e.getOrganizationId()); - return new ResponseEntity<>(new PublicEvent(e, request.getContextPath(), descriptionsLoader.eventDescriptions(), categories, organization), getCorsHeaders(), HttpStatus.OK); + return new ResponseEntity<>(new PublicEvent(e, request.getContextPath(), descriptionsLoader.eventDescriptions().load(e), categories, organization), getCorsHeaders(), HttpStatus.OK); }) .orElseGet(() -> new ResponseEntity<>(getCorsHeaders(), HttpStatus.NOT_FOUND)); } diff --git a/src/main/java/alfio/controller/api/admin/EventApiController.java b/src/main/java/alfio/controller/api/admin/EventApiController.java index c38dc5f04e..68728e7fbd 100644 --- a/src/main/java/alfio/controller/api/admin/EventApiController.java +++ b/src/main/java/alfio/controller/api/admin/EventApiController.java @@ -147,7 +147,7 @@ public List getAllEventsForExternal(Principal principal, HttpServ return eventManager.getActiveEvents().stream() .filter(e -> userOrganizations.contains(e.getOrganizationId())) .sorted(Comparator.comparing(e -> e.getBegin().withZoneSameInstant(ZoneId.systemDefault()))) - .map(s -> new EventListItem(s, request.getContextPath(), descriptionsLoader.eventDescriptions())) + .map(s -> new EventListItem(s, request.getContextPath(), descriptionsLoader.eventDescriptions().load(s))) .collect(toList()); } diff --git a/src/main/java/alfio/controller/api/support/EventListItem.java b/src/main/java/alfio/controller/api/support/EventListItem.java index 51f8f943f6..4b85c85d91 100644 --- a/src/main/java/alfio/controller/api/support/EventListItem.java +++ b/src/main/java/alfio/controller/api/support/EventListItem.java @@ -30,10 +30,10 @@ public class EventListItem { private final String requestContextPath; private final List eventDescriptions; - public EventListItem(Event event, String requestContextPath, DataLoader eventDescriptionsLoader) { + public EventListItem(Event event, String requestContextPath, List eventDescriptions) { this.event = event; this.requestContextPath = requestContextPath; - this.eventDescriptions = eventDescriptionsLoader.load(event); + this.eventDescriptions = eventDescriptions; } public String getImageUrl() { diff --git a/src/main/java/alfio/controller/api/support/PublicEvent.java b/src/main/java/alfio/controller/api/support/PublicEvent.java index 41429de58d..01a993cced 100644 --- a/src/main/java/alfio/controller/api/support/PublicEvent.java +++ b/src/main/java/alfio/controller/api/support/PublicEvent.java @@ -29,8 +29,8 @@ public class PublicEvent extends EventListItem { private final List categories; private final Organization organization; - public PublicEvent(Event event, String requestContextPath, DataLoader eventDescriptionsLoader, List categories, Organization organization) { - super(event, requestContextPath, eventDescriptionsLoader); + public PublicEvent(Event event, String requestContextPath, List eventDescriptions, List categories, Organization organization) { + super(event, requestContextPath, eventDescriptions); this.categories = categories; this.organization = organization; } diff --git a/src/main/java/alfio/controller/api/user/RestEventApiController.java b/src/main/java/alfio/controller/api/user/RestEventApiController.java index e49ed0b1a6..9200540da2 100644 --- a/src/main/java/alfio/controller/api/user/RestEventApiController.java +++ b/src/main/java/alfio/controller/api/user/RestEventApiController.java @@ -92,7 +92,7 @@ public RestEventApiController(EventManager eventManager, @RequestMapping("events") public List listEvents(HttpServletRequest request) { return eventManager.getPublishedEvents().stream() - .map(e -> new EventListItem(e, request.getContextPath(), descriptionsLoader.eventDescriptions())) + .map(e -> new EventListItem(e, request.getContextPath(), descriptionsLoader.eventDescriptions().load(e))) .collect(Collectors.toList()); } @@ -108,7 +108,7 @@ public ResponseEntity showEvent(@PathVariable("shortName") String s .collect(Collectors.toList()); Organization organization = organizationRepository.getById(e.getOrganizationId()); - return new ResponseEntity<>(new PublicEvent(e, request.getContextPath(), descriptionsLoader.eventDescriptions(), categories, organization), HttpStatus.OK); + return new ResponseEntity<>(new PublicEvent(e, request.getContextPath(), descriptionsLoader.eventDescriptions().load(e), categories, organization), HttpStatus.OK); }).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); } diff --git a/src/main/java/alfio/controller/api/v2/user/EventApiV2Controller.java b/src/main/java/alfio/controller/api/v2/user/EventApiV2Controller.java new file mode 100644 index 0000000000..413de4506e --- /dev/null +++ b/src/main/java/alfio/controller/api/v2/user/EventApiV2Controller.java @@ -0,0 +1,63 @@ +/** + * This file is part of alf.io. + * + * alf.io is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * alf.io is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with alf.io. If not, see . + */ +package alfio.controller.api.v2.user; + +import alfio.controller.api.support.EventListItem; +import alfio.manager.EventManager; +import alfio.model.Event; +import alfio.repository.EventDescriptionRepository; +import lombok.AllArgsConstructor; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.stream.Collectors; + + +@RestController +@RequestMapping("/api/v2/public/") +@AllArgsConstructor +public class EventApiV2Controller { + + private final EventManager eventManager; + private final EventDescriptionRepository eventDescriptionRepository; + + //TODO: copy EventListItem to v2.model + @RequestMapping("events") + public ResponseEntity> listEvents(HttpServletRequest request) { + var events = eventManager.getPublishedEvents(); + var eventIds = events.stream().map(Event::getId).collect(Collectors.toList()); + var descriptionsByEventId = eventDescriptionRepository.findByEventIdsAsMap(eventIds); + + var res = events.stream() + .map(ev -> new EventListItem(ev, request.getContextPath(), descriptionsByEventId.get(ev.getId()))) + .collect(Collectors.toList()); + + return new ResponseEntity<>(res, getCorsHeaders(), HttpStatus.OK); + } + + + private static HttpHeaders getCorsHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.add("Access-Control-Allow-Origin", "*"); + return headers; + } +} diff --git a/src/main/java/alfio/repository/EventDescriptionRepository.java b/src/main/java/alfio/repository/EventDescriptionRepository.java index db2f7f598b..c37931c46b 100644 --- a/src/main/java/alfio/repository/EventDescriptionRepository.java +++ b/src/main/java/alfio/repository/EventDescriptionRepository.java @@ -21,9 +21,11 @@ import ch.digitalfondue.npjt.Query; import ch.digitalfondue.npjt.QueryRepository; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Function; import java.util.stream.Collectors; @QueryRepository @@ -32,10 +34,17 @@ public interface EventDescriptionRepository { @Query("select * from event_description_text where event_id_fk = :eventId") List findByEventId(@Bind("eventId") int eventId); + @Query("select * from event_description_text where event_id_fk in (:eventIds)") + List findByEventIds(@Bind("eventIds") Collection eventIds); + default Map findByEventIdAsMap(int eventId) { return findByEventId(eventId).stream().collect(Collectors.toMap(EventDescription::getLocale, EventDescription::getDescription)); } + default Map> findByEventIdsAsMap(Collection eventIds) { + return findByEventIds(eventIds).stream().collect(Collectors.groupingBy(EventDescription::getEventId)); + } + @Query("select description from event_description_text where event_id_fk = :eventId and type = :type and locale = :locale") Optional findDescriptionByEventIdTypeAndLocale(@Bind("eventId") int eventId, @Bind("type") EventDescription.EventDescriptionType type, @Bind("locale") String locale);