Search in sources :

Example 16 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippetsIT method testOrderByNameDescWithLimitQuery.

@Test
public void testOrderByNameDescWithLimitQuery() throws Exception {
    Query q = queryDataSnippets.createOrderByNameDescWithLimitQuery();
    List<String> result = getResults(q);
    List<String> expectedResults = Arrays.asList("DC", "TOK", "SF");
    assertTrue(Objects.equals(result, expectedResults));
}
Also used : Query(com.google.cloud.firestore.Query) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 17 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippetsIT method testInvalidRangeWithOrderByQuery.

@Test(expected = Exception.class)
public void testInvalidRangeWithOrderByQuery() throws Exception {
    Query q = queryDataSnippets.createInvalidRangeWithOrderByQuery();
    getResults(q);
}
Also used : Query(com.google.cloud.firestore.Query) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 18 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippetsIT method testOrderByNameWithLimitQuery.

@Test
public void testOrderByNameWithLimitQuery() throws Exception {
    Query q = queryDataSnippets.createOrderByNameWithLimitQuery();
    List<String> result = getResults(q);
    List<String> expectedResults = Arrays.asList("BJ", "LA", "SF");
    assertEquals(result, expectedResults);
}
Also used : Query(com.google.cloud.firestore.Query) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 19 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippetsIT method testWhereWithOrderByAndLimitQuery.

@Test
public void testWhereWithOrderByAndLimitQuery() throws Exception {
    Query q = queryDataSnippets.createWhereWithOrderByAndLimitQuery();
    List<String> result = getResults(q);
    List<String> expectedResults = Arrays.asList("LA", "TOK");
    assertEquals(result, expectedResults);
}
Also used : Query(com.google.cloud.firestore.Query) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 20 with Query

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

the class FirestoreDao method listBooksByUser.

// [END bookshelf_firestore_list_books]
// [START bookshelf_firestore_list_by_user]
@Override
public Result<Book> listBooksByUser(String userId, String startTitle) {
    Query booksQuery = booksCollection.orderBy("title").whereEqualTo(Book.CREATED_BY_ID, userId).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