Search in sources :

Example 6 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project Protego by nickdlc.

the class ProtegoHomeController method getUser.

@GetMapping("/user")
public ProtegoUser getUser(@RequestParam("user") String uid) {
    // Get user with id uid
    try {
        // Temporarily create a new doctor and return that
        FirebaseAttributes.firestore.collection("users").document(uid).get();
        // Asynchronously retrieve multiple documents
        ApiFuture<DocumentSnapshot> future = FirebaseAttributes.firestore.collection("users").document(uid).get();
        // future.get() blocks on response
        DocumentSnapshot document = future.get();
        return document.toObject(ProtegoUser.class);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project Protego by nickdlc.

the class ProtegoHomeController method getDoctor.

@GetMapping("/doctor")
public Doctor getDoctor(@RequestParam("doctor") String duid) {
    // Get doctor with id duid
    try {
        // Temporarily create a new doctor and return that
        FirebaseAttributes.firestore.collection("users").document(duid).get();
        // Asynchronously retrieve multiple documents
        ApiFuture<DocumentSnapshot> future = FirebaseAttributes.firestore.collection("users").document(duid).get();
        // future.get() blocks on response
        DocumentSnapshot document = future.get();
        return document.toObject(Doctor.class);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class ITSystemTest method deleteNestedFieldUsingFieldPath.

@Test
public void deleteNestedFieldUsingFieldPath() throws Exception {
    DocumentReference documentReference = randomColl.document("doc1");
    documentReference.set(map("a.b", SINGLE_FILED_MAP_WITH_DOT)).get();
    DocumentSnapshot documentSnapshots = documentReference.get().get();
    assertEquals(SINGLE_FILED_MAP_WITH_DOT, documentSnapshots.getData().get("a.b"));
    FieldPath path = FieldPath.of("a.b", "c.d");
    documentReference.update(path, FieldValue.delete()).get();
    documentSnapshots = documentReference.get().get();
    assertNull(documentSnapshots.getData().get("c.d"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) FieldPath(com.google.cloud.firestore.FieldPath) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Example 9 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class ITSystemTest method updateDocument.

@Test
public void updateDocument() throws Exception {
    AllSupportedTypes expectedResult = new AllSupportedTypes();
    WriteResult writeResult = randomDoc.set(ALL_SUPPORTED_TYPES_MAP).get();
    DocumentSnapshot documentSnapshot = randomDoc.get().get();
    assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
    randomDoc.update(Precondition.updatedAt(writeResult.getUpdateTime()), "foo", "updated").get();
    documentSnapshot = randomDoc.get().get();
    expectedResult.foo = "updated";
    assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
    expectedResult.model = ImmutableMap.of("foo", UPDATE_SINGLE_FIELD_OBJECT.foo);
    randomDoc.update("model", UPDATE_SINGLE_FIELD_OBJECT).get();
    documentSnapshot = randomDoc.get().get();
    assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) WriteResult(com.google.cloud.firestore.WriteResult) AllSupportedTypes(com.google.cloud.firestore.LocalFirestoreHelper.AllSupportedTypes) Test(org.junit.Test)

Example 10 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class ITSystemTest method writeBatch.

@Test
public void writeBatch() throws Exception {
    DocumentReference createDocument = randomColl.document();
    DocumentReference setDocument = randomColl.document();
    DocumentReference updateDocument = addDocument("foo", "bar");
    DocumentReference deleteDocument = addDocument("foo", "bar");
    WriteBatch batch = firestore.batch();
    batch.create(createDocument, SINGLE_FIELD_OBJECT);
    batch.set(setDocument, ALL_SUPPORTED_TYPES_OBJECT);
    batch.update(updateDocument, "foo", "foobar");
    batch.delete(deleteDocument);
    batch.commit().get();
    List<DocumentSnapshot> documentSnapshots = firestore.getAll(createDocument, setDocument, updateDocument, deleteDocument).get();
    Iterator<DocumentSnapshot> iterator = documentSnapshots.iterator();
    assertEquals(SINGLE_FIELD_OBJECT, iterator.next().toObject(SingleField.class));
    assertEquals(ALL_SUPPORTED_TYPES_OBJECT, iterator.next().toObject(AllSupportedTypes.class));
    assertEquals("foobar", iterator.next().get("foo"));
    assertFalse(iterator.next().exists());
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) AllSupportedTypes(com.google.cloud.firestore.LocalFirestoreHelper.AllSupportedTypes) WriteBatch(com.google.cloud.firestore.WriteBatch) DocumentReference(com.google.cloud.firestore.DocumentReference) SingleField(com.google.cloud.firestore.LocalFirestoreHelper.SingleField) Test(org.junit.Test)

Aggregations

DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)103 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)73 Test (org.junit.Test)52 DocumentReference (com.google.cloud.firestore.DocumentReference)49 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)23 ExecutionException (java.util.concurrent.ExecutionException)13 CollectionReference (com.google.cloud.firestore.CollectionReference)12 Query (com.google.cloud.firestore.Query)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)10 SingleField (com.google.cloud.firestore.LocalFirestoreHelper.SingleField)9 WriteResult (com.google.cloud.firestore.WriteResult)9 ArrayList (java.util.ArrayList)8 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)6 HashMap (java.util.HashMap)6 BulkWriter (com.google.cloud.firestore.BulkWriter)5 City (com.example.firestore.snippets.model.City)4 Timestamp (com.google.cloud.Timestamp)4 FirestoreBundleTest.verifyNamedQuery (com.google.cloud.firestore.FirestoreBundleTest.verifyNamedQuery)4 AllSupportedTypes (com.google.cloud.firestore.LocalFirestoreHelper.AllSupportedTypes)4 WriteBatch (com.google.cloud.firestore.WriteBatch)4