Search in sources :

Example 11 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippetsIT method testAddDocWithAutoGenId.

@Test
public void testAddDocWithAutoGenId() throws Exception {
    String autoId = manageDataSnippets.addDocumentDataWithAutoGeneratedId();
    City city = new City("Tokyo");
    city.setCountry("Japan");
    DocumentReference docRef = db.collection("cities").document(autoId);
    assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
}
Also used : City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 12 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippetsIT method testUpdateSimpleDocument.

@Test
public void testUpdateSimpleDocument() throws Exception {
    manageDataSnippets.updateSimpleDocument();
    DocumentReference docRef = db.collection("cities").document("DC");
    City city = new City("Washington D.C.");
    city.setCapital(true);
    assertTrue(Objects.equals(city, getDocumentDataAsCity(docRef)));
}
Also used : City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 13 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class RetrieveDataSnippets method prepareExamples.

/**
 * Create cities collection and add sample documents.
 */
void prepareExamples() throws Exception {
    // [START fs_retrieve_create_examples]
    CollectionReference cities = db.collection("cities");
    List<ApiFuture<WriteResult>> futures = new ArrayList<>();
    futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L)));
    futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L)));
    futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L)));
    futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L)));
    futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L)));
    // (optional) block on operation
    ApiFutures.allAsList(futures).get();
// [END fs_retrieve_create_examples]
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ArrayList(java.util.ArrayList) City(com.example.firestore.snippets.model.City) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 14 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method updateSimpleDocument.

/**
 * Partially update a document using the .update(field1, value1..) method.
 */
void updateSimpleDocument() throws Exception {
    db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
    // [START fs_update_doc]
    // Update an existing document
    DocumentReference docRef = db.collection("cities").document("DC");
    // (async) Update one field
    ApiFuture<WriteResult> future = docRef.update("capital", true);
    // ...
    WriteResult result = future.get();
    System.out.println("Write result: " + result);
// [END fs_update_doc]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 15 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method addDocumentDataAfterAutoGeneratingId.

/**
 * Add data to a document after generating the document id.
 *
 * @return auto generated id
 */
String addDocumentDataAfterAutoGeneratingId() throws Exception {
    City data = new City();
    // [START fs_add_doc_data_after_auto_id]
    // Add document data after generating an id.
    DocumentReference addedDocRef = db.collection("cities").document();
    System.out.println("Added document with ID: " + addedDocRef.getId());
    // later...
    ApiFuture<WriteResult> writeResult = addedDocRef.set(data);
    // [END fs_add_doc_data_after_auto_id]
    // writeResult.get() blocks on operation
    System.out.println("Update time : " + writeResult.get().getUpdateTime());
    return addedDocRef.getId();
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

City (com.example.firestore.snippets.model.City)16 DocumentReference (com.google.cloud.firestore.DocumentReference)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)6 WriteResult (com.google.cloud.firestore.WriteResult)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 ApiFuture (com.google.api.core.ApiFuture)2 CollectionReference (com.google.cloud.firestore.CollectionReference)2 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)2 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)2 HashMap (java.util.HashMap)2 Transaction (com.google.cloud.firestore.Transaction)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 List (java.util.List)1