Skip to content

Commit

Permalink
#588 new strategy for api v2
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Mar 29, 2019
1 parent 4109d42 commit 24bc7cf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/alfio/controller/EventController.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public void calendar(@PathVariable("eventName") String eventName,

@RequestMapping(value = "/event/{eventName}/reserve-tickets", method = { RequestMethod.POST, RequestMethod.GET, RequestMethod.HEAD })
public String reserveTicket(@PathVariable("eventName") String eventName,
@ModelAttribute ReservationForm reservation, BindingResult bindingResult, Model model,
@ModelAttribute ReservationForm reservation, BindingResult bindingResult,
ServletWebRequest request, RedirectAttributes redirectAttributes, Locale locale) {

return eventRepository.findOptionalByShortName(eventName).map(event -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,64 @@
*/
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 alfio.controller.EventController;
import alfio.controller.form.ReservationForm;
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 org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Locale;


@RestController
@RequestMapping("/api/v2/public/")
@AllArgsConstructor
public class EventApiV2Controller {

private final EventManager eventManager;
private final EventDescriptionRepository eventDescriptionRepository;
private final EventController eventController;

//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());
@GetMapping("events")
public ResponseEntity<Model> listEvents(Model model, Locale locale, HttpServletRequest request) {
eventController.listEvents(model, locale);
return new ResponseEntity<>(model, getCorsHeaders(), HttpStatus.OK);
}

@GetMapping("/event/{eventName}")
public ResponseEntity<Model> getEvent(@PathVariable("eventName") String eventName,
Model model, HttpServletRequest request, Locale locale) {
if ("/event/show-event".equals(eventController.showEvent(eventName, model, request, locale))) {
return new ResponseEntity<>(model, getCorsHeaders(), HttpStatus.OK);
} else {
return ResponseEntity.notFound().headers(getCorsHeaders()).build();
}
}

@PostMapping(value = "/event/{eventName}/reserve-tickets")
public ResponseEntity<Model> reserveTicket(@PathVariable("eventName") String eventName,
@RequestBody ReservationForm reservation,
BindingResult bindingResult,
ServletWebRequest request,
RedirectAttributes redirectAttributes,
Locale locale) {

String redirectResult = eventController.reserveTicket(eventName, reservation, bindingResult, request, redirectAttributes, locale);

if (bindingResult.hasErrors()) {
return new ResponseEntity<>(redirectAttributes, getCorsHeaders(), HttpStatus.OK);
} else {
String reservationIdentifier = redirectResult.substring(redirectResult.length()).replace("/book", "");
redirectAttributes.addAttribute("reservationIdentifier", reservationIdentifier);
return new ResponseEntity<>(redirectAttributes, getCorsHeaders(), HttpStatus.OK);
}

return new ResponseEntity<>(res, getCorsHeaders(), HttpStatus.OK);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,15 @@ private String reserveTicket(String eventName) {
request.setMethod("POST");
ServletWebRequest servletWebRequest = new ServletWebRequest(request);
BindingResult bindingResult = new BeanPropertyBindingResult(reservationForm, "reservation");
Model model = new BindingAwareModelMap();

RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
TicketReservationModification ticketReservation = new TicketReservationModification();
ticketReservation.setAmount(1);
ticketReservation.setTicketCategoryId(ticketCategoryRepository.findByEventId(event.getId()).stream().findFirst().map(TicketCategory::getId).orElseThrow(IllegalStateException::new));
reservationForm.setReservation(Collections.singletonList(ticketReservation));


return eventController.reserveTicket(eventName, reservationForm, bindingResult, model, servletWebRequest, redirectAttributes, Locale.ENGLISH);
return eventController.reserveTicket(eventName, reservationForm, bindingResult, servletWebRequest, redirectAttributes, Locale.ENGLISH);
}

}

0 comments on commit 24bc7cf

Please sign in to comment.