This repository has been archived by the owner on Apr 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
library delete
Konstantin Sobolev edited this page May 25, 2017
·
2 revisions
Delete operation takes a list of book IDs from the request projection, removes them from the backend and reports back errors.
The same map[BookId,BookRecord]
is used as a result type, but we will only use it to
store errors as values.
Operation schema is is shown below.
delete {
deleteProjection [ required ] ()
outputProjection [ forbidden ] ()
}
- delete projection requires keys to be deleted and doesn't accept any
BookRecord
fields - output projection forbids keys and doesn't accept any fields either. Output map will only contain keys for failures, with errors as values.
Logic is simple here: iterate over keys from request projection, ask backend to delete corresponding books, report any "not found" errors.
public class BooksDeleteOperation extends AbstractDeleteOperation {
BooksDeleteOperation(@NotNull DeleteOperationDeclaration declaration) {
super(declaration);
}
@Override
protected @NotNull CompletableFuture<BookId_BookRecord_Map.Data> process(
@NotNull BookId_BookRecord_Map.Builder.Data responseBuilder,
@NotNull DeleteBooksFieldProjection deleteProjection,
@NotNull OutputBooksFieldProjection outputProjection) {
BookId_BookRecord_Map.Builder booksMapBuilder = BookId_BookRecord_Map.create();
responseBuilder.set(booksMapBuilder);
for (DeleteBookId_BookRecord_MapKeyProjection keyProjection : deleteProjection.dataProjection().keys()) {
BookId bookId = keyProjection.value();
if (!BooksBackend.delete(bookId)) {
booksMapBuilder.putError(
bookId,
new ErrorValue(HttpStatusCode.NOT_FOUND, "Book " + bookId.getVal() + " not found")
);
}
}
return CompletableFuture.completedFuture(responseBuilder);
}
}
As usual we have to plug it into BooksResourceFactory
:
@Override
protected @NotNull DeleteOperation<BookId_BookRecord_Map.Data> constructDeleteOperation(
@NotNull DeleteOperationDeclaration operationDeclaration) throws ServiceInitializationException {
return new BooksDeleteOperation(operationDeclaration);
}
Lets delete books 2
and 5
, where book 5
doesn't exist:
curl -s -g -X DELETE "http://localhost:8888/books<[2,5]" | jq
[
{
"K": 5,
"V": {
"ERROR": 404,
"message": "Book 5 not found"
}
}
]
Nothing is reported for 2
which means a success, and we got a 404 error for 5
. Lets check that 2
is
actually removed:
curl -s -g "http://localhost:8888/books[1,2,3](title)" | jq
[
{
"K": 1,
"V": {
"title": "The Gold Bug"
}
},
{
"K": 2,
"V": {
"ERROR": 404,
"message": "No book with id 2"
}
},
{
"K": 3,
"V": {
"title": "The Adventures of Tom Sawyer"
}
}
]
Next section: client