Search in sources :

Example 1 with FirestoreReactiveOperations

use of org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations in project spring-cloud-gcp by spring-cloud.

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(org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with FirestoreReactiveOperations

use of org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations in project spring-cloud-gcp by spring-cloud.

the class FirestoreIntegrationTests method writeReadDeleteTest.

@Test
public 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(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()).isEqualTo(0);
    this.firestoreTemplate.save(alice).block();
    this.firestoreTemplate.save(bob).block();
    assertThat(this.firestoreTemplate.deleteAll(User.class).block()).isEqualTo(2);
    assertThat(usersBeforeDelete).containsExactlyInAnyOrder(alice, bob);
    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(org.springframework.cloud.gcp.data.firestore.entities.User) FirestoreReactiveOperations(org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations) PhoneNumber(org.springframework.cloud.gcp.data.firestore.entities.PhoneNumber) Test(org.junit.Test)

Aggregations

FirestoreReactiveOperations (org.springframework.cloud.gcp.data.firestore.FirestoreReactiveOperations)2 Test (org.junit.Test)1 PhoneNumber (org.springframework.cloud.gcp.data.firestore.entities.PhoneNumber)1 User (org.springframework.cloud.gcp.data.firestore.entities.User)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1