package br.com.alura.forum.controller;
import java.net.URI; import java.util.List;
import javax.transaction.Transactional; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.util.UriComponentsBuilder;
import br.com.alura.forum.controller.dto.DetalhesDoTopicoDto; import br.com.alura.forum.controller.dto.TopicoDto; import br.com.alura.forum.controller.form.AtualizarTopicoForm; import br.com.alura.forum.controller.form.TopicoForm; import br.com.alura.forum.modelo.Topico; import br.com.alura.forum.repository.CursoRepository; import br.com.alura.forum.repository.TopicoRepository;
@RestController @RequestMapping("/topicos") public class TopicoController {
@Autowired
private TopicoRepository topicoRepository;
@Autowired
private CursoRepository cursoRepository;
@GetMapping
public List<TopicoDto> topicos(String nomeCurso) {
if (nomeCurso == null) {
List<Topico> topicos = topicoRepository.findAll();
return TopicoDto.converter(topicos);
} else {
List<Topico> topicos = topicoRepository.findByCursoNome(nomeCurso);
return TopicoDto.converter(topicos);
}
}
@PostMapping
public ResponseEntity<TopicoDto> cadastrar(@RequestBody @Valid TopicoForm form, UriComponentsBuilder uriBuilder) {
Topico topico = form.converter(cursoRepository);
topicoRepository.save(topico);
URI uri = uriBuilder.path("/topicos/{id}").buildAndExpand(topico.getId()).toUri();
return ResponseEntity.created(uri).body(new TopicoDto(topico));
}
@GetMapping("/{id}")
public DetalhesDoTopicoDto detalhar(@PathVariable Long id) {
Topico topico = topicoRepository.getById(id);
return new DetalhesDoTopicoDto(topico);
}
@PutMapping("/{id}")
@Transactional
ResponseEntity<TopicoDto>atualizar(@PathVariable Long id, @RequestBody @Valid AtualizarTopicoForm form){
Topico topico=form.Atualizar(id,topicoRepository);
return ResponseEntity.ok(new TopicoDto(topico));
}
}