Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refatoração Representante. #70

Merged
merged 9 commits into from Oct 31, 2021
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.NoSuchElementException;

@RestController
@RequestMapping("representante")
Expand Down Expand Up @@ -42,9 +43,13 @@ public ResponseEntity<RepresentanteDTO> alterar(@RequestBody RepresentanteForm r
return new ResponseEntity<>(RepresentanteDTO.converteEmRepresentanteDTO(representante), HttpStatus.OK);
}

@DeleteMapping("/apagar/{codigo}")
public ResponseEntity<RepresentanteDTO> apagar(@PathVariable String codigo){
Representante representante = representanteService.apagar(codigo);
return new ResponseEntity<>(RepresentanteDTO.converteEmRepresentanteDTO(representante), HttpStatus.ACCEPTED);
@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<String> apagar(@PathVariable Long id) {
try{
representanteService.apagar(id);
} catch (NoSuchElementException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>("Representante deletado com sucesso", HttpStatus.OK);
}
}
Expand Up @@ -26,21 +26,14 @@ public class RepresentanteForm {
public RepresentanteForm() {
}

public RepresentanteForm(String nome, String sobrenome, String cpf, String telefone, String endereco) {
this.nome = nome;
this.sobrenome = sobrenome;
this.cpf = cpf;
this.telefone = telefone;
this.endereco = endereco;
}

/**
* Método criado sem reccebimento sem parametro, e retornando apenas um nono Representante.
*
* @return representante
*/
public Representante converte(){
return Representante.builder()
.codigo(this.codigo)
.nome(this.nome)
.sobrenome(this.sobrenome)
.cpf(this.cpf)
Expand Down
Expand Up @@ -20,12 +20,16 @@
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class Representante extends Pessoa{
public class Representante {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String codigo;

private String nome;
private String sobrenome;
private String cpf;
private String telefone;
private String endereco;

}
Expand Up @@ -44,7 +44,8 @@ public Representante atualizar(Representante representante) {
return representanteRepository.save(representanteEdited);
}

public Representante apagar(String codigo) {
return representanteRepository.deleteByCodigo(codigo);
public Representante apagar(Long id){
representanteRepository.deleteById(id);
return null;
}
}
Expand Up @@ -96,16 +96,15 @@ void atualizarRepresentanteTest(){
}

@Test

void apagarRepresentanteTest(){

representante.setCodigo("25");
Mockito.when(representanteRepository.deleteByCodigo(Mockito.any(String.class))).thenReturn(null);
Mockito.when(representanteRepository.deleteByCodigo(Mockito.any(String.class)));

representanteService = new RepresentanteService(representanteRepository);
Representante deletado = representanteService.apagar(representante.getCodigo());
Representante deletado = representanteService.apagar(representante.getId());

Mockito.verify(representanteRepository, Mockito.times(1)).deleteByCodigo(representante.getCodigo());
Mockito.verify(representanteRepository, Mockito.times(1)).deleteById(representante.getId());

assertNotEquals(deletado,representante);
}
Expand Down