Skip to content

Commit

Permalink
#71 - order ticket categories by expiration date when searching for f…
Browse files Browse the repository at this point in the history
…irst/last category
  • Loading branch information
cbellone committed Sep 4, 2015
1 parent c41fefc commit b0eeab1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/java/alfio/manager/WaitingQueueManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void preReserveTickets(Event event, int ticketsNeeded, int eventId, int
.map(tc -> Pair.of(determineAvailableSeats(tc, eventWithStatistics), tc))
.collect(new PreReservedTicketDistributor(toBeGenerated));
MapSqlParameterSource[] candidates = collectedTickets.stream()
.flatMap(p -> ticketRepository.selectFreeTicketsForPreReservation(eventId, p.getValue(), p.getKey()).stream())
.flatMap(p -> ticketRepository.selectFreeTicketsForPreReservation(eventId, p.getKey()).stream())
.map(id -> new MapSqlParameterSource().addValue("id", id))
.toArray(MapSqlParameterSource[]::new);
jdbc.batchUpdate(ticketRepository.preReserveTicket(), candidates);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/alfio/repository/TicketRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ int updateOptionalTicketInfo(@Bind("ticketIdentifier") String ticketIdentifier,
List<Ticket> selectWaitingTicketsForUpdate(@Bind("eventId") int eventId, @Bind("status") String status, @Bind("amount") int amount);

@Query("select id from ticket where status = 'FREE' and event_id = :eventId order by id limit :amount for update")
List<Integer> selectFreeTicketsForPreReservation(@Bind("eventId") int eventId, @Bind("categoryId") int categoryId, @Bind("amount") int amount);
List<Integer> selectFreeTicketsForPreReservation(@Bind("eventId") int eventId, @Bind("amount") int amount);

@Query("select count(*) from ticket where status = 'PRE_RESERVED'")
Integer countPreReservedTickets(@Bind("eventId") int eventId);
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/alfio/util/EventUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@

import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
Expand All @@ -56,13 +53,18 @@ public static boolean displayWaitingQueueForm(Event event, List<SaleableTicketCa
}

private static Optional<SaleableTicketCategory> findLastCategory(List<SaleableTicketCategory> categories) {
return Optional.ofNullable(categories).filter(IS_EMPTY.negate()).map(l -> l.get(l.size() - 1));
return sortCategories(categories, (c1, c2) -> c2.getUtcExpiration().compareTo(c1.getUtcExpiration())).findFirst();
}

private static Optional<SaleableTicketCategory> findFirstCategory(List<SaleableTicketCategory> categories) {
return Optional.ofNullable(categories).filter(IS_EMPTY.negate()).flatMap(l -> l.stream().findFirst());
return sortCategories(categories, (c1, c2) -> c1.getUtcExpiration().compareTo(c2.getUtcExpiration())).findFirst();
}

private static Stream<SaleableTicketCategory> sortCategories(List<SaleableTicketCategory> categories, Comparator<SaleableTicketCategory> comparator) {
return Optional.ofNullable(categories).orElse(Collections.emptyList()).stream().sorted(comparator);
}


private static boolean noTicketAvailable(List<SaleableTicketCategory> categories) {
return categories.stream().noneMatch(c -> c.getAvailableTickets() > 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,26 @@ public void testPreRegistration() {

@Test
public void testSoldOut() throws InterruptedException {
List<TicketCategoryModification> categories = Collections.singletonList(
new TicketCategoryModification(null, "default", AVAILABLE_SEATS,
new DateTimeModification(LocalDate.now().minusDays(1), LocalTime.now()),
new DateTimeModification(LocalDate.now().plusDays(2), LocalTime.now()),
DESCRIPTION, BigDecimal.ZERO, false, "", true));
List<TicketCategoryModification> categories = Arrays.asList(
new TicketCategoryModification(null, "default", AVAILABLE_SEATS -1,
new DateTimeModification(LocalDate.now().minusDays(1), LocalTime.now()),
new DateTimeModification(LocalDate.now().plusDays(2), LocalTime.now()),
DESCRIPTION, BigDecimal.ZERO, false, "", true),
new TicketCategoryModification(null, "unbounded", 0,
new DateTimeModification(LocalDate.now().minusDays(1), LocalTime.now()),
new DateTimeModification(LocalDate.now().plusDays(2), LocalTime.now()),
DESCRIPTION, BigDecimal.ZERO, false, "", false));

Pair<Event, String> pair = initEvent(categories, organizationRepository, userManager, eventManager);
Event event = pair.getKey();
TicketCategory ticketCategory = eventManager.loadTicketCategories(event).get(0);
List<Integer> reserved = ticketRepository.selectFreeTicketsForPreReservation(event.getId(), ticketCategory.getId(), 20);
List<TicketCategory> ticketCategories = eventManager.loadTicketCategories(event);
TicketCategory bounded = ticketCategories.get(0);
TicketCategory unbounded = ticketCategories.get(1);
List<Integer> reserved = ticketRepository.selectFreeTicketsForPreReservation(event.getId(), 20);
String reservationId = UUID.randomUUID().toString();
ticketReservationRepository.createNewReservation(reservationId, DateUtils.addHours(new Date(), 1), null);
ticketRepository.reserveTickets(reservationId, reserved, ticketCategory.getId(), Locale.ITALIAN.getLanguage());
ticketRepository.reserveTickets(reservationId, reserved.subList(0, 19), bounded.getId(), Locale.ITALIAN.getLanguage());
ticketRepository.reserveTickets(reservationId, reserved.subList(19, 20), unbounded.getId(), Locale.ITALIAN.getLanguage());
ticketRepository.updateTicketsStatusWithReservationId(reservationId, Ticket.TicketStatus.ACQUIRED.name());

//sold-out
Expand Down
Loading

0 comments on commit b0eeab1

Please sign in to comment.