Skip to content

Commit e00ead8

Browse files
Categorização de Gastos: - Criado serviço de categorização de gastos. - Criado teste unitário para testar categorização de gastos
1 parent 3de86af commit e00ead8

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

testeSantanderWay/src/main/java/br/com/testesantanderway/controller/GastoController.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ public Page<GastoDTO> listagemDeGastosPorData(HttpServletRequest request,
5454
return GastoDTO.converter(gastoService.encontrarGastosDoDia(codigoUsuario, dataCriacao, paginacao));
5555
}
5656

57-
// @PostMapping("/{categorizaGasto}")
58-
// public ResponseEntity<GastoDTO> categorizarGastos(@RequestBody CategoriaForm form, UriComponentsBuilder uriBuilder) {
59-
// Gasto categoriaCadastro = form.converter();
60-
// gastoRepository.save(categoriaCadastro);
61-
// URI uri = uriBuilder.path("/{id}").buildAndExpand(categoriaCadastro.getCodigo()).toUri();
62-
//
63-
// return ResponseEntity.created(uri).body(new GastoDTO(categoriaCadastro));
64-
// }
57+
//TODO permitir apenas USUARIO categorizar gasto
58+
@PutMapping("/{categorizarGasto}")
59+
public ResponseEntity categorizarGasto(@RequestBody Gasto gasto) {
60+
gastoService.categorizarGasto(gasto);
61+
return ResponseEntity.ok().build();
62+
}
6563

6664
}

testeSantanderWay/src/main/java/br/com/testesantanderway/service/GastoService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public Page<Gasto> encontrarGastosDoDia(String codigoUsuario, LocalDate dia, Pag
3535
return gastoRepository.findByCodigoUsuarioAndDataCriacaoBetween(codigoUsuario, inicio, fim, paginacao);
3636
}
3737

38+
public void categorizarGasto(Gasto gasto) {
39+
Optional<Gasto> categoriaASerAlterada = gastoRepository.findById(gasto.getCodigoUsuario());
40+
if(!categoriaASerAlterada.isPresent() || categoriaASerAlterada.get().getCategoria() != null){
41+
throw new IllegalArgumentException("Operação não permitida");
42+
}
43+
categoriaASerAlterada.get().setCategoria(gasto.getCategoria());
44+
gastoRepository.save(categoriaASerAlterada.get());
45+
}
46+
3847
private void integrarCategoria(Gasto gasto){
3948
if(gasto.getCategoria() == null){
4049
Optional<String> categoria = gastoRepository.findCategoriaByDescricao(gasto.getDescricao());

testeSantanderWay/src/test/java/br/com/testesantanderway/config/TesteGasto.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ public void testarEncontrarGastosDoDia(){
5252
service.encontrarGastosDoDia(codigoUsuario, LocalDate.now(), Pageable.unpaged());
5353
}
5454

55+
@DisplayName("Testa o Serviço que categoriza um Gasto")
56+
@Test
57+
public void testarCategorizacaoDeGasto(){
58+
this.gasto.setCategoria("aluguel");
59+
service.categorizarGasto(this.gasto);
60+
}
61+
5562
}

0 commit comments

Comments
 (0)