Skip to content

Commit

Permalink
Merge pull request #65 from mathwillock/dev
Browse files Browse the repository at this point in the history
Subindo Refatoração  Da Entity Armazem
  • Loading branch information
mathwillock committed Oct 29, 2021
2 parents 18b9df1 + dc32851 commit 160a898
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 87 deletions.
21 changes: 15 additions & 6 deletions src/main/java/com/bootcamp_w3_g3/controller/ArmazemController.java
Expand Up @@ -11,8 +11,10 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("armazem/")
@RequestMapping("armazem")
public class ArmazemController {

@Autowired
Expand All @@ -24,18 +26,25 @@ public class ArmazemController {
@Autowired
private SetorService setorService;

@GetMapping("ping/")
public String pong() {
return "pong";
}

@PostMapping("/criar")
public ResponseEntity<ArmazemDTO> criarArmazem(@RequestBody ArmazemForm armazemForm) {
Armazem armazem = armazemService.criarArmazem(armazemForm.converte(representanteService, setorService));

return new ResponseEntity<>(ArmazemDTO.converter(armazem), HttpStatus.CREATED);
}

// OK
@GetMapping("/listar")
public ResponseEntity<List<ArmazemDTO>> listar() {
List<Armazem> armazemList = armazemService.listarArmazens();

return new ResponseEntity<>(
ArmazemDTO.armazemDTOListConverte(armazemList),
HttpStatus.OK
);

}




Expand Down
Expand Up @@ -24,32 +24,20 @@ public class ArmazemForm {
private Integer numero;
private String uf;
private RepresentanteForm representanteForm;
private List<SetorForm> setoresDoArmazem;

public Armazem converte(RepresentanteService representanteService, SetorService setorService) {
Representante representante = representanteService.obter(representanteForm.getCodigo());
List<Setor> listaDeSetoresExistentes = carregaSetoresDoArmazem(setorService);

return Armazem.builder()
.codArmazem(codArmazem)
.nome(nome)
.endereco(endereco)
.uf(uf)
.setoresDoArmazem(listaDeSetoresExistentes)
.representante(representante)
.build();

.build()
;
}

private List<Setor> carregaSetoresDoArmazem(SetorService setorService) {
List<Setor> listaDeSetoresExistentes = new ArrayList<>();
if(setoresDoArmazem!=null && !setoresDoArmazem.isEmpty()){
for (SetorForm s : setoresDoArmazem) {
Setor setor = setorService.obterSetor(s.getCodigo());
listaDeSetoresExistentes.add(setor);
}
}
return listaDeSetoresExistentes;
}


}
Expand Up @@ -8,6 +8,7 @@
import lombok.NoArgsConstructor;


import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
Expand All @@ -20,10 +21,7 @@ public class ArmazemDTO {
private String nome;
private String endereco;
private String uf;

private Representante representante;
private List<Setor> SetoresDoArmazem;


public static ArmazemDTO converter(Armazem armazem) {

Expand All @@ -32,13 +30,34 @@ public static ArmazemDTO converter(Armazem armazem) {
armazem.getNome(),
armazem.getEndereco(),
armazem.getUf(),
armazem.getRepresentante(),
armazem.getSetoresDoArmazem()
armazem.getRepresentante()
);

}


public static List<ArmazemDTO> armazemDTOListConverte(List<Armazem> armazemList) {
List<ArmazemDTO> armazemDTOList = new ArrayList<>();

for (Armazem armazem : armazemList) {

armazemDTOList.add(
new ArmazemDTO(
armazem.getCodArmazem(),
armazem.getNome(),
armazem.getEndereco(),
armazem.getUf(),
armazem.getRepresentante()
)
);
}

return armazemDTOList;


}




}
12 changes: 0 additions & 12 deletions src/main/java/com/bootcamp_w3_g3/model/entity/Armazem.java
Expand Up @@ -30,27 +30,15 @@ public class Armazem {
private long id;

private String codArmazem;

private String nome;
private String endereco;
private String uf;

@OneToOne
private Representante representante;

@JsonManagedReference
@OneToMany(cascade = CascadeType.ALL, mappedBy = "armazem", fetch = FetchType.EAGER)
private List<Setor> setoresDoArmazem = new ArrayList<>();




public Armazem(String codigoArmazem){
this.codArmazem = codigoArmazem;
}

public void adicionaSetor(Setor setor){
setor.setArmazem(this);
this.setoresDoArmazem.add(setor);
}
}
Expand Up @@ -18,7 +18,4 @@ public interface ArmazemRepository extends JpaRepository<Armazem, Long> {

Armazem deleteByCodArmazem(String cod);

Armazem findByRepresentanteCodigo(String codigo);


}
21 changes: 0 additions & 21 deletions src/main/java/com/bootcamp_w3_g3/service/ArmazemService.java
Expand Up @@ -6,14 +6,11 @@
*/
import com.bootcamp_w3_g3.model.entity.Armazem;
import com.bootcamp_w3_g3.model.entity.Representante;
import com.bootcamp_w3_g3.model.entity.Setor;
import com.bootcamp_w3_g3.repository.ArmazemRepository;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
Expand Down Expand Up @@ -47,7 +44,6 @@ public Armazem deletarArmazem(String cod){
public Armazem atualizarArmazem(Armazem armazem) {
Armazem editedArmazem = armazemRepository.findByCodArmazem(armazem.getCodArmazem());

editedArmazem.setSetoresDoArmazem(armazem.getSetoresDoArmazem());
editedArmazem.setRepresentante(armazem.getRepresentante());
editedArmazem.setEndereco(armazem.getEndereco());
editedArmazem.setUf(armazem.getUf());
Expand All @@ -57,15 +53,10 @@ public Armazem atualizarArmazem(Armazem armazem) {

}

public List<Setor> listarSetores() {
return setorService.listarSetores();
}

public List<Armazem> listarArmazens() {
return armazemRepository.findAll();
}


public Representante retornaRepresentanteDoArmazem(String codigoRepresentante) {
for (Armazem armazem : listarArmazens()){
if (armazem.getRepresentante().getCodigo().equals(codigoRepresentante)){
Expand All @@ -75,17 +66,5 @@ public Representante retornaRepresentanteDoArmazem(String codigoRepresentante) {
return null;
}

public Setor retornaSetorDoArmazem(String codigoDoSetor) {
List<Setor> setorList = new ArrayList<>();
for (Armazem armazem : listarArmazens()) {
setorList = armazem.getSetoresDoArmazem();
}
for (Setor setor : setorList) {
if (setor.getCodigo().equals(codigoDoSetor)){
return setor;
}
}
return null;
}

}
Expand Up @@ -15,25 +15,20 @@
public class ArmazemServiceUnitTest {

private ArmazemService armazemService;
private ArmazemRepository armazemRepository = Mockito.mock(ArmazemRepository.class);


List<Setor> setorList = new ArrayList<>();
private final ArmazemRepository armazemRepository = Mockito.mock(ArmazemRepository.class);

Representante representante = Representante.builder()
.nome("Alex").sobrenome("Cruz").cpf("2345678910").telefone("5555555").endereco("Rua Joao neves 18").build();

.nome("Alex").sobrenome("Cruz").cpf("2345678910").telefone("5555555").endereco("Rua Joao neves 18").build()
;

Armazem armazem1 = Armazem.builder()
.setoresDoArmazem(setorList)
.codArmazem("Ar-123")
.representante(representante)
.nome("AR1")
.endereco("rua 10")
.uf("SP").build();

Armazem armazem2= Armazem.builder()
.setoresDoArmazem(setorList)
.codArmazem("Ar-123")
.representante(representante)
.nome("AR1")
Expand Down Expand Up @@ -91,23 +86,6 @@ void deletarArmazemTest() {

}

@Test
void atualizarSetorTest(){

Mockito.when(armazemRepository.findByCodArmazem(Mockito.any(String.class))).thenReturn(armazem1);

Mockito.when(armazemRepository.save(Mockito.any(Armazem.class))).thenReturn(armazem1);


armazemService = new ArmazemService(armazemRepository);
Armazem uptaded = armazemService.atualizarArmazem(armazem1);

Mockito.verify(armazemRepository, Mockito.times(1)).findByCodArmazem(armazem1.getCodArmazem());
Mockito.verify(armazemRepository, Mockito.times(1)).save(armazem1);

assertNotNull(uptaded);
assertEquals(uptaded.getCodArmazem(), armazem1.getCodArmazem());

}

}

0 comments on commit 160a898

Please sign in to comment.