Skip to content

Commit

Permalink
sponsor scanning API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Jan 17, 2016
1 parent 246b69c commit da07708
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 57 deletions.
22 changes: 19 additions & 3 deletions src/main/java/alfio/controller/api/AttendeeApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
package alfio.controller.api;

import alfio.manager.AttendeeManager;
import alfio.manager.support.TicketAndCheckInResult;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;
import java.security.Principal;

@RestController
@RequestMapping("/api/attendees")
Expand Down Expand Up @@ -52,8 +56,20 @@ public ResponseEntity<String> handleGenericException(RuntimeException e) {


@RequestMapping(value = "/sponsor-scan", method = RequestMethod.POST)
public ResponseEntity<ZonedDateTime> scanBadge(@RequestParam("eventId") int eventId, @RequestParam("ticketIdentifier") String ticketIdentifier) {
return attendeeManager.registerSponsorScan(eventId, ticketIdentifier).map(z -> new ResponseEntity<>(z, HttpStatus.CONFLICT)).orElseGet(() -> new ResponseEntity<>(HttpStatus.OK));
public ResponseEntity<TicketAndCheckInResult> scanBadge(@RequestBody SponsorScanRequest request, Principal principal) {
return new ResponseEntity<>(attendeeManager.registerSponsorScan(request.eventName, request.ticketIdentifier, principal.getName()), HttpStatus.OK);
}

@Getter
public static class SponsorScanRequest {
private final String eventName;
private final String ticketIdentifier;

@JsonCreator
public SponsorScanRequest(@JsonProperty("eventName") String eventName, @JsonProperty("ticketIdentifier") String ticketIdentifier) {
this.eventName = eventName;
this.ticketIdentifier = ticketIdentifier;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package alfio.controller.api.admin;

import alfio.manager.CheckInManager;
import alfio.manager.CheckInManager.TicketAndCheckInResult;
import alfio.manager.support.TicketAndCheckInResult;
import alfio.model.FullTicketInfo;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/alfio/manager/AttendeeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
*/
package alfio.manager;

import alfio.manager.support.CheckInStatus;
import alfio.manager.support.DefaultCheckInResult;
import alfio.manager.support.TicketAndCheckInResult;
import alfio.model.Event;
import alfio.model.Ticket;
import alfio.repository.EventRepository;
import alfio.repository.SponsorScanRepository;
import alfio.repository.TicketRepository;
import alfio.repository.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -35,22 +39,39 @@ public class AttendeeManager {
private final SponsorScanRepository sponsorScanRepository;
private final EventRepository eventRepository;
private final TicketRepository ticketRepository;
private final UserRepository userRepository;

@Autowired
public AttendeeManager(SponsorScanRepository sponsorScanRepository, EventRepository eventRepository, TicketRepository ticketRepository) {
public AttendeeManager(SponsorScanRepository sponsorScanRepository,
EventRepository eventRepository,
TicketRepository ticketRepository,
UserRepository userRepository) {
this.sponsorScanRepository = sponsorScanRepository;
this.eventRepository = eventRepository;
this.ticketRepository = ticketRepository;
this.userRepository = userRepository;
}

public Optional<ZonedDateTime> registerSponsorScan(int eventId, String ticketUid) {
Event event = optionally(() -> eventRepository.findById(eventId)).orElseThrow(IllegalArgumentException::new);
Ticket ticket = optionally(() -> ticketRepository.findByUUID(ticketUid)).orElseThrow(IllegalArgumentException::new);
Optional<ZonedDateTime> existingRegistration = sponsorScanRepository.getRegistrationTimestamp(event.getId(), ticket.getUuid());
public TicketAndCheckInResult registerSponsorScan(String eventShortName, String ticketUid, String username) {
int userId = userRepository.getByUsername(username).getId();
Optional<Event> maybeEvent = eventRepository.findOptionalByShortName(eventShortName);
if(!maybeEvent.isPresent()) {
return new TicketAndCheckInResult(null, new DefaultCheckInResult(CheckInStatus.EVENT_NOT_FOUND, "event not found"));
}
Event event = maybeEvent.get();
Optional<Ticket> maybeTicket = optionally(() -> ticketRepository.findByUUID(ticketUid));
if(!maybeTicket.isPresent()) {
return new TicketAndCheckInResult(null, new DefaultCheckInResult(CheckInStatus.TICKET_NOT_FOUND, "ticket not found"));
}
Ticket ticket = maybeTicket.get();
if(ticket.getStatus() != Ticket.TicketStatus.CHECKED_IN) {
return new TicketAndCheckInResult(ticket, new DefaultCheckInResult(CheckInStatus.INVALID_TICKET_STATE, "not checked-in"));
}
Optional<ZonedDateTime> existingRegistration = sponsorScanRepository.getRegistrationTimestamp(userId, event.getId(), ticket.getId());
if(!existingRegistration.isPresent()) {
sponsorScanRepository.insert(ZonedDateTime.now(event.getZoneId()), event.getId(), ticket.getUuid());
return Optional.empty();
sponsorScanRepository.insert(userId, ZonedDateTime.now(event.getZoneId()), event.getId(), ticket.getId());
}
return existingRegistration.map(d -> d.withZoneSameInstant(event.getZoneId()));
return new TicketAndCheckInResult(ticket, new DefaultCheckInResult(CheckInStatus.SUCCESS, "success"));
}

}
35 changes: 5 additions & 30 deletions src/main/java/alfio/manager/CheckInManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
*/
package alfio.manager;

import alfio.manager.support.CheckInStatus;
import alfio.manager.support.DefaultCheckInResult;
import alfio.manager.support.OnSitePaymentResult;
import alfio.manager.support.TicketAndCheckInResult;
import alfio.model.Event;
import alfio.model.FullTicketInfo;
import alfio.model.Ticket;
import alfio.model.Ticket.TicketStatus;
import alfio.repository.EventRepository;
import alfio.repository.TicketRepository;
import alfio.util.MonetaryUtil;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
Expand All @@ -32,21 +35,17 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

import static alfio.manager.CheckInManager.CheckInStatus.*;
import static alfio.manager.support.CheckInStatus.*;
import static alfio.util.OptionalWrapper.optionally;

@Component
@Transactional
@Log4j2
public class CheckInManager {

public enum CheckInStatus {
EVENT_NOT_FOUND, TICKET_NOT_FOUND, EMPTY_TICKET_CODE, INVALID_TICKET_CODE, INVALID_TICKET_STATE, ALREADY_CHECK_IN, MUST_PAY, OK_READY_TO_BE_CHECKED_IN, SUCCESS
}
private final TicketRepository ticketRepository;
private final EventRepository eventRepository;

Expand Down Expand Up @@ -171,28 +170,4 @@ private TicketAndCheckInResult extractStatus(Optional<Event> maybeEvent, Optiona
return new TicketAndCheckInResult(ticket, new DefaultCheckInResult(OK_READY_TO_BE_CHECKED_IN, "Ready to be checked in"));
}

@Data
public static class TicketAndCheckInResult {
private final Ticket ticket;
private final CheckInResult result;
}

@Data
public static class DefaultCheckInResult implements CheckInResult {
private final CheckInStatus status;
private final String message;
}

@Data
public static class OnSitePaymentResult implements CheckInResult {
private final CheckInStatus status;
private final String message;
private final BigDecimal dueAmount;
private final String currency;
}

public interface CheckInResult {
CheckInStatus getStatus();
}

}
21 changes: 21 additions & 0 deletions src/main/java/alfio/manager/support/CheckInResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.manager.support;

public interface CheckInResult {
CheckInStatus getStatus();
}
21 changes: 21 additions & 0 deletions src/main/java/alfio/manager/support/CheckInStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.manager.support;

public enum CheckInStatus {
EVENT_NOT_FOUND, TICKET_NOT_FOUND, EMPTY_TICKET_CODE, INVALID_TICKET_CODE, INVALID_TICKET_STATE, ALREADY_CHECK_IN, MUST_PAY, OK_READY_TO_BE_CHECKED_IN, SUCCESS
}
25 changes: 25 additions & 0 deletions src/main/java/alfio/manager/support/DefaultCheckInResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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.manager.support;

import lombok.Data;

@Data
public class DefaultCheckInResult implements CheckInResult {
private final CheckInStatus status;
private final String message;
}
29 changes: 29 additions & 0 deletions src/main/java/alfio/manager/support/OnSitePaymentResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.manager.support;

import lombok.Data;

import java.math.BigDecimal;

@Data
public class OnSitePaymentResult implements CheckInResult {
private final CheckInStatus status;
private final String message;
private final BigDecimal dueAmount;
private final String currency;
}
26 changes: 26 additions & 0 deletions src/main/java/alfio/manager/support/TicketAndCheckInResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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.manager.support;

import alfio.model.Ticket;
import lombok.Data;

@Data
public class TicketAndCheckInResult {
private final Ticket ticket;
private final CheckInResult result;
}
8 changes: 4 additions & 4 deletions src/main/java/alfio/repository/SponsorScanRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
@QueryRepository
public interface SponsorScanRepository {

@Query("select creation from sponsor_scan where event_id = :eventId and ticket_id = :ticketId")
Optional<ZonedDateTime> getRegistrationTimestamp(@Bind("eventId") int eventId, @Bind("ticketId") String ticketId);
@Query("select creation from sponsor_scan where user_id = :userId and event_id = :eventId and ticket_id = :ticketId")
Optional<ZonedDateTime> getRegistrationTimestamp(@Bind("userId") int userId, @Bind("eventId") int eventId, @Bind("ticketId") int ticketId);

@Query("insert into sponsor_scan (creation, event_id, ticket_id) values(:creation, :eventId, :ticketId)")
int insert(@Bind("creation") ZonedDateTime creation, @Bind("eventId") int eventId, @Bind("ticketId") String ticketId);
@Query("insert into sponsor_scan (user_id, creation, event_id, ticket_id) values(:userId, :creation, :eventId, :ticketId)")
int insert(@Bind("userId") int userId, @Bind("creation") ZonedDateTime creation, @Bind("eventId") int eventId, @Bind("ticketId") int ticketId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- 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/>.
--

alter table sponsor_scan add column user_id integer DEFAULT 0 NOT null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- 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/>.
--

alter table sponsor_scan add column user_id integer NOT NULL DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- 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/>.
--

alter table sponsor_scan add column user_id integer not NULL DEFAULT 0;
Loading

0 comments on commit da07708

Please sign in to comment.