Skip to content

Commit

Permalink
versão 2.0-Alpha1 e #38 Filtro para Leis na tela de autores
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-hos committed Nov 20, 2017
1 parent 6056ace commit 63cd4b3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.sjcdigital.temis</groupId>
<artifactId>temis-api</artifactId>
<packaging>war</packaging>
<version>2.0.0-Alpha1</version>
<version>2.0-Alpha1</version>
<name>temis-api</name>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public List<Lei> doAutor(final Long idAutor, int total, int pg) {

// Sem ser por autor

public List<Lei> filtraPaginado(final Long idSituacao, final Long idClasse, final Long idTipo, final Integer ano, final int total, final int pg) {
public List<Lei> filtraPaginado( final Long idSituacao, final Long idClasse, final Long idTipo, final Integer ano, final Long idAutor,
final int total, final int pg) {

StringBuilder sql = new StringBuilder("SELECT lei FROM Lei lei ");
boolean and = false;
Expand Down Expand Up @@ -100,6 +101,15 @@ public List<Lei> filtraPaginado(final Long idSituacao, final Long idClasse, fina

}

if(Objects.nonNull(idAutor)) {
adicionaWhereSeNecessario(sql, where);
adicionaAndSeNecessario(sql, and);
and = true;
where = true;
sql.append("lei.autor.id = :idAutor ");

}

TypedQuery<Lei> query = em.createQuery(sql.toString(), Lei.class);

if(Objects.nonNull(idTipo)) {
Expand All @@ -118,6 +128,10 @@ public List<Lei> filtraPaginado(final Long idSituacao, final Long idClasse, fina
query.setParameter("ano", ano);
}

if(Objects.nonNull(idAutor)) {
query.setParameter("idAutor", idAutor);
}

query.setFirstResult(pg * total);
query.setMaxResults(total);

Expand Down Expand Up @@ -164,9 +178,24 @@ public List<DataChart> contaLeisPorClasse() {
return query.getResultList();
}

public Anos anos() {
TypedQuery<Ano> query = em.createQuery("SELECT DISTINCT new com.sjcdigital.temis.model.dto.Ano(lei.ano) FROM Lei lei ORDER BY lei.ano", Ano.class);
public Anos anos(final Long idAutor) {
StringBuilder sql = new StringBuilder("SELECT DISTINCT new com.sjcdigital.temis.model.dto.Ano(lei.ano) FROM Lei lei ");

if(Objects.nonNull(idAutor)) {
sql.append("WHERE lei.autor.id = :idAutor ");

}

sql.append("ORDER BY lei.ano");

TypedQuery<Ano> query = em.createQuery(sql.toString(), Ano.class);

if(Objects.nonNull(idAutor)) {
query.setParameter("idAutor", idAutor);
}

List<Ano> anos = query.getResultList();

return new Anos(anos);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/sjcdigital/temis/resources/LeiResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface LeiResource {
@GET
@Path("/filtra")
Response filtraPaginado( @QueryParam("idSituacao") Long idSituacao, @QueryParam("idClasse") Long idClasse,
@QueryParam("idTipo") Long idTipo, @QueryParam("ano") Integer ano,
@QueryParam("idTipo") Long idTipo, @QueryParam("ano") Integer ano, @QueryParam("idAutor") Long idAutor,
@QueryParam("total") int total, @QueryParam("pg") int pg );

@GET
Expand All @@ -38,7 +38,7 @@ Response filtraPaginado( @QueryParam("idSituacao") Long idSituacao, @QueryParam(

@GET
@Path("/anos")
Response buscaAnos();
Response buscaAnos(@QueryParam("idAutor") Long idAutor);

@PUT
@Path("/{id}/vota")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public Response graficos() {
}

@Override
public Response filtraPaginado(Long idSituacao, Long idClasse, Long idTipo, Integer ano, int total, int pg) {
List<Lei> leisFiltrada = RESTUtils.lanca404SeNulo(leis.filtraPaginado(idSituacao, idClasse, idTipo, ano, total, pg));
public Response filtraPaginado(Long idSituacao, Long idClasse, Long idTipo, Integer ano, Long idAutor, int total, int pg) {
List<Lei> leisFiltrada = RESTUtils.lanca404SeNulo(leis.filtraPaginado(idSituacao, idClasse, idTipo, ano, idAutor, total, pg));
return Response.ok().entity(leisFiltrada).build();
}

@Override
public Response buscaAnos() {
return Response.ok().entity(leis.anos()).build();
public Response buscaAnos(final Long idAutor) {
return Response.ok().entity(leis.anos(idAutor)).build();
}

}

0 comments on commit 63cd4b3

Please sign in to comment.