Interface: public interface TagsDayRepositoryQuery {
public interface TagsDayRepositoryQuery {
public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter);
}
}
e a classe de implementação: public class TagsDayRepositoryImpl implements TagsDayRepositoryQuery{
public class TagsDayRepositoryImpl implements TagsDayRepositoryQuery{
@PersistenceContext
private EntityManager manager;
@Override
public List<TagsDay> filtrar(List<Long> ids, TagsDayFilter tagsDayFilter) {
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<TagsDay> criteria = builder.createQuery(TagsDay.class);
Root<TagsDay> root = criteria.from(TagsDay.class);
//criar as restrições
Predicate[] predicates = criarRestricoes(ids, tagsDayFilter, builder, root);
criteria.where(predicates);
TypedQuery<TagsDay> query = manager.createQuery(criteria);
return query.getResultList();
}
private Predicate[] criarRestricoes(List<Long> ids, TagsDayFilter tagsDayFilter, CriteriaBuilder builder, Root<TagsDay> root) {
List<Predicate> predicates = new ArrayList<>();
if(ids.size() > 0) {
for(int i = 0; i < ids.size(); i++) {
predicates.add(builder.equal(root.get("id"), ids.get(i))); //adiciona vários ids para consulta
}
}
if(tagsDayFilter.getDataInicial() != null) {
predicates.add(builder.greaterThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataInicial()));
}
if(tagsDayFilter.getDataFinal() != null) {
predicates.add(builder.lessThanOrEqualTo(root.get("data_coleta"), tagsDayFilter.getDataFinal()));
}
return predicates.toArray(new Predicate[predicates.size()]);
}
}
}
e na URL faço:
localhost:8080/tagsday/search?ids=1,2&dataInicial=2018-09-27&dataFinal=2018-09-28 localhost:8080/tagsday/search?ids=1,2&dataInicial=2018-09-27&dataFinal=2018-09-28