use of org.folio.inventory.validation.ItemsValidator in project mod-inventory by folio-org.
the class Items method update.
private void update(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject itemRequest = routingContext.getBodyAsJson();
Optional<ValidationError> validationError = itemHasCorrectStatus(itemRequest);
if (validationError.isPresent()) {
unprocessableEntity(routingContext.response(), validationError.get());
return;
}
Item newItem = ItemUtil.jsonToItem(itemRequest);
ItemCollection itemCollection = storage.getItemCollection(context);
UserCollection userCollection = storage.getUserCollection(context);
final String itemId = routingContext.request().getParam("id");
final CompletableFuture<Success<Item>> getItemFuture = new CompletableFuture<>();
itemCollection.findById(itemId, getItemFuture::complete, FailureResponseConsumer.serverError(routingContext.response()));
getItemFuture.thenApply(Success::getResult).thenCompose(ItemsValidator::refuseWhenItemNotFound).thenCompose(oldItem -> hridChanged(oldItem, newItem)).thenCompose(oldItem -> claimedReturnedMarkedAsMissing(oldItem, newItem)).thenAccept(oldItem -> {
if (hasSameBarcode(newItem, oldItem)) {
findUserAndUpdateItem(routingContext, newItem, oldItem, userCollection, itemCollection);
} else {
try {
checkForNonUniqueBarcode(routingContext, newItem, oldItem, itemCollection, userCollection);
} catch (UnsupportedEncodingException e) {
ServerErrorResponse.internalError(routingContext.response(), e.toString());
}
}
}).exceptionally(doExceptionally(routingContext));
}
Aggregations