Search in sources :

Example 1 with FirestoreReactiveOperations

use of com.google.cloud.spring.data.firestore.FirestoreReactiveOperations in project spring-cloud-gcp by GoogleCloudPlatform.

the class FirestoreIntegrationTests method writeReadDeleteTest.

@Test
void writeReadDeleteTest() {
    User alice = new User("Alice", 29);
    User bob = new User("Bob", 60);
    this.firestoreTemplate.save(alice).block();
    this.firestoreTemplate.save(bob).block();
    assertThat(this.firestoreTemplate.findById(Mono.just("Saitama"), User.class).block()).isNull();
    assertThat(this.firestoreTemplate.findById(Mono.just("Bob"), User.class).block()).isEqualTo(bob);
    assertThat(this.firestoreTemplate.findAllById(Flux.just("Bob", "Saitama", "Alice"), User.class).collectList().block()).containsExactlyInAnyOrder(bob, alice);
    List<User> usersBeforeDelete = this.firestoreTemplate.findAll(User.class).collectList().block();
    assertThat(usersBeforeDelete).containsExactlyInAnyOrder(alice, bob);
    assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(2);
    this.firestoreTemplate.delete(Mono.just(bob)).block();
    assertThat(this.firestoreTemplate.count(User.class).block()).isEqualTo(1);
    assertThat(this.firestoreTemplate.existsById(Mono.just("Alice"), User.class).block()).isEqualTo(Boolean.TRUE);
    assertThat(this.firestoreTemplate.existsById(Mono.just("Bob"), User.class).block()).isEqualTo(Boolean.FALSE);
    this.firestoreTemplate.deleteById(Mono.just("Alice"), User.class).block();
    assertThat(this.firestoreTemplate.count(User.class).block()).isZero();
    alice.setUpdateTime(null);
    this.firestoreTemplate.save(alice).block();
    bob.setUpdateTime(null);
    this.firestoreTemplate.save(bob).block();
    assertThat(this.firestoreTemplate.deleteAll(User.class).block()).isEqualTo(2);
    assertThat(this.firestoreTemplate.findAll(User.class).collectList().block()).isEmpty();
    // tag::subcollection[]
    FirestoreReactiveOperations bobTemplate = this.firestoreTemplate.withParent(new User("Bob", 60));
    PhoneNumber phoneNumber = new PhoneNumber("111-222-333");
    bobTemplate.save(phoneNumber).block();
    assertThat(bobTemplate.findAll(PhoneNumber.class).collectList().block()).containsExactly(phoneNumber);
    bobTemplate.deleteAll(PhoneNumber.class).block();
    assertThat(bobTemplate.findAll(PhoneNumber.class).collectList().block()).isEmpty();
// end::subcollection[]
}
Also used : User(com.google.cloud.spring.data.firestore.entities.User) FirestoreReactiveOperations(com.google.cloud.spring.data.firestore.FirestoreReactiveOperations) PhoneNumber(com.google.cloud.spring.data.firestore.entities.PhoneNumber) Test(org.junit.jupiter.api.Test)

Example 2 with FirestoreReactiveOperations

use of com.google.cloud.spring.data.firestore.FirestoreReactiveOperations in project spring-cloud-gcp by GoogleCloudPlatform.

the class UserController method saveUser.

@PostMapping("/saveUser")
private Mono<User> saveUser(ServerWebExchange serverWebExchange) {
    return serverWebExchange.getFormData().flatMap(formData -> {
        User user = new User(formData.getFirst("name"), Integer.parseInt(formData.getFirst("age")), createPets(formData.getFirst("pets")));
        FirestoreReactiveOperations userTemplate = this.firestoreTemplate.withParent(user);
        List<PhoneNumber> phones = getPhones(formData.getFirst("phones"));
        return userTemplate.saveAll(Flux.fromIterable(phones)).then(userRepository.save(user));
    });
}
Also used : FirestoreReactiveOperations(com.google.cloud.spring.data.firestore.FirestoreReactiveOperations) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

FirestoreReactiveOperations (com.google.cloud.spring.data.firestore.FirestoreReactiveOperations)2 PhoneNumber (com.google.cloud.spring.data.firestore.entities.PhoneNumber)1 User (com.google.cloud.spring.data.firestore.entities.User)1 Test (org.junit.jupiter.api.Test)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1