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)));
}
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)));
}
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));
}
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);
}
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));
}
Aggregations