Search in sources :

Example 1 with Meta

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);
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Meta(com.yahoo.elide.jsonapi.models.Meta) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) ToString(lombok.ToString) RequestScope(com.yahoo.elide.core.RequestScope) Pagination(com.yahoo.elide.core.request.Pagination) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Example 2 with Meta

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);
}
Also used : Meta(com.yahoo.elide.jsonapi.models.Meta) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 3 with Meta

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);
}
Also used : Meta(com.yahoo.elide.jsonapi.models.Meta) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)3 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)3 Meta (com.yahoo.elide.jsonapi.models.Meta)3 Resource (com.yahoo.elide.jsonapi.models.Resource)2 Test (org.junit.jupiter.api.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 RequestScope (com.yahoo.elide.core.RequestScope)1 Pagination (com.yahoo.elide.core.request.Pagination)1 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)1 IncludedProcessor (com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)1 Relationship (com.yahoo.elide.jsonapi.models.Relationship)1 HashMap (java.util.HashMap)1 ToString (lombok.ToString)1