Search in sources :

Example 31 with Query

use of com.google.cloud.firestore.Query in project getting-started-java by GoogleCloudPlatform.

the class FirestoreDao method listBooks.

// [END bookshelf_firestore_documents_to_books]
// [START bookshelf_firestore_list_books]
@Override
public Result<Book> listBooks(String startTitle) {
    Query booksQuery = booksCollection.orderBy("title").limit(10);
    if (startTitle != null) {
        booksQuery = booksQuery.startAfter(startTitle);
    }
    try {
        QuerySnapshot snapshot = booksQuery.get().get();
        List<Book> results = documentsToBooks(snapshot.getDocuments());
        String newCursor = null;
        if (results.size() > 0) {
            newCursor = results.get(results.size() - 1).getTitle();
        }
        return new Result<>(results, newCursor);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    return new Result<>(Lists.newArrayList(), null);
}
Also used : Query(com.google.cloud.firestore.Query) Book(com.example.getstarted.objects.Book) ExecutionException(java.util.concurrent.ExecutionException) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) Result(com.example.getstarted.objects.Result)

Aggregations

Query (com.google.cloud.firestore.Query)31 CollectionReference (com.google.cloud.firestore.CollectionReference)15 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)12 Test (org.junit.Test)12 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)6 HashSet (java.util.HashSet)3 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)2 ExecutionException (java.util.concurrent.ExecutionException)2 FirestoreException (com.google.cloud.firestore.FirestoreException)1 ListenerRegistration (com.google.cloud.firestore.ListenerRegistration)1 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1