Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'juan'
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas0headshot committed May 23, 2024
2 parents c8d5b0f + 1bde52a commit 19f114a
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 15 deletions.
28 changes: 27 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,33 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>5.0.0</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-sql-spring</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.senac.gestaocurso.enterprise;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.PathBuilder;


import java.lang.reflect.Field;
import java.time.LocalDate;


public class BooleanBuilderUtil {


public static BooleanBuilder buildPredicateFromFilter(String filter, Class<?> classes) {
if (filter == null || filter.isEmpty()) {
return new BooleanBuilder();
}


BooleanBuilder predicate = new BooleanBuilder();
String[] parts = filter.split("\\+");


if (parts.length == 3) {
try {
Field field = getFieldRecursively(classes, parts[0]);
field.setAccessible(true);
Class<?> fieldType = field.getType();
PathBuilder<?> fieldPath = new PathBuilder<>(fieldType, field.getName());


switch (parts[1].toLowerCase()) {
case "equal":
predicate.and(fieldPath.eq(Expressions.constant(parts[2])));
break;
case "notEqual":
predicate.and(fieldPath.ne(Expressions.constant(parts[2])));
break;
case "greater":
predicate.and(Expressions.booleanTemplate("{0} > {1}", fieldPath, getType(fieldType, parts[2])));
break;
case "lesser":
predicate.and(Expressions.booleanTemplate("{0} < {1}", fieldPath, getType(fieldType, parts[2])));
break;
case "greaterequal":
predicate.and(Expressions.booleanTemplate("{0} >= {1}", fieldPath, getType(fieldType, parts[2])));
break;
case "lesserequal":
predicate.and(Expressions.booleanTemplate("{0} <= {1}", fieldPath, getType(fieldType, parts[2])));
break;
case "like":
predicate.and(Expressions.booleanTemplate("{0} like '%'||{1}||'%'", fieldPath, Expressions.constant(parts[2])));
break;
default:
throw new RuntimeException("Operador não suportado");
}


} catch (NoSuchFieldException e) {
throw new RuntimeException("Campo não encontrado");
} catch (Exception e) {
throw new RuntimeException("Acesso ilegal ao campo");
}
}


if (parts.length == 4) {
try {
Field field = getFieldRecursively(classes, parts[0]);
field.setAccessible(true);
Class<?> fieldType = field.getType();
PathBuilder<?> fieldPath = new PathBuilder<>(fieldType, field.getName());


switch (parts[1].toLowerCase()) {
case "between":
predicate.and(Expressions.booleanTemplate("{0} >= {1} AND {0} <= {2}", fieldPath, getType(fieldType, parts[2]), getType(fieldType, parts[3])));
break;
default:
throw new RuntimeException("Operador não suportado");
}


} catch (NoSuchFieldException e) {
throw new RuntimeException("Campo não encontrado");
} catch (Exception e) {
throw new RuntimeException("Acesso ilegal ao campo");
}
}


return predicate;
}




public static Expression getType(Class<?> fieldType, String part) {
if (fieldType == Integer.class || fieldType == int.class) {
return Expressions.constant(Integer.parseInt(part));
} else if (fieldType == Double.class || fieldType == double.class) {
return Expressions.constant(Double.parseDouble(part));
} else if (fieldType == LocalDate.class) {
return Expressions.constant(LocalDate.parse(part));
}
return Expressions.constant(part);
}


private static Field getFieldRecursively(Class<?> classes, String fieldName) throws NoSuchFieldException {
try {
return classes.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
if (classes.getSuperclass() != null) {
return getFieldRecursively(classes.getSuperclass(), fieldName);
} else {
throw e;
}
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.senac.gestaocurso.enterprise;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;


import java.util.List;


public interface CustomQuerydslPredicateExecutor<T> extends QuerydslPredicateExecutor<T> {


@Override
List<T> findAll(Predicate predicate);


default Page<T> findAll(String filter, Class<T> entityType, Pageable pageable) {
BooleanBuilder booleanBuilder = BooleanBuilderUtil.buildPredicateFromFilter(filter, entityType);
return this.findAll(booleanBuilder, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Aula;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AulaRepository extends JpaRepository<Aula, Long> {
public interface AulaRepository extends JpaRepository<Aula, Long>, CustomQuerydslPredicateExecutor<Aula> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Avaliacao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AvaliacaoRepository extends JpaRepository<Avaliacao, Long> {
public interface AvaliacaoRepository extends JpaRepository<Avaliacao, Long>, CustomQuerydslPredicateExecutor<Avaliacao> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Cargo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CargoRepository extends JpaRepository<Cargo, Long> {
public interface CargoRepository extends JpaRepository<Cargo, Long>, CustomQuerydslPredicateExecutor<Cargo> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Certificacoes;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CertifcacoesRepository extends JpaRepository<Certificacoes, Long>{
public interface CertifcacoesRepository extends JpaRepository<Certificacoes, Long>, CustomQuerydslPredicateExecutor<Certificacoes> {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Curso;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CursoRepository extends JpaRepository<Curso, Long> {
public interface CursoRepository extends JpaRepository<Curso, Long>, CustomQuerydslPredicateExecutor<Curso> {

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.DadosBancarios;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DadosBancariosRepository extends JpaRepository <DadosBancarios, Long> {
public interface DadosBancariosRepository extends JpaRepository <DadosBancarios, Long>, CustomQuerydslPredicateExecutor<DadosBancarios> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Dependentes;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DependentesRepository extends JpaRepository<Dependentes, Long> {
public interface DependentesRepository extends JpaRepository<Dependentes, Long>, CustomQuerydslPredicateExecutor<Dependentes> {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.ExpAnterior;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ExpAnteriorRepository extends JpaRepository<ExpAnterior, Long> {
public interface ExpAnteriorRepository extends JpaRepository<ExpAnterior, Long>, CustomQuerydslPredicateExecutor<ExpAnterior> {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Filiacao;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FiliacaoRepository extends JpaRepository<Filiacao, Long> {
public interface FiliacaoRepository extends JpaRepository<Filiacao, Long>, CustomQuerydslPredicateExecutor<Filiacao> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Frequencia;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FrequenciaRepository extends JpaRepository<Frequencia, Long> {
public interface FrequenciaRepository extends JpaRepository<Frequencia, Long>, CustomQuerydslPredicateExecutor<Frequencia> {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Funcionario;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FuncionarioRepository extends JpaRepository <Funcionario, Long> {
public interface FuncionarioRepository extends JpaRepository <Funcionario, Long>, CustomQuerydslPredicateExecutor<Funcionario> {

Funcionario findByCpf(String cpf);
Funcionario findByRg(String rg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Inscricao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

@Service
public interface InscricaoRepository extends JpaRepository<Inscricao, Long> {
public interface InscricaoRepository extends JpaRepository<Inscricao, Long>, CustomQuerydslPredicateExecutor<Inscricao> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Materia;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MateriaRepository extends JpaRepository<Materia, Long> {
public interface MateriaRepository extends JpaRepository<Materia, Long>, CustomQuerydslPredicateExecutor<Materia> {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.senac.gestaocurso.repository;

import com.senac.gestaocurso.enterprise.CustomQuerydslPredicateExecutor;
import com.senac.gestaocurso.models.Turma;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TurmaRepository extends JpaRepository<Turma, Long> {
public interface TurmaRepository extends JpaRepository<Turma, Long>, CustomQuerydslPredicateExecutor<Turma> {
}

0 comments on commit 19f114a

Please sign in to comment.