Search in sources :

Example 6 with Data

use of com.yahoo.elide.jsonapi.models.Data in project faf-java-api by FAForever.

the class LeaderboardController method getSingleGlobal.

@Async
@RequestMapping(path = "/global/{playerId}", method = RequestMethod.GET)
@ApiOperation("Lists the global leaderboard for the specified player")
public CompletableFuture<JsonApiDocument> getSingleGlobal(@PathVariable("playerId") String playerId) {
    GlobalLeaderboardEntry entry = leaderboardService.getGlobalEntry(Integer.valueOf(playerId));
    if (entry == null) {
        throw new ResourceNotFoundException("No global leaderboard entry found for player: " + playerId);
    }
    Resource resource = new Resource(GLOBAL_LEADERBOARD_ENTRY, playerId, ImmutableMap.<String, Object>builder().put("name", entry.getPlayerName()).put("mean", entry.getMean()).put("deviation", entry.getDeviation()).put("numGames", entry.getNumGames()).put("rank", entry.getRank()).put("rating", (int) (entry.getMean() - 3 * entry.getDeviation())).build(), null, null, null);
    return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(resource)));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) Data(com.yahoo.elide.jsonapi.models.Data) ResourceNotFoundException(com.faforever.api.web.ResourceNotFoundException) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with Data

use of com.yahoo.elide.jsonapi.models.Data in project faf-java-api by FAForever.

the class LeaderboardController method getSingleLadder1v1.

@Async
@RequestMapping(path = "/ladder1v1/{playerId}", method = RequestMethod.GET)
@ApiOperation("Lists the ladder1v1 leaderboard for the specified player")
public CompletableFuture<JsonApiDocument> getSingleLadder1v1(@PathVariable("playerId") String playerId) {
    Ladder1v1LeaderboardEntry entry = leaderboardService.getLadder1v1Entry(Integer.valueOf(playerId));
    if (entry == null) {
        throw new ResourceNotFoundException("No ladder1v1 entry found for player: " + playerId);
    }
    Resource resource = new Resource(LADDER_1V1_LEADERBOARD_ENTRY, playerId, ImmutableMap.<String, Object>builder().put("name", entry.getPlayerName()).put("mean", entry.getMean()).put("deviation", entry.getDeviation()).put("numGames", entry.getNumGames()).put("wonGames", entry.getWonGames()).put("rank", entry.getRank()).put("rating", (int) (entry.getMean() - 3 * entry.getDeviation())).build(), null, null, null);
    return CompletableFuture.completedFuture(new JsonApiDocument(new Data<>(resource)));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) Data(com.yahoo.elide.jsonapi.models.Data) ResourceNotFoundException(com.faforever.api.web.ResourceNotFoundException) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with Data

use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.

the class PersistentResourceTest method testRelationshipMissingData.

/**
 * Verify that Relationship toMany cannot contain null resources, but toOne can.
 *
 * @throws Exception
 */
@Test
public void testRelationshipMissingData() throws Exception {
    User goodUser = new TestUser("1");
    @SuppressWarnings("resource") DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, tx, goodUser, null, null, UUID.randomUUID(), elideSettings);
    // null resource in toMany relationship is not valid
    List<Resource> idList = new ArrayList<>();
    idList.add(new ResourceIdentifier("child", "3").castToResource());
    idList.add(new ResourceIdentifier("child", "6").castToResource());
    idList.add(null);
    assertThrows(NullPointerException.class, () -> new Relationship(Collections.emptyMap(), new Data<>(idList)));
    // However null toOne relationship is valid
    Relationship toOneRelationship = new Relationship(Collections.emptyMap(), new Data<>((Resource) null));
    assertTrue(toOneRelationship.getData().get().isEmpty());
    assertNull(toOneRelationship.toPersistentResources(goodScope));
    // no Data
    Relationship nullRelationship = new Relationship(Collections.emptyMap(), null);
    assertNull(nullRelationship.getData());
    assertNull(nullRelationship.toPersistentResources(goodScope));
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) TestUser(com.yahoo.elide.core.security.TestUser) User(com.yahoo.elide.core.security.User) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Data(com.yahoo.elide.jsonapi.models.Data) TestUser(com.yahoo.elide.core.security.TestUser) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) Test(org.junit.jupiter.api.Test)

Example 9 with Data

use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.

the class DataDeserializer method deserialize.

@Override
public Data<Resource> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    if (node.isArray()) {
        List<Resource> resources = new ArrayList<>();
        for (JsonNode n : node) {
            Resource r = MAPPER.convertValue(n, Resource.class);
            validateResource(jsonParser, r);
            resources.add(r);
        }
        return new Data<>(resources);
    }
    Resource resource = MAPPER.convertValue(node, Resource.class);
    validateResource(jsonParser, resource);
    return new Data<>(resource);
}
Also used : Resource(com.yahoo.elide.jsonapi.models.Resource) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Data(com.yahoo.elide.jsonapi.models.Data)

Example 10 with Data

use of com.yahoo.elide.jsonapi.models.Data in project elide by yahoo.

the class RelationshipTerminalState method handleGet.

@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
    JsonApiDocument doc = new JsonApiDocument();
    RequestScope requestScope = state.getRequestScope();
    JsonApiMapper mapper = requestScope.getMapper();
    MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
    Map<String, Relationship> relationships = record.toResource(parentProjection).getRelationships();
    if (relationships != null && relationships.containsKey(relationshipName)) {
        Relationship relationship = relationships.get(relationshipName);
        // Handle valid relationship
        // Set data
        Data<Resource> data = relationship.getData();
        doc.setData(data);
        // Run include processor
        DocumentProcessor includedProcessor = new IncludedProcessor();
        includedProcessor.execute(doc, record, queryParams);
        return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
    }
    // Handle no data for relationship
    if (relationshipType.isToMany()) {
        doc.setData(new Data<>(new ArrayList<>()));
    } else if (relationshipType.isToOne()) {
        doc.setData(new Data<>((Resource) null));
    } else {
        throw new IllegalStateException("Failed to GET a relationship; relationship is neither toMany nor toOne");
    }
    return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ArrayList(java.util.ArrayList) Data(com.yahoo.elide.jsonapi.models.Data) RequestScope(com.yahoo.elide.core.RequestScope) Relationship(com.yahoo.elide.jsonapi.models.Relationship) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Aggregations

Data (com.yahoo.elide.jsonapi.models.Data)11 Resource (com.yahoo.elide.jsonapi.models.Resource)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)8 PersistentResource (com.yahoo.elide.core.PersistentResource)5 Test (org.junit.jupiter.api.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ArrayList (java.util.ArrayList)3 Async (org.springframework.scheduling.annotation.Async)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResourceNotFoundException (com.faforever.api.web.ResourceNotFoundException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 RequestScope (com.yahoo.elide.core.RequestScope)2 JsonApiMapper (com.yahoo.elide.jsonapi.JsonApiMapper)2 Relationship (com.yahoo.elide.jsonapi.models.Relationship)2 FeaturedMod (com.faforever.api.data.domain.FeaturedMod)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1 RelationshipType (com.yahoo.elide.core.dictionary.RelationshipType)1 TestUser (com.yahoo.elide.core.security.TestUser)1 User (com.yahoo.elide.core.security.User)1 DocumentProcessor (com.yahoo.elide.jsonapi.document.processors.DocumentProcessor)1