Skip to content

Instantly share code, notes, and snippets.

@joshlong
Created December 4, 2018 00:48
Show Gist options
  • Select an option

  • Save joshlong/6b7fcf77fbbf1dc9908158f6aadbde92 to your computer and use it in GitHub Desktop.

Select an option

Save joshlong/6b7fcf77fbbf1dc9908158f6aadbde92 to your computer and use it in GitHub Desktop.
package com.example.reservationservice;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.data.annotation.Id;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import java.time.Duration;
@SpringBootApplication
public class ReservationServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ReservationServiceApplication.class, args);
}
}
/**
* create primary index on reservations ;
*/
@Log4j2
@Component
class DataInitializer {
private final ReservationRepository reservationRepository;
DataInitializer(ReservationRepository reservationRepository) {
this.reservationRepository = reservationRepository;
}
@EventListener(ApplicationReadyEvent.class)
public void listen() throws Exception {
Flux<Reservation> reservationFlux =
Flux
.just("Laurent", "Josh", "Mario", "Jane")
.map(name -> new Reservation(null, name))
.flatMap(this.reservationRepository::save);
this.reservationRepository
.findAll().flatMap(this.reservationRepository::delete)
.thenMany(reservationFlux)
.thenMany(this.reservationRepository.findAll())
.subscribe(log::info);
}
}
@Configuration
class R2dbcConfiguration {
@Bean
ReservationRepository reservationRepository(R2dbcRepositoryFactory rf) {
return rf.getRepository(ReservationRepository.class);
}
@Bean
R2dbcRepositoryFactory r2dbcRepositoryFactory(DatabaseClient dbc) {
RelationalMappingContext rmc = new RelationalMappingContext();
rmc.afterPropertiesSet();
return new R2dbcRepositoryFactory(dbc, rmc);
}
@Bean
DatabaseClient databaseClient(ConnectionFactory cf) {
return DatabaseClient.create(cf);
}
@Bean
PostgresqlConnectionFactory postgresqlConnectionFactory() {
PostgresqlConnectionConfiguration build = PostgresqlConnectionConfiguration.builder()
.host("localhost")
.username("orders")
.password("0rd3rs")
.database("orders")
.build();
return new PostgresqlConnectionFactory(build);
}
}
/*
@Configuration
class ILoveCouchbase implements CouchbaseConfigurer {
@Override
public CouchbaseEnvironment couchbaseEnvironment() throws Exception {
return DefaultCouchbaseEnvironment.create();
}
@Override
public Cluster couchbaseCluster() throws Exception {
CouchbaseCluster localhost = CouchbaseCluster.create(couchbaseEnvironment(), "localhost");
localhost.authenticate("jlong", "password");
return localhost;
}
@Override
public ClusterInfo couchbaseClusterInfo() throws Exception {
return couchbaseCluster().clusterManager("Administrator", "asdasd").info();
}
@Override
public Bucket couchbaseClient() throws Exception {
return couchbaseCluster().openBucket("reservations");
}
}
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
//@Document
class Reservation {
@Id
private Integer id;
private String name;
}
//@ViewIndexed(designDoc = "reservation", viewName = "all")
interface ReservationRepository extends ReactiveCrudRepository<Reservation, String> {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment