use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.
the class FakeStorageModule method getMany.
private void getMany(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
Integer limit = context.getIntegerParameter("limit", 10);
Integer offset = context.getIntegerParameter("offset", 0);
String query = context.getStringParameter("query", MATCH_ALL_RECORDS);
log.debug("Handling {}", routingContext.request().uri());
if (query != null) {
queries.add(format("%s?%s", routingContext.request().path(), query));
}
Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
List<JsonObject> filteredItems = new FakeCQLToJSONInterpreter().execute(resourcesForTenant.values(), query);
List<JsonObject> pagedItems = filteredItems.stream().skip(offset).limit(limit).collect(Collectors.toList());
JsonObject result = new JsonObject();
result.put(collectionPropertyName, new JsonArray(pagedItems));
result.put("totalRecords", filteredItems.size());
log.debug("Found {} resources: {}", recordTypeName, result.encodePrettily());
HttpServerResponse response = routingContext.response();
String json = Json.encodePrettily(result);
Buffer buffer = Buffer.buffer(json, "UTF-8");
response.setStatusCode(200);
response.putHeader("content-type", "application/json; charset=utf-8");
response.putHeader("content-length", Integer.toString(buffer.length()));
response.putHeader("X-Okapi-Trace", "GET mod-authtoken-1.4.1-SNAPSHOT.21");
response.putHeader("X-Okapi-Trace", "GET mod-circulation-10.1.0-SNAPSHOT.131");
response.write(buffer);
response.end();
}
use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.
the class FakeStorageModule method checkUniqueProperties.
private void checkUniqueProperties(RoutingContext routingContext) {
if (uniqueProperties.isEmpty()) {
routingContext.next();
return;
}
final WebContext context = new WebContext(routingContext);
JsonObject body = getJsonFromBody(routingContext);
ArrayList<ValidationError> errors = new ArrayList<>();
uniqueProperties.forEach(uniqueProperty -> {
String proposedValue = body.getString(uniqueProperty);
Map<String, JsonObject> records = getResourcesForTenant(new WebContext(routingContext));
if (records.values().stream().map(record -> record.getString(uniqueProperty)).anyMatch(usedValue -> usedValue.equals(proposedValue))) {
errors.add(new ValidationError(format("%s with this %s already exists", recordTypeName, uniqueProperty), uniqueProperty, proposedValue));
context.write(new ValidationErrorFailure(errors));
}
});
if (errors.isEmpty()) {
routingContext.next();
}
}
use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.
the class FakeStorageModule method delete.
private void delete(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
String id = routingContext.request().getParam("id");
Map<String, JsonObject> resourcesForTenant = getResourcesForTenant(context);
if (resourcesForTenant.containsKey(id)) {
resourcesForTenant.remove(id);
noContent().writeTo(routingContext.response());
} else {
ClientErrorResponse.notFound(routingContext.response());
}
}
use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.
the class FakeStorageModule method create.
private void create(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject body = preProcessBody(null, getJsonFromBody(routingContext));
String id = body.getString("id", UUID.randomUUID().toString());
body.put("id", id);
final ZonedDateTime now = getZonedDateTime();
if (includeChangeMetadata) {
final String fakeUserId = APITestContext.getUserId();
body.put(changeMetadataPropertyName, new JsonObject().put("createdDate", formatDateTime(now)).put("createdByUserId", fakeUserId).put("updatedDate", formatDateTime(now)).put("updatedByUserId", fakeUserId));
}
final Map<String, JsonObject> existingRecords = getResourcesForTenant(context);
if (constraint == null) {
existingRecords.put(id, body);
log.debug("Created {} resource: {}", recordTypeName, id);
created(body).writeTo(routingContext.response());
} else {
final Result<Object> checkConstraint = constraint.apply(existingRecords.values(), body);
if (checkConstraint.succeeded()) {
existingRecords.put(id, body);
log.debug("Created {} resource: {}", recordTypeName, id);
created(body).writeTo(routingContext.response());
} else {
checkConstraint.cause().writeTo(routingContext.response());
}
}
}
use of org.folio.circulation.support.http.server.WebContext in project mod-circulation by folio-org.
the class FakeStorageModule method batchUpdate.
private void batchUpdate(RoutingContext routingContext) {
WebContext context = new WebContext(routingContext);
JsonObject body = routingContext.getBodyAsJson();
if (batchUpdatePreProcessor != null) {
body = batchUpdatePreProcessor.apply(body);
}
JsonArray entities = body.getJsonArray(collectionPropertyName);
Result<Void> lastResult = Result.succeeded(null);
for (int entityIndex = 0; entityIndex < entities.size(); entityIndex++) {
JsonObject entity = entities.getJsonObject(entityIndex);
String id = entity.getString("id");
lastResult = lastResult.next(notUsed -> replaceSingleItem(context, id, entity));
}
if (lastResult.failed()) {
lastResult.cause().writeTo(routingContext.response());
} else {
routingContext.response().setStatusCode(201).end();
}
}
Aggregations