use of org.folio.inventory.validation.InstancePrecedingSucceedingTitleValidators in project mod-inventory by folio-org.
the class Instances method create.
private void create(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject instanceRequest = routingContext.getBodyAsJson();
if (StringUtils.isBlank(instanceRequest.getString(Instance.TITLE_KEY))) {
ClientErrorResponse.badRequest(routingContext.response(), "Title must be provided for an instance");
return;
}
Instance newInstance = Instance.fromJson(instanceRequest);
completedFuture(newInstance).thenCompose(InstancePrecedingSucceedingTitleValidators::refuseWhenUnconnectedHasNoTitle).thenCompose(instance -> storage.getInstanceCollection(context).add(instance)).thenCompose(response -> {
response.setParentInstances(newInstance.getParentInstances());
response.setChildInstances(newInstance.getChildInstances());
response.setPrecedingTitles(newInstance.getPrecedingTitles());
response.setSucceedingTitles(newInstance.getSucceedingTitles());
return updateRelatedRecords(routingContext, context, response).thenApply(notUsed -> response);
}).thenAccept(response -> {
try {
URL url = context.absoluteUrl(format("%s/%s", INSTANCES_PATH, response.getId()));
RedirectResponse.created(routingContext.response(), url.toString());
} catch (MalformedURLException e) {
log.warn(format("Failed to create self link for instance: %s", e.toString()));
}
}).exceptionally(doExceptionally(routingContext));
}
use of org.folio.inventory.validation.InstancePrecedingSucceedingTitleValidators in project mod-inventory by folio-org.
the class Instances method update.
private void update(RoutingContext rContext) {
WebContext wContext = new WebContext(rContext);
JsonObject instanceRequest = rContext.getBodyAsJson();
Instance updatedInstance = Instance.fromJson(instanceRequest);
InstanceCollection instanceCollection = storage.getInstanceCollection(wContext);
completedFuture(updatedInstance).thenCompose(InstancePrecedingSucceedingTitleValidators::refuseWhenUnconnectedHasNoTitle).thenCompose(instance -> instanceCollection.findById(rContext.request().getParam("id"))).thenCompose(InstancesValidators::refuseWhenInstanceNotFound).thenCompose(existingInstance -> fetchPrecedingSucceedingTitles(new Success<>(existingInstance), rContext, wContext)).thenCompose(existingInstance -> refuseWhenBlockedFieldsChanged(existingInstance, updatedInstance)).thenCompose(existingInstance -> refuseWhenHridChanged(existingInstance, updatedInstance)).thenAccept(existingInstance -> updateInstance(updatedInstance, rContext, wContext)).exceptionally(doExceptionally(rContext));
}
Aggregations