use of com.yahoo.elide.jsonapi.models.Meta in project elide by yahoo.
the class CollectionTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument jsonApiDocument = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Set<PersistentResource> collection = getResourceCollection(requestScope).toList(LinkedHashSet::new).blockingGet();
// Set data
jsonApiDocument.setData(getData(collection, requestScope.getDictionary()));
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(jsonApiDocument, collection, queryParams);
Pagination pagination = parentProjection.getPagination();
if (parent.isPresent()) {
pagination = parentProjection.getRelationship(relationName.orElseThrow(IllegalStateException::new)).get().getProjection().getPagination();
}
// Add pagination meta data
if (!pagination.isDefaultInstance()) {
Map<String, Number> pageMetaData = new HashMap<>();
pageMetaData.put("number", (pagination.getOffset() / pagination.getLimit()) + 1);
pageMetaData.put("limit", pagination.getLimit());
// Get total records if it has been requested and add to the page meta data
if (pagination.returnPageTotals()) {
Long totalRecords = pagination.getPageTotals();
pageMetaData.put("totalPages", totalRecords / pagination.getLimit() + ((totalRecords % pagination.getLimit()) > 0 ? 1 : 0));
pageMetaData.put("totalRecords", totalRecords);
}
Map<String, Object> allMetaData = new HashMap<>();
allMetaData.put("page", pageMetaData);
Meta meta = new Meta(allMetaData);
jsonApiDocument.setMeta(meta);
}
JsonNode responseBody = requestScope.getMapper().toJsonObject(jsonApiDocument);
return () -> Pair.of(HttpStatus.SC_OK, responseBody);
}
use of com.yahoo.elide.jsonapi.models.Meta in project elide by yahoo.
the class JsonApiTest method readSingleWithMeta.
@Test
public void readSingleWithMeta() throws IOException {
String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}},\"meta\":{\"additional\":\"info\"}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Meta meta = jsonApiDocument.getMeta();
Data<Resource> dataObj = jsonApiDocument.getData();
Resource data = dataObj.getSingleValue();
Map<String, Object> attributes = data.getAttributes();
Map<String, Relationship> relations = data.getRelationships();
assertEquals(meta.getMetaMap().get("additional"), "info");
assertEquals(data.getType(), "parent");
assertEquals(data.getId(), "123");
assertEquals(attributes.get("firstName"), "bob");
assertEquals(relations.get("children").getData().getSingleValue().getType(), "child");
assertEquals(relations.get("children").getData().getSingleValue().getId(), "2");
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.Meta in project elide by yahoo.
the class JsonApiTest method readListWithMeta.
@Test
public void readListWithMeta() throws IOException {
String doc = "{\"data\":[{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"child\",\"id\":\"2\"}}}}],\"meta\":{\"additional\":\"info\"}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Meta meta = jsonApiDocument.getMeta();
Data<Resource> list = jsonApiDocument.getData();
Resource data = list.get().iterator().next();
Map<String, Object> attributes = data.getAttributes();
List<Resource> included = jsonApiDocument.getIncluded();
assertEquals(meta.getMetaMap().get("additional"), "info");
assertEquals(data.getType(), "parent");
assertEquals(data.getId(), "123");
assertEquals(attributes.get("firstName"), "bob");
assertEquals(data.getRelationships().get("children").getData().getSingleValue().getId(), "2");
assertNull(included);
checkEquality(jsonApiDocument);
}
Aggregations