33import br .com .testesantanderway .config .security .AutenticacaoViaTokenFilter ;
44import br .com .testesantanderway .config .security .ServicoDeToken ;
55import br .com .testesantanderway .controller .form .GastoForm ;
6- import br .com .testesantanderway .dto .DetalheGastosDTO ;
7- import br .com .testesantanderway .dto .GastosDTO ;
6+ import br .com .testesantanderway .dto .GastoDTO ;
87import br .com .testesantanderway .modelo .Gasto ;
98import br .com .testesantanderway .repository .GastoRepository ;
109import br .com .testesantanderway .service .GastoService ;
1817import org .springframework .web .bind .annotation .*;
1918
2019import javax .servlet .http .HttpServletRequest ;
21- import java .util .Optional ;
20+ import java .time .LocalDateTime ;
21+ import java .util .List ;
2222
2323@ RestController
2424@ RequestMapping ("/gastos" )
@@ -33,20 +33,11 @@ public class GastoController {
3333 private GastoService gastoService ;
3434
3535 @ GetMapping
36- @ Cacheable (value = "gastoDeCliente" )
37- public Page <GastosDTO > listagemDeGastos (@ RequestParam (required = false ) String descricao ,
38- @ PageableDefault (sort = "codigoCliente" ,
39- direction = Sort .Direction .ASC ) Pageable paginacao ) {
40-
41- if (descricao == null || descricao .isEmpty ()) {
42- Page <Gasto > gastos = gastoRepository .findAll (paginacao );
43-
44- return GastosDTO .converter (gastos );
45- } else {
46- Page <Gasto > gastos = gastoRepository .findByCategoria (descricao , paginacao );
47-
48- return GastosDTO .converter (gastos );
49- }
36+ public Page <GastoDTO > listagemDeGastos (HttpServletRequest request ) {
37+ String codigoUsuario = servicoDeToken .getCodigo (AutenticacaoViaTokenFilter .recuperarToken (request ));
38+ LocalDateTime ultimosCincoSegundos = LocalDateTime .now ().minusSeconds (5 );
39+ List <Gasto > gastos = gastoRepository .findByCodigoUsuarioAndDataCriacaoAfter (codigoUsuario , ultimosCincoSegundos );
40+ return GastoDTO .converter (gastos );
5041 }
5142
5243 //TODO permitir apenas SISTEMA lançar gasto
@@ -57,24 +48,20 @@ public ResponseEntity lancarGastosCartao(HttpServletRequest request, @RequestBod
5748 return ResponseEntity .ok ().build ();
5849 }
5950
51+ @ Cacheable ("gastoUsuario" )
6052 @ GetMapping ("/{dataCriacao}" )
61- public ResponseEntity <DetalheGastosDTO > listagemDeGastosPorData (@ PathVariable String dataCriacao ) {
62- Optional <Gasto > gasto = gastoRepository .findByDataCriacao (dataCriacao );
63-
64- if (gasto .isPresent ()) {
65- return ResponseEntity .ok (new DetalheGastosDTO (gasto .get ()));
66- }
67-
68- return ResponseEntity .notFound ().build ();
53+ public Page <GastoDTO > listagemDeGastosPorData (@ PathVariable String dataCriacao , @ PageableDefault (sort = "dataCriacao" , direction = Sort .Direction .DESC ) Pageable paginacao ) {
54+ Page <Gasto > gastos = gastoRepository .findByDataCriacao (dataCriacao , paginacao );
55+ return GastoDTO .converter (gastos );
6956 }
7057
7158// @PostMapping("/{categorizaGasto}")
72- // public ResponseEntity<GastosDTO > categorizarGastos(@RequestBody CategoriaForm form, UriComponentsBuilder uriBuilder) {
59+ // public ResponseEntity<GastoDTO > categorizarGastos(@RequestBody CategoriaForm form, UriComponentsBuilder uriBuilder) {
7360// Gasto categoriaCadastro = form.converter();
7461// gastoRepository.save(categoriaCadastro);
7562// URI uri = uriBuilder.path("/{id}").buildAndExpand(categoriaCadastro.getCodigo()).toUri();
7663//
77- // return ResponseEntity.created(uri).body(new GastosDTO (categoriaCadastro));
64+ // return ResponseEntity.created(uri).body(new GastoDTO (categoriaCadastro));
7865// }
7966
8067}
0 commit comments