123456789101112131415161718192021222324252627282930313233343536373839 |
- package hello;
- import static hello.Helper.randomWorldNumber;
- import static hello.Helper.sendException;
- import static hello.Helper.sendJson;
- import com.mongodb.async.client.MongoCollection;
- import com.mongodb.async.client.MongoDatabase;
- import com.mongodb.client.model.Filters;
- import io.undertow.server.HttpHandler;
- import io.undertow.server.HttpServerExchange;
- import org.bson.Document;
- /**
- * Handles the single-query database test using MongoDB with an asynchronous
- * API.
- */
- final class DbMongoAsyncHandler implements HttpHandler {
- private final MongoCollection<Document> worldCollection;
- DbMongoAsyncHandler(MongoDatabase db) {
- worldCollection = db.getCollection("world");
- }
- @Override
- public void handleRequest(HttpServerExchange exchange) {
- worldCollection
- .find(Filters.eq(randomWorldNumber()))
- .map(Helper::mongoDocumentToWorld)
- .first(
- (world, exception) -> {
- if (exception != null) {
- sendException(exchange, exception);
- } else {
- sendJson(exchange, world);
- }
- });
- }
- }
|