use of alfio.model.result.ValidationResult in project alf.io by alfio-event.
the class EventController method handleCode.
@RequestMapping(value = "/event/{eventName}/code/{code}", method = { RequestMethod.GET, RequestMethod.HEAD })
public String handleCode(@PathVariable("eventName") String eventName, @PathVariable("code") String code, Model model, ServletWebRequest request, RedirectAttributes redirectAttributes, Locale locale) {
String trimmedCode = StringUtils.trimToNull(code);
return eventRepository.findOptionalByShortName(eventName).map(event -> {
String redirectToEvent = "redirect:/event/" + eventName + "/";
ValidationResult res = savePromoCode(eventName, trimmedCode, model, request.getRequest());
CodeType codeType = getCodeType(event.getId(), trimmedCode);
if (res.isSuccess() && codeType == CodeType.PROMO_CODE_DISCOUNT) {
return redirectToEvent;
} else if (codeType == CodeType.TICKET_CATEGORY_CODE) {
TicketCategory category = ticketCategoryRepository.findCodeInEvent(event.getId(), trimmedCode).get();
if (!category.isAccessRestricted()) {
return makeSimpleReservation(eventName, request, redirectAttributes, locale, null, event, category.getId());
} else {
Optional<SpecialPrice> specialPrice = specialPriceRepository.findActiveNotAssignedByCategoryId(category.getId()).stream().findFirst();
if (!specialPrice.isPresent()) {
return redirectToEvent;
}
savePromoCode(eventName, specialPrice.get().getCode(), model, request.getRequest());
return makeSimpleReservation(eventName, request, redirectAttributes, locale, specialPrice.get().getCode(), event, category.getId());
}
} else if (res.isSuccess() && codeType == CodeType.SPECIAL_PRICE) {
int ticketCategoryId = specialPriceRepository.getByCode(trimmedCode).get().getTicketCategoryId();
return makeSimpleReservation(eventName, request, redirectAttributes, locale, trimmedCode, event, ticketCategoryId);
} else {
return redirectToEvent;
}
}).orElse("redirect:/");
}
use of alfio.model.result.ValidationResult in project alf.io by alfio-event.
the class AdditionalServiceApiController method update.
@RequestMapping(value = "/event/{eventId}/additional-services/{additionalServiceId}", method = RequestMethod.PUT)
@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 = additionalServiceRepository.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(MonetaryUtil::unitToCents).orElse(0));
Validate.isTrue(result <= 1, "too many records updated");
Stream.concat(additionalService.getTitle().stream(), additionalService.getDescription().stream()).forEach(t -> {
if (t.getId() != null) {
additionalServiceTextRepository.update(t.getId(), t.getLocale(), t.getType(), t.getValue());
} else {
additionalServiceTextRepository.insert(additionalService.getId(), t.getLocale(), t.getType(), t.getValue());
}
});
return ResponseEntity.ok(additionalService);
}).orElseThrow(IllegalArgumentException::new);
}
use of alfio.model.result.ValidationResult in project alf.io by alfio-event.
the class TicketHelper method assignTicket.
private Triple<ValidationResult, Event, Ticket> assignTicket(UpdateTicketOwnerForm updateTicketOwner, Optional<Errors> bindingResult, HttpServletRequest request, Optional<UserDetails> userDetails, Triple<Event, TicketReservation, Ticket> result) {
Ticket t = result.getRight();
final Event event = result.getLeft();
if (t.getLockedAssignment()) {
// in case of locked assignment, fullName and Email will be overwritten
updateTicketOwner.setFirstName(t.getFirstName());
updateTicketOwner.setLastName(t.getLastName());
updateTicketOwner.setFullName(t.getFullName());
updateTicketOwner.setEmail(t.getEmail());
}
final TicketReservation ticketReservation = result.getMiddle();
List<TicketFieldConfiguration> fieldConf = ticketFieldRepository.findAdditionalFieldsForEvent(event.getId());
ValidationResult validationResult = Validator.validateTicketAssignment(updateTicketOwner, fieldConf, bindingResult, event).ifSuccess(() -> updateTicketOwner(updateTicketOwner, request, t, event, ticketReservation, userDetails));
return Triple.of(validationResult, event, ticketRepository.findByUUID(t.getUuid()));
}
use of alfio.model.result.ValidationResult in project alf.io by alfio-event.
the class Validator method validateAdditionalService.
public static ValidationResult validateAdditionalService(EventModification.AdditionalService additionalService, EventModification eventModification, Errors errors) {
if (additionalService.isFixPrice() && !Optional.ofNullable(additionalService.getPrice()).filter(p -> p.compareTo(BigDecimal.ZERO) >= 0).isPresent()) {
errors.rejectValue("additionalServices", "error.price");
}
List<EventModification.AdditionalServiceText> descriptions = additionalService.getDescription();
List<EventModification.AdditionalServiceText> titles = additionalService.getTitle();
if (descriptions == null || titles == null || titles.size() != descriptions.size()) {
errors.rejectValue("additionalServices", "error.title");
errors.rejectValue("additionalServices", "error.description");
} else {
if (!validateDescriptionList(titles) || !containsAllRequiredTranslations(eventModification, titles)) {
errors.rejectValue("additionalServices", "error.title");
}
if (!validateDescriptionList(descriptions) || !containsAllRequiredTranslations(eventModification, descriptions)) {
errors.rejectValue("additionalServices", "error.description");
}
}
DateTimeModification inception = additionalService.getInception();
DateTimeModification expiration = additionalService.getExpiration();
if (inception == null || expiration == null || expiration.isBefore(inception)) {
errors.rejectValue("additionalServices", "error.inception");
errors.rejectValue("additionalServices", "error.expiration");
} else if (eventModification != null && expiration.isAfter(eventModification.getEnd())) {
errors.rejectValue("additionalServices", "error.expiration");
}
return evaluateValidationResult(errors);
}
Aggregations