Skip to content

Commit

Permalink
CIRC-2099 POST Api implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SreejaMangarapu committed Jul 3, 2024
1 parent 96b5607 commit ee8f081
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
import org.folio.circulation.support.RouteRegistration;
import org.folio.circulation.support.http.server.JsonHttpResponse;
import org.folio.circulation.support.http.server.WebContext;
import org.folio.circulation.support.results.Result;

import java.lang.invoke.MethodHandles;
import java.util.function.Function;

import static org.folio.circulation.support.ValidationErrorFailure.singleValidationError;
import static org.folio.circulation.support.results.Result.ofAsync;
import static org.folio.circulation.support.results.Result.succeeded;

public class PrintEventsResource extends Resource {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
Expand All @@ -40,8 +44,18 @@ void create(RoutingContext routingContext) {
log.info("create:: Creating print event: {}", () -> printEventRequest);

ofAsync(printEventRequest)
.thenApply(refuseWhenPrintEventRequestIsInvalid())
.thenCompose(r -> r.after(printEventsRepository::create))
.thenApply(r -> r.map(response -> JsonHttpResponse.created(null, null)))
.thenApply(r -> r.map(response -> {
assert printEventRequest != null;
return JsonHttpResponse.created(printEventRequest.getRepresentation(), null);
}))
.thenAccept(context::writeResultToHttpResponse);
}

private static Function<Result<PrintEventRequest>, Result<PrintEventRequest>>
refuseWhenPrintEventRequestIsInvalid() {
return r -> r.failWhen(printEventRequest -> succeeded(printEventRequest == null),
circulationSetting -> singleValidationError("Print Event Request JSON is invalid", "", ""));
}
}

0 comments on commit ee8f081

Please sign in to comment.