Search in sources :

Example 1 with AdditionalService

use of alfio.model.AdditionalService in project alf.io by alfio-event.

the class AdditionalServiceApiController method insert.

@PostMapping(value = "/event/{eventId}/additional-services")
@Transactional
public ResponseEntity<EventModification.AdditionalService> insert(@PathVariable("eventId") int eventId, @RequestBody EventModification.AdditionalService additionalService, BindingResult bindingResult) {
    ValidationResult validationResult = Validator.validateAdditionalService(additionalService, bindingResult);
    Validate.isTrue(validationResult.isSuccess(), "validation failed");
    return eventRepository.findOptionalById(eventId).map(event -> ResponseEntity.ok(eventManager.insertAdditionalService(event, additionalService))).orElseThrow(IllegalArgumentException::new);
}
Also used : PriceContainer(alfio.model.PriceContainer) ValidationResult(alfio.model.result.ValidationResult) ExportUtils(alfio.util.ExportUtils) RequiredArgsConstructor(lombok.RequiredArgsConstructor) BindingResult(org.springframework.validation.BindingResult) EventModification(alfio.model.modification.EventModification) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) BigDecimal(java.math.BigDecimal) Map(java.util.Map) AdditionalServiceManager(alfio.manager.AdditionalServiceManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) EventRepository(alfio.repository.EventRepository) Validator(alfio.util.Validator) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) EventManager(alfio.manager.EventManager) AdditionalService(alfio.model.AdditionalService) Validate(org.apache.commons.lang3.Validate) Principal(java.security.Principal) Stream(java.util.stream.Stream) MonetaryUtil(alfio.util.MonetaryUtil) DateTimeFormatter(java.time.format.DateTimeFormatter) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) Event(alfio.model.Event) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) ValidationResult(alfio.model.result.ValidationResult) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with AdditionalService

use of alfio.model.AdditionalService in project alf.io by alfio-event.

the class ReservationForm method validate.

public Optional<Pair<List<TicketReservationWithOptionalCodeModification>, List<ASReservationWithOptionalCodeModification>>> validate(Errors bindingResult, TicketReservationManager tickReservationManager, AdditionalServiceRepository additionalServiceRepository, EventManager eventManager, Event event) {
    int selectionCount = ticketSelectionCount();
    if (selectionCount <= 0) {
        bindingResult.reject(ErrorsCode.STEP_1_SELECT_AT_LEAST_ONE);
        return Optional.empty();
    }
    List<Pair<TicketReservationModification, Integer>> maxTicketsByTicketReservation = selected().stream().map(r -> Pair.of(r, tickReservationManager.maxAmountOfTicketsForCategory(event.getOrganizationId(), event.getId(), r.getTicketCategoryId()))).collect(toList());
    Optional<Pair<TicketReservationModification, Integer>> error = maxTicketsByTicketReservation.stream().filter(p -> p.getKey().getAmount() > p.getValue()).findAny();
    if (error.isPresent()) {
        bindingResult.reject(ErrorsCode.STEP_1_OVER_MAXIMUM, new Object[] { error.get().getValue() }, null);
        return Optional.empty();
    }
    final List<TicketReservationModification> categories = selected();
    final List<AdditionalServiceReservationModification> additionalServices = selectedAdditionalServices();
    final boolean validCategorySelection = categories.stream().allMatch(c -> {
        TicketCategory tc = eventManager.getTicketCategoryById(c.getTicketCategoryId(), event.getId());
        return OptionalWrapper.optionally(() -> eventManager.findEventByTicketCategory(tc)).isPresent();
    });
    final boolean validAdditionalServiceSelected = additionalServices.stream().allMatch(asm -> {
        AdditionalService as = eventManager.getAdditionalServiceById(asm.getAdditionalServiceId(), event.getId());
        ZonedDateTime now = ZonedDateTime.now(event.getZoneId());
        return as.getInception(event.getZoneId()).isBefore(now) && as.getExpiration(event.getZoneId()).isAfter(now) && asm.getQuantity() >= 0 && ((as.isFixPrice() && asm.isQuantityValid(as, selectionCount)) || (!as.isFixPrice() && asm.getAmount() != null && asm.getAmount().compareTo(BigDecimal.ZERO) >= 0)) && OptionalWrapper.optionally(() -> eventManager.findEventByAdditionalService(as)).isPresent();
    });
    if (!validCategorySelection || !validAdditionalServiceSelected) {
        bindingResult.reject(ErrorsCode.STEP_1_TICKET_CATEGORY_MUST_BE_SALEABLE);
        return Optional.empty();
    }
    List<TicketReservationWithOptionalCodeModification> res = new ArrayList<>();
    // 
    Optional<SpecialPrice> specialCode = Optional.ofNullable(StringUtils.trimToNull(promoCode)).flatMap((trimmedCode) -> tickReservationManager.getSpecialPriceByCode(trimmedCode));
    // 
    final ZonedDateTime now = ZonedDateTime.now(event.getZoneId());
    maxTicketsByTicketReservation.forEach((pair) -> validateCategory(bindingResult, tickReservationManager, eventManager, event, pair.getRight(), res, specialCode, now, pair.getLeft()));
    return bindingResult.hasErrors() ? Optional.empty() : Optional.of(Pair.of(res, additionalServices.stream().map(as -> new ASReservationWithOptionalCodeModification(as, specialCode)).collect(Collectors.toList())));
}
Also used : Errors(org.springframework.validation.Errors) TicketReservationModification(alfio.model.modification.TicketReservationModification) ZonedDateTime(java.time.ZonedDateTime) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Pair(org.apache.commons.lang3.tuple.Pair) ASReservationWithOptionalCodeModification(alfio.model.modification.ASReservationWithOptionalCodeModification) AdditionalServiceRepository(alfio.repository.AdditionalServiceRepository) TicketReservationWithOptionalCodeModification(alfio.model.modification.TicketReservationWithOptionalCodeModification) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) ErrorsCode(alfio.util.ErrorsCode) OptionalWrapper(alfio.util.OptionalWrapper) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) TicketReservationManager(alfio.manager.TicketReservationManager) TicketCategory(alfio.model.TicketCategory) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) EventManager(alfio.manager.EventManager) AdditionalService(alfio.model.AdditionalService) SpecialPrice(alfio.model.SpecialPrice) Data(lombok.Data) Optional(java.util.Optional) Event(alfio.model.Event) AdditionalServiceReservationModification(alfio.model.modification.AdditionalServiceReservationModification) SpecialPrice(alfio.model.SpecialPrice) AdditionalService(alfio.model.AdditionalService) ArrayList(java.util.ArrayList) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) TicketCategory(alfio.model.TicketCategory) ASReservationWithOptionalCodeModification(alfio.model.modification.ASReservationWithOptionalCodeModification) AdditionalServiceReservationModification(alfio.model.modification.AdditionalServiceReservationModification) TicketReservationWithOptionalCodeModification(alfio.model.modification.TicketReservationWithOptionalCodeModification) ZonedDateTime(java.time.ZonedDateTime) TicketReservationModification(alfio.model.modification.TicketReservationModification) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with AdditionalService

use of alfio.model.AdditionalService in project alf.io by alfio-event.

the class AdditionalServiceApiController method update.

@PutMapping("/event/{eventId}/additional-services/{additionalServiceId}")
@Transactional
public ResponseEntity<EventModification.AdditionalService> update(@PathVariable("eventId") int eventId, @PathVariable("additionalServiceId") int additionalServiceId, @RequestBody EventModification.AdditionalService additionalService, BindingResult bindingResult) {
    ValidationResult validationResult = Validator.validateAdditionalService(additionalService, bindingResult);
    Validate.isTrue(validationResult.isSuccess(), "validation failed");
    Validate.isTrue(additionalServiceId == additionalService.getId(), "wrong input");
    return eventRepository.findOptionalById(eventId).map(event -> {
        int result = additionalServiceManager.update(additionalServiceId, additionalService.isFixPrice(), additionalService.getOrdinal(), additionalService.getAvailableQuantity(), additionalService.getMaxQtyPerOrder(), additionalService.getInception().toZonedDateTime(event.getZoneId()), additionalService.getExpiration().toZonedDateTime(event.getZoneId()), additionalService.getVat(), additionalService.getVatType(), Optional.ofNullable(additionalService.getPrice()).map(p -> MonetaryUtil.unitToCents(p, event.getCurrency())).orElse(0));
        Validate.isTrue(result <= 1, "too many records updated");
        Stream.concat(additionalService.getTitle().stream(), additionalService.getDescription().stream()).forEach(t -> {
            if (t.getId() != null) {
                additionalServiceManager.updateText(t.getId(), t.getLocale(), t.getType(), t.getValue());
            } else {
                additionalServiceManager.insertText(additionalService.getId(), t.getLocale(), t.getType(), t.getValue());
            }
        });
        return ResponseEntity.ok(additionalService);
    }).orElseThrow(IllegalArgumentException::new);
}
Also used : PriceContainer(alfio.model.PriceContainer) ValidationResult(alfio.model.result.ValidationResult) ExportUtils(alfio.util.ExportUtils) RequiredArgsConstructor(lombok.RequiredArgsConstructor) BindingResult(org.springframework.validation.BindingResult) EventModification(alfio.model.modification.EventModification) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) BigDecimal(java.math.BigDecimal) Map(java.util.Map) AdditionalServiceManager(alfio.manager.AdditionalServiceManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) EventRepository(alfio.repository.EventRepository) Validator(alfio.util.Validator) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) EventManager(alfio.manager.EventManager) AdditionalService(alfio.model.AdditionalService) Validate(org.apache.commons.lang3.Validate) Principal(java.security.Principal) Stream(java.util.stream.Stream) MonetaryUtil(alfio.util.MonetaryUtil) DateTimeFormatter(java.time.format.DateTimeFormatter) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) Event(alfio.model.Event) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) ValidationResult(alfio.model.result.ValidationResult) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

EventManager (alfio.manager.EventManager)3 AdditionalService (alfio.model.AdditionalService)3 Event (alfio.model.Event)3 BigDecimal (java.math.BigDecimal)3 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 AdditionalServiceManager (alfio.manager.AdditionalServiceManager)2 PriceContainer (alfio.model.PriceContainer)2 EventModification (alfio.model.modification.EventModification)2 ValidationResult (alfio.model.result.ValidationResult)2 EventRepository (alfio.repository.EventRepository)2 ExportUtils (alfio.util.ExportUtils)2 MonetaryUtil (alfio.util.MonetaryUtil)2 Validator (alfio.util.Validator)2 IOException (java.io.IOException)2 Principal (java.security.Principal)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Collections (java.util.Collections)2 Map (java.util.Map)2