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()));
}
}
Aggregations