use of org.folio.inventory.domain.user.UserCollection 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));
}
use of org.folio.inventory.domain.user.UserCollection in project mod-inventory by folio-org.
the class Items method create.
private void create(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject item = routingContext.getBodyAsJson();
Optional<ValidationError> validationError = itemHasCorrectStatus(item);
if (validationError.isPresent()) {
unprocessableEntity(routingContext.response(), validationError.get());
return;
}
Item newItem = ItemUtil.jsonToItem(item);
ItemCollection itemCollection = storage.getItemCollection(context);
UserCollection userCollection = storage.getUserCollection(context);
if (newItem.getBarcode() != null) {
try {
itemCollection.findByCql(CqlHelper.barcodeIs(newItem.getBarcode()), PagingParameters.defaults(), findResult -> {
if (findResult.getResult().records.isEmpty()) {
findUserAndAddItem(routingContext, context, newItem, userCollection, itemCollection);
} else {
ClientErrorResponse.badRequest(routingContext.response(), String.format("Barcode must be unique, %s is already assigned to another item", newItem.getBarcode()));
}
}, FailureResponseConsumer.serverError(routingContext.response()));
} catch (UnsupportedEncodingException e) {
ServerErrorResponse.internalError(routingContext.response(), e.toString());
}
} else {
findUserAndAddItem(routingContext, context, newItem, userCollection, itemCollection);
}
}
Aggregations