use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class VerifyService method getPokemonById.
// Get Pokemon by ID and return its data.
private void getPokemonById(final ServerRequest request, final ServerResponse response) {
try {
final String idStr = AbstractService.param(request, QUERY_ID_PARAM);
final int id = Integer.parseInt(idStr);
final JsonObjectBuilder pokemonBuilder = Json.createObjectBuilder();
dbClient.execute(exec -> exec.namedGet("get-pokemon-by-id", id)).thenAccept(data -> data.ifPresentOrElse(row -> {
final JsonArrayBuilder typesBuilder = Json.createArrayBuilder();
pokemonBuilder.add("name", row.column("name").as(String.class));
pokemonBuilder.add("id", row.column("id").as(Integer.class));
dbClient.execute(exec -> exec.namedQuery("get-pokemon-types", id)).forEach(typeRow -> typesBuilder.add(typeRow.as(JsonObject.class))).onComplete(() -> {
pokemonBuilder.add("types", typesBuilder.build());
response.send(AppResponse.okStatus(pokemonBuilder.build()));
}).exceptionally(t -> {
response.send(exceptionStatus(t));
return null;
});
}, () -> response.send(AppResponse.okStatus(JsonObject.EMPTY_JSON_OBJECT)))).exceptionally(t -> {
response.send(exceptionStatus(t));
return null;
});
} catch (RemoteTestException ex) {
response.send(exceptionStatus(ex));
}
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class MapperService method executeUpdateTest.
private void executeUpdateTest(final ServerRequest request, final ServerResponse response, final String testName, final TestDMLFunction test) {
LOGGER.fine(() -> String.format("Running Mapper.%s on server", testName));
try {
String name = param(request, QUERY_NAME_PARAM);
String idStr = param(request, QUERY_ID_PARAM);
int id = Integer.parseInt(idStr);
Pokemon srcPokemon = Pokemon.POKEMONS.get(id);
Pokemon updatedPokemon = new Pokemon(id, name, srcPokemon.getTypesArray());
test.apply(updatedPokemon).thenAccept(result -> response.send(AppResponse.okStatus(Json.createValue(result)))).exceptionally(t -> {
response.send(exceptionStatus(t));
return null;
});
} catch (RemoteTestException | NumberFormatException ex) {
LOGGER.fine(() -> String.format("Error in Mapper.%s on server", testName));
response.send(exceptionStatus(ex));
}
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class MapperService method executeInsertTest.
private void executeInsertTest(final ServerRequest request, final ServerResponse response, final String testName, final String pokemonName, final List<Type> pokemonTypes, final TestDMLFunction test) {
LOGGER.fine(() -> String.format("Running Mapper.%s on server", testName));
try {
String idStr = param(request, QUERY_ID_PARAM);
int id = Integer.parseInt(idStr);
Pokemon pokemon = new Pokemon(id, pokemonName, pokemonTypes);
test.apply(pokemon).thenAccept(result -> response.send(AppResponse.okStatus(pokemon.toJsonObject()))).exceptionally(t -> {
response.send(AppResponse.exceptionStatus(t));
return null;
});
} catch (RemoteTestException | NumberFormatException ex) {
LOGGER.fine(() -> String.format("Error in Mapper.%s on server", testName));
response.send(exceptionStatus(ex));
}
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class MapperService method executeDeleteTest.
private void executeDeleteTest(final ServerRequest request, final ServerResponse response, final String testName, final TestDMLFunction test) {
LOGGER.fine(() -> String.format("Running Mapper.%s on server", testName));
try {
String idStr = param(request, QUERY_ID_PARAM);
int id = Integer.parseInt(idStr);
Pokemon pokemon = Pokemon.POKEMONS.get(id);
test.apply(pokemon).thenAccept(result -> response.send(AppResponse.okStatus(Json.createValue(result)))).exceptionally(t -> {
response.send(exceptionStatus(t));
return null;
});
} catch (RemoteTestException | NumberFormatException ex) {
LOGGER.fine(() -> String.format("Error in Mapper.%s on server", testName));
response.send(exceptionStatus(ex));
}
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class TransactionInsertService method executeTest.
// Common test execution code
private JsonObject executeTest(final ServerRequest request, final ServerResponse response, final String testName, final String pokemonName, final List<Type> pokemonTypes, final TestFunction test) {
LOGGER.fine(() -> String.format("Running SimpleInsertService.%s on server", testName));
try {
String idStr = param(request, QUERY_ID_PARAM);
int id = Integer.parseInt(idStr);
Pokemon pokemon = new Pokemon(id, pokemonName, pokemonTypes);
test.apply(pokemon).thenAccept(result -> response.send(AppResponse.okStatus(pokemon.toJsonObject()))).exceptionally(t -> {
response.send(AppResponse.exceptionStatus(t));
return null;
});
} catch (RemoteTestException | NumberFormatException ex) {
LOGGER.fine(() -> String.format("Error in SimpleInsertService.%s on server", testName));
response.send(AppResponse.exceptionStatus(ex));
}
return null;
}
Aggregations