
driven-adapter.r2dbc-postgresql.helper.reactive-adapter-operations.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scaffold-clean-architecture Show documentation
Show all versions of scaffold-clean-architecture Show documentation
Gradle plugin to create a clean application in Java that already works, It follows our best practices!
package {{package}}.r2dbc.helper;
import org.reactivecommons.utils.ObjectMapper;
import org.springframework.data.domain.Example;
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.lang.reflect.ParameterizedType;
import java.util.function.Function;
public abstract class ReactiveAdapterOperations & ReactiveQueryByExampleExecutor> {
protected R repository;
protected ObjectMapper mapper;
private final Class dataClass;
private final Function toEntityFn;
@SuppressWarnings("unchecked")
protected ReactiveAdapterOperations(R repository, ObjectMapper mapper, Function toEntityFn) {
this.repository = repository;
this.mapper = mapper;
ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
this.dataClass = (Class) genericSuperclass.getActualTypeArguments()[1];
this.toEntityFn = toEntityFn;
}
protected D toData(E entity) {
return mapper.map(entity, dataClass);
}
protected E toEntity(D data) {
return data != null ? toEntityFn.apply(data) : null;
}
public Mono save(E entity) {
return saveData(toData(entity))
.map(this::toEntity);
}
protected Flux saveAllEntities(Flux entities) {
return saveData(entities.map(this::toData))
.map(this::toEntity);
}
protected Mono saveData(D data) {
return repository.save(data);
}
protected Flux saveData(Flux data) {
return repository.saveAll(data);
}
public Mono findById(I id) {
return repository.findById(id).map(this::toEntity);
}
public Flux findByExample(E entity) {
return repository.findAll(Example.of(toData(entity)))
.map(this::toEntity);
}
public Flux findAll() {
return repository.findAll()
.map(this::toEntity);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy