Created
June 27, 2023 15:53
-
-
Save DavidCabral/2c2acc21d9f6057cfef07b19b0180f2e to your computer and use it in GitHub Desktop.
This file contains 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
@Getter | |
@Setter | |
public class Filter { | |
private String nome; | |
private Boolean ativo; | |
private Boolean pai; | |
} |
This file contains 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
@RestController | |
@RequestMapping("api/unidades") | |
@RequiredArgsConstructor | |
public class UnidadeController { | |
private final UnidadeService service; | |
@GetMapping | |
public Page<Unidade> findAll(Filter filter, Pageable page) { | |
return service.findAll(filter, page); | |
} | |
} |
This file contains 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
public interface UnidadeDAO extends JpaRepository<Unidade, Long>, JpaSpecificationExecutor<Unidade> { | |
} |
This file contains 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
package br.upe.model.service; | |
import br.upe.model.dto.UnidadeDTO; | |
import br.upe.model.entity.Unidade; | |
import br.upe.model.entity.enums.Status; | |
import br.upe.model.repository.UnidadeDAO; | |
import br.upe.model.security.DTOService; | |
import br.upe.model.util.Filter; | |
import jakarta.persistence.EntityNotFoundException; | |
import lombok.RequiredArgsConstructor; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.Pageable; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import static br.upe.model.repository.specification.UnidadeSpecification; | |
@Service | |
@RequiredArgsConstructor | |
public class UnidadeService { | |
private final UnidadeDAO dao; | |
@Transactional(readOnly = true) | |
public Page<Unidade> findAll(Filter filter, Pageable page) { | |
return dao.findAll(UnidadeSpecification.filter(filter), page) | |
} | |
} |
This file contains 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
public class UnidadeSpecification { | |
public static Specification<Unidade> filter(Filter filtro) { | |
return (root, query, builder) -> { | |
var predicates = new ArrayList<Predicate>(); | |
if (filtro.getNome() != null) { | |
predicates.add(builder.like(builder.upper(root.get("nome")), "%" + filtro.getNome().toUpperCase() + "%")); | |
} | |
if (filtro.getAtivo() != null && filtro.getAtivo()) { | |
predicates.add(root.get("status").equal(filtro.getStatus())); | |
} | |
return builder.and(predicates.toArray(new Predicate[0])); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment