Skip to content

Commit

Permalink
Delete received pieces in bulk with correct raml file and PiecesAPIBa…
Browse files Browse the repository at this point in the history
…tch.java for the batch pieces
  • Loading branch information
yuntianhu committed Apr 25, 2024
1 parent b93efba commit 691a332
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 55 deletions.
64 changes: 27 additions & 37 deletions ramls/piecesBatch.raml
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,39 @@ title: "Orders Storage"
baseUri: http://github.com/org/folio/mod-orders-storage
version: v3.2


documentation:
- title: Pieces Batch
content: <b>This module implements the pieces batch processing interface. This API is intended for internal use only. </b>

- 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_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-fA-F]{3}-[0-9a-fA-F]{12}$

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

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

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

/orders-storage/pieces-batch:
type:
collection:
exampleCollection: !include acq-models/mod-orders-storage/examples/piece_collection.sample
exampleItem: !include acq-models/mod-orders-storage/examples/piece_post.sample
schemaCollection: piece_collection
delete:
description: delete collection of pieces
is: [
searchable: {description: "with valid searchable fields: for example code", example: "[\"code\", \"MEDGRANT\", \"=\"]"},
pageable
]
/piecesCollection:
uriParameters:
pieceCollection:
description: A Collection of Pieces
type: collection
type:
collection-item:
exampleItem: !include acq-models/mod-orders-storage/examples/piece_get.sample
schema: piece

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.
32 changes: 14 additions & 18 deletions src/main/java/org/folio/rest/impl/PiecesAPIBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,33 @@ public PiecesAPIBatch(Vertx vertx, String tenantId) {
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 deleteOrdersStoragePiecesByBatch(PieceCollection pieceCollection, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
Future.all(pieceCollection.getPieces().stream()
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(pieceCollection.getPieces()), ar.cause());
log.error("deleteOrdersStoragePiecesBatch:: failed, piece ids: {}", getPieceIdsForLogMessage(entity.getPieces()), ar.cause());
asyncResultHandler.handle(buildErrorResponse(ar.cause()));
} else {
log.info("deleteOrdersStoragePiecesBatch:: completed, piece ids: {}", getPieceIdsForLogMessage(pieceCollection.getPieces()));
log.info("deleteOrdersStoragePiecesBatch:: completed, piece ids: {}", getPieceIdsForLogMessage(entity.getPieces()));
asyncResultHandler.handle(buildNoContentResponse());
}
});
}

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



}

0 comments on commit 691a332

Please sign in to comment.