Last active
October 10, 2018 00:03
-
-
Save filipeovercom/476f2bdecf266368caf3183d6ebca797 to your computer and use it in GitHub Desktop.
Exemplo de uma consulta utilizando QueryDSL onde o resultado é transformado para um DTO.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public Page<TurmaListagemDTO> findAllByParams(ConsultaTurmasParams params, Pageable pageable) { | |
QTurma turma = QTurma.turma; | |
BooleanBuilder conditions = new BooleanBuilder(); | |
if (!Objects.isNull(params)) { | |
params.getNome() | |
.ifPresent(nome -> conditions.and(turma.nome.startsWithIgnoreCase(nome))); | |
params.getDisciplina().ifPresent(disciplina -> conditions.and(turma.disciplina.uuid.eq(disciplina))); | |
params.getCurso().ifPresent(curso -> conditions.and(turma.curso.uuid.eq(curso))); | |
params.getProfessor().ifPresent(professor -> conditions.and(turma.professor.uuid.eq(professor))); | |
params.getPeriodoLetivo().ifPresent(periodo -> conditions.and(turma.periodoLetivo.uuid.eq(periodo))); | |
} | |
conditions.and(turma.deleted.eq(false)); | |
JPQLQuery<TurmaListagemDTO> query = getQuerydsl().createQuery() | |
.select(Projections.constructor(TurmaListagemDTO.class, turma.uuid, turma.nome, | |
turma.curso.nome, turma.professor.nome, turma.disciplina.nome)) | |
.from(turma) | |
.where(conditions).orderBy(turma.dataHoraCadastro.desc()); | |
Page page = readPage((JPAQuery) query, pageable); | |
return page.map(transformPageTurmaListagemDTO()); | |
} | |
private Function<TurmaListagemDTO, TurmaListagemDTO> transformPageTurmaListagemDTO() { | |
return turma -> { | |
QPlanoEnsino planoEnsino = QPlanoEnsino.planoEnsino; | |
StatusPlanoEnsino status = getQuerydsl().createQuery().select(planoEnsino.status) | |
.from(planoEnsino) | |
.where(planoEnsino.turma.uuid.eq(turma.getUuid()) | |
.and(planoEnsino.deleted.eq(false))) | |
.orderBy(planoEnsino.dataHoraCadastro.desc()) | |
.fetchFirst(); | |
turma.setStatusPlanoEnsino(status); | |
return turma; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment