Skip to content

Commit

Permalink
#588 initial stubs for event api v2 controller
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Mar 7, 2019
1 parent d6712f8 commit 666d166
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ResponseEntity<String> handleGenericException(RuntimeException e) {
@RequestMapping("/events")
public ResponseEntity<List<EventListItem>> getEvents(HttpServletRequest request) {
List<EventListItem> 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);
}
Expand All @@ -98,7 +98,7 @@ public ResponseEntity<PublicEvent> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public List<EventListItem> 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());
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/alfio/controller/api/support/EventListItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class EventListItem {
private final String requestContextPath;
private final List<EventDescription> eventDescriptions;

public EventListItem(Event event, String requestContextPath, DataLoader<Event, EventDescription> eventDescriptionsLoader) {
public EventListItem(Event event, String requestContextPath, List<EventDescription> eventDescriptions) {
this.event = event;
this.requestContextPath = requestContextPath;
this.eventDescriptions = eventDescriptionsLoader.load(event);
this.eventDescriptions = eventDescriptions;
}

public String getImageUrl() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/alfio/controller/api/support/PublicEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class PublicEvent extends EventListItem {
private final List<PublicCategory> categories;
private final Organization organization;

public PublicEvent(Event event, String requestContextPath, DataLoader<Event, EventDescription> eventDescriptionsLoader, List<PublicCategory> categories, Organization organization) {
super(event, requestContextPath, eventDescriptionsLoader);
public PublicEvent(Event event, String requestContextPath, List<EventDescription> eventDescriptions, List<PublicCategory> categories, Organization organization) {
super(event, requestContextPath, eventDescriptions);
this.categories = categories;
this.organization = organization;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public RestEventApiController(EventManager eventManager,
@RequestMapping("events")
public List<EventListItem> 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());
}

Expand All @@ -108,7 +108,7 @@ public ResponseEntity<PublicEvent> 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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<List<EventListItem>> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,10 +34,17 @@ public interface EventDescriptionRepository {
@Query("select * from event_description_text where event_id_fk = :eventId")
List<EventDescription> findByEventId(@Bind("eventId") int eventId);

@Query("select * from event_description_text where event_id_fk in (:eventIds)")
List<EventDescription> findByEventIds(@Bind("eventIds") Collection<Integer> eventIds);

default Map<String, String> findByEventIdAsMap(int eventId) {
return findByEventId(eventId).stream().collect(Collectors.toMap(EventDescription::getLocale, EventDescription::getDescription));
}

default Map<Integer, List<EventDescription>> findByEventIdsAsMap(Collection<Integer> 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<String> findDescriptionByEventIdTypeAndLocale(@Bind("eventId") int eventId, @Bind("type") EventDescription.EventDescriptionType type, @Bind("locale") String locale);

Expand Down

0 comments on commit 666d166

Please sign in to comment.