Usamos o AllowFiltering no Repository, igual vimos no comando do cqlsh. Utilizaremos também, os query methods. Spring Data Cassandra Query Methods.
public interface ProductRepository extends CassandraRepository<Product, UUID> {
@AllowFiltering
List<Product> findByDepartment(String department);
}
Caso o text que passamos no Controller seja vazio ou seja: "", retornamos um findAll. Se não, mandamos a busca customizada do repository.
public List<ProductDTO> findByDepartment(String text) {
List<Product> list;
if ("".equals(text)) {
list = repository.findAll();
} else {
list = repository.findByDepartment(text);
}
return list.stream().map(ProductDTO::new).toList();
}
Caso a description/text seja diferente de nula (preenchida), realizamos o findByDepartment do Service.
Caso seja de fato nula, utilzaremos o findAll do Service.
@GetMapping(params = "department")
public ResponseEntity<List<ProductDTO>> findByDepartment(
@RequestParam(name = "department", defaultValue = "") String description) {
if (description != null) {
List<ProductDTO> dto = productService.findByDepartment(description);
return ResponseEntity.ok().body(dto);
} else {
List<ProductDTO> dto = productService.findAll();
return ResponseEntity.ok().body(dto);
}
}