
driven-adapter.redis-reactive.redis-repository.helper.reactive-repository-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}}.redis.repository.helper;
import org.reactivecommons.utils.ObjectMapper;
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;
import static org.springframework.data.domain.Example.of;
public abstract class ReactiveRepositoryAdapterOperations & ReactiveQueryByExampleExecutor> {
protected R repository;
protected ObjectMapper mapper;
private final Class dataClass;
private final Function toEntityFn;
@SuppressWarnings("unchecked")
public ReactiveRepositoryAdapterOperations(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;
}
public Mono save(E entity) {
return Mono.just(entity)
.map(this::toData)
.flatMap(this::saveData)
.map(this::toEntity);
}
public Flux saveAll(Flux entities) {
return doQueryMany(repository.saveAll(entities.map(this::toData)));
}
public Mono findById(I id) {
return doQuery(repository.findById(id));
}
public Flux findByExample(E entity) {
return doQueryMany(repository.findAll(of(toData(entity))));
}
public Mono deleteById(I id) {
return repository.deleteById(id);
}
public Flux findAll() {
return doQueryMany(repository.findAll());
}
protected Mono doQuery(Mono query) {
return query.map(this::toEntity);
}
protected Flux doQueryMany(Flux query) {
return query.map(this::toEntity);
}
protected Mono saveData(D data) {
return repository.save(data);
}
protected D toData(E entity) {
return mapper.map(entity, dataClass);
}
protected E toEntity(D data) {
return toEntityFn.apply(data);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy