ph.com.nightowlstudios.repository.Repository Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edge Show documentation
Show all versions of edge Show documentation
A simple library for building REST API using Vertx.
package ph.com.nightowlstudios.repository;
import io.vertx.core.Future;
import io.vertx.sqlclient.Row;
import ph.com.nightowlstudios.dto.DTO;
import ph.com.nightowlstudios.entity.Entity;
import ph.com.nightowlstudios.persistence.Collectors;
import ph.com.nightowlstudios.persistence.PersistenceClient;
import ph.com.nightowlstudios.persistence.query.Query;
import ph.com.nightowlstudios.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collector;
/**
* @author Joseph Harvey Angeles - @yev
* @since 7/4/20
*/
public abstract class Repository {
private final PersistenceClient dbClient;
public Repository() {
this.dbClient = new PersistenceClient();
}
public Repository(PersistenceClient dbClient) {
this.dbClient = dbClient;
}
protected PersistenceClient db() {
return this.dbClient;
}
public Future> findOneById(Class entityClass, UUID id) {
return findOneById(entityClass, id, Collectors.ofEntities(entityClass));
}
public Future> findOneById(Class entityClass, String id) {
return findOneById(entityClass, UUID.fromString(id), Collectors.ofEntities(entityClass));
}
public Future> findOneById(Class entityClass, UUID id, Function rowMapper) {
return findOne(Query.select(entityClass, id), rowMapper);
}
public Future> findOneById(Class entityClass, String id, Function rowMapper) {
return findOneById(entityClass, UUID.fromString(id), rowMapper);
}
public Future> findOneById(Class entityClass, UUID id, Collector> collector) {
return db()
.query(
Query.select(entityClass, id),
collector
).map(Utils::getFirstElement);
}
public Future> findMany(Query query, Collector> collector) {
return db().query(query, collector);
}
public Future> findMany(Query query, Function rowMapper) {
return db().query(query, collect(rowMapper));
}
protected Future> findOne(Query q, Function rowMapper) {
return db().query(q, collect(rowMapper)).map(Utils::getFirstElement);
}
protected Collector> collect(Function rowMapper) {
return Collector.of(
ArrayList::new,
(list, row) -> list.add(rowMapper.apply(row)),
(first, second) -> {
first.addAll(second);
return first;
}
);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy