Boa Tarde,
Para entender melhor a questão do uso de filtros, resolvi adicionar outros três endpoints diferentes para teste.
package br.com.alura.forum.controller;
import br.com.alura.forum.model.Topico;
import br.com.alura.forum.model.dtos.TopicoDTO;
import br.com.alura.forum.repository.TopicoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController("/v2/topicos")
public class TopicosController {
@Autowired
private TopicoRepository topicoRepository;
@GetMapping
public ResponseEntity<List<TopicoDTO>> findAll() {
final List<TopicoDTO> topics = convert(topicoRepository.findAll());
if (topics.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(topics, HttpStatus.OK);
}
@GetMapping("/name/{name}")
public ResponseEntity<List<TopicoDTO>> findByName(@PathVariable("name") String name) {
final List<TopicoDTO> topics = convert(topicoRepository.findByTitulo(name));
if (topics.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(topics, HttpStatus.OK);
}
@GetMapping("/{courseName}")
public ResponseEntity<List<TopicoDTO>> findByCourse(@PathVariable("courseName") String courseName) {
final List<TopicoDTO> topics = convert(topicoRepository.findByCursoNome(courseName));
if (topics.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(topics, HttpStatus.OK);
}
@GetMapping("/categoria/{category}")
public ResponseEntity<List<TopicoDTO>> findByCategory(@PathVariable("category") String category) {
final List<TopicoDTO> topics = convert(topicoRepository.findByCursoCategoria(category));
if (topics.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(topics, HttpStatus.OK);
}
@PostMapping
public void save(Topico topico) {
}
private List<TopicoDTO> convert(List<Topico> topicos) {
TopicoDTO topicoDTO = new TopicoDTO();
return topicoDTO.convert(topicos);
}
}
A dúvida é a seguinte, toda vez que eu chamo os métodos findByName, findByCourse e findByCategory ambos sempre me retorna todos os dados sem filtro. É como se ele considerasse apenas o findAll
Poderia me ajudar?
Grato