Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modorders-1087-Delete received pieces in bulk #409

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ramls/piecesBatch.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#%RAML 1.0
title: "Orders Storage"
baseUri: http://github.com/org/folio/mod-orders-storage
version: v3.2

documentation:
- title: Pieces Batch
content: <b>API to manage batch operations for Pieces. This batch API allows for creating, updating, and deleting multiple pieces in one request.</b>

types:
piece: !include acq-models/mod-orders-storage/schemas/piece.json
piece_collection: !include acq-models/mod-orders-storage/schemas/piece_collection.json
UUID:
type: string
pattern: ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-f]{3}-[0-9a-fA-F]{12}$

traits:
orderable: !include raml-util/traits/orderable.raml
pageable: !include raml-util/traits/pageable.raml
searchable: !include raml-util/traits/searchable.raml

resourceTypes:
collection: !include raml-util/rtypes/collection.raml
collection-item: !include raml-util/rtypes/item-collection.raml

/orders-storage/pieces-batch:
delete:
description: Delete multiple pieces in a batch.
body:
application/json:
type: piece_collection
example: !include acq-models/mod-orders-storage/examples/piece_collection.sample
responses:
204:
description: Successfully deleted pieces.
400:
description: Bad request, e.g., malformed JSON.
404:
description: Not found, one or more pieces do not exist.
500:
description: Internal server error, e.g., database error.
77 changes: 77 additions & 0 deletions src/main/java/org/folio/rest/impl/PiecesAPIBatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.folio.rest.impl;

import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.dao.PostgresClientFactory;
import org.folio.event.service.AuditOutboxService;
import org.folio.rest.annotations.Validate;
import org.folio.rest.core.BaseApi;
import org.folio.rest.jaxrs.model.Piece;
import org.folio.rest.jaxrs.model.PieceCollection;
import org.folio.rest.jaxrs.resource.OrdersStoragePieces;
import org.folio.rest.jaxrs.resource.OrdersStoragePiecesBatch;
import org.folio.rest.persist.HelperUtils;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.persist.PostgresClient;
import org.folio.spring.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

public class PiecesAPIBatch extends BaseApi implements OrdersStoragePiecesBatch {
private static final Logger log = LogManager.getLogger();

public static final String PIECES_TABLE = "pieces";

private final PostgresClient pgClient;

@Autowired
private AuditOutboxService auditOutboxService;
@Autowired
private PostgresClientFactory pgClientFactory;
private static final Logger logger = LogManager.getLogger();
@Autowired
public PiecesAPIBatch(Vertx vertx, String tenantId) {
SpringContextUtil.autowireDependencies(this, Vertx.currentContext());
log.trace("Init PiecesAPI creating PostgresClient");
pgClient = pgClientFactory.createInstance(tenantId);
}

private String getPieceIdsForLogMessage(List<Piece> pieces) {
return pieces.stream()
.map(Piece::getId)
.collect(Collectors.joining(", "));
}

protected String getEndpoint(Object entity) {
return HelperUtils.getEndpoint(OrdersStoragePiecesBatch.class);
}


@Override
@Validate
public void deleteOrdersStoragePiecesBatch(PieceCollection entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
Future.all(entity.getPieces().stream()
.map(piece -> PgUtil.deleteById(PIECES_TABLE, piece.getId(), okapiHeaders, vertxContext, OrdersStoragePieces.DeleteOrdersStoragePiecesByIdResponse.class)
.onFailure(t -> log.error("Failed to delete piece with ID: {}", piece.getId()))
.mapEmpty())
.collect(Collectors.toList()))
.onComplete(ar -> {
if (ar.failed()) {
log.error("deleteOrdersStoragePiecesBatch:: failed, piece ids: {}", getPieceIdsForLogMessage(entity.getPieces()), ar.cause());
asyncResultHandler.handle(buildErrorResponse(ar.cause()));
} else {
log.info("deleteOrdersStoragePiecesBatch:: completed, piece ids: {}", getPieceIdsForLogMessage(entity.getPieces()));
asyncResultHandler.handle(buildNoContentResponse());
}
});
}
}
Loading