forked from joshlong-attic/matt-and-josh-react-reactively
/
SampleDataInitializer.java
36 lines (30 loc) · 1.16 KB
/
SampleDataInitializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example.demo;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import java.util.UUID;
@Log4j2 // <1>
@Component
@org.springframework.context.annotation.Profile("demo") // <2>
class SampleDataInitializer
implements ApplicationListener<ApplicationReadyEvent> {
private final ProfileRepository repository; // <3>
public SampleDataInitializer(ProfileRepository repository) {
this.repository = repository;
}
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
repository
.deleteAll() // <4>
.thenMany(
Flux
.just("A", "B", "C", "D") // <5>
.map(name -> new Profile(UUID.randomUUID().toString(), name + "@email.com")) // <6>
.flatMap(repository::save) // <7>
)
.thenMany(repository.findAll()) // <8>
.subscribe(profile -> log.info("saving " + profile.toString())); // <9>
}
}