Skip to content

Commit

Permalink
Merge pull request #70 from hugaodamm/dev
Browse files Browse the repository at this point in the history
Refatoração Representante.
  • Loading branch information
lexscruz committed Oct 31, 2021
2 parents d556002 + 6ef5bb5 commit 23bba97
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
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

0 comments on commit 23bba97

Please sign in to comment.