Search in sources :

Example 11 with ValidationResult

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

the class EventApiV1Controller method create.

@PostMapping("/create")
@Transactional
public ResponseEntity<String> create(@RequestBody EventCreationRequest request, Principal user) {
    String imageRef = Optional.ofNullable(request.getImageUrl()).map(this::fetchImage).orElse(null);
    Organization organization = userManager.findUserOrganizations(user.getName()).get(0);
    AtomicReference<Errors> errorsContainer = new AtomicReference<>();
    Result<String> result = new Result.Builder<String>().checkPrecondition(() -> isNotBlank(request.getTitle()), ErrorCode.custom("invalid.title", "Invalid title")).checkPrecondition(() -> isBlank(request.getSlug()) || eventNameManager.isUnique(request.getSlug()), ErrorCode.custom("invalid.slug", "Invalid slug")).checkPrecondition(() -> isNotBlank(request.getWebsiteUrl()), ErrorCode.custom("invalid.websiteUrl", "Invalid Website URL")).checkPrecondition(() -> isNotBlank(request.getTermsAndConditionsUrl()), ErrorCode.custom("invalid.tc", "Invalid Terms and Conditions")).checkPrecondition(() -> isNotBlank(request.getImageUrl()), ErrorCode.custom("invalid.imageUrl", "Invalid Image URL")).checkPrecondition(() -> isNotBlank(request.getTimezone()), ErrorCode.custom("invalid.timezone", "Invalid Timezone")).checkPrecondition(() -> isNotBlank(imageRef), ErrorCode.custom("invalid.image", "Image is either missing or too big (max 200kb)")).checkPrecondition(() -> {
        EventModification eventModification = request.toEventModification(organization, eventNameManager::generateShortName, imageRef);
        errorsContainer.set(new BeanPropertyBindingResult(eventModification, "event"));
        int descriptionMaxLength = configurationManager.getFor(ConfigurationKeys.DESCRIPTION_MAXLENGTH, ConfigurationLevel.system()).getValueAsIntOrDefault(4096);
        ValidationResult validationResult = validateEvent(eventModification, errorsContainer.get(), descriptionMaxLength);
        if (!validationResult.isSuccess()) {
            log.warn("validation failed {}", validationResult.getValidationErrors());
        }
        return validationResult.isSuccess();
    }, ErrorCode.lazy(() -> toErrorCode(errorsContainer.get()))).build(() -> insertEvent(request, user, imageRef).map(Event::getShortName).orElseThrow(IllegalStateException::new));
    if (result.isSuccess()) {
        return ResponseEntity.ok(result.getData());
    } else {
        return ResponseEntity.badRequest().body(Json.toJson(result.getErrors()));
    }
}
Also used : Errors(org.springframework.validation.Errors) Organization(alfio.model.user.Organization) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) EventModification(alfio.model.modification.EventModification) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) ValidationResult(alfio.model.result.ValidationResult) ValidationResult(alfio.model.result.ValidationResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Result(alfio.model.result.Result) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ValidationResult (alfio.model.result.ValidationResult)11 Validator (alfio.util.Validator)6 IOException (java.io.IOException)5 Collectors (java.util.stream.Collectors)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ResponseEntity (org.springframework.http.ResponseEntity)5 org.springframework.web.bind.annotation (org.springframework.web.bind.annotation)5 alfio.model (alfio.model)4 I18nManager (alfio.manager.i18n.I18nManager)3 Event (alfio.model.Event)3 PaymentProxy (alfio.model.transaction.PaymentProxy)3 Organization (alfio.model.user.Organization)3 BigDecimal (java.math.BigDecimal)3 Principal (java.security.Principal)3 java.util (java.util)3 Log4j2 (lombok.extern.log4j.Log4j2)3 StringUtils (org.apache.commons.lang3.StringUtils)3 Validate (org.apache.commons.lang3.Validate)3 HttpStatus (org.springframework.http.HttpStatus)3 EventListItem (alfio.controller.api.support.EventListItem)2