diff --git a/pom.xml b/pom.xml index 2b9bb5e6..70a6228d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,11 +1,11 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent - 2.6.3 + 2.6.4-SNAPSHOT br.com @@ -15,6 +15,7 @@ Demo project for Spring Boot 11 + 2021.0.1-SNAPSHOT @@ -25,6 +26,30 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.cloud + spring-cloud-starter + + + org.springframework.boot + spring-boot-starter-validation + + + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + + + org.webjars + bootstrap + 3.3.5 + org.springframework.boot @@ -48,6 +73,17 @@ test + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + @@ -57,5 +93,41 @@ + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + diff --git a/src/main/java/br/com/sigaBemApi/DTO/Request/Carga.java b/src/main/java/br/com/sigaBemApi/DTO/Request/Carga.java index 1a63a173..aa915a15 100644 --- a/src/main/java/br/com/sigaBemApi/DTO/Request/Carga.java +++ b/src/main/java/br/com/sigaBemApi/DTO/Request/Carga.java @@ -1,110 +1,3 @@ package br.com.sigaBemApi.DTO.Request; -import javax.persistence.Entity; -import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.Size; -import java.time.LocalDate; -@Entity -public class Carga { - @javax.persistence.Id - private Long Id; - - @NotEmpty(message = "Campo com preenchimento obrigatório!") - @Size(max = 55, message = "Nome deve conter menos de 55 caracteres!") - private String nomeDestinatario; - - @NotEmpty(message = "Campo com preenchimento obrigatório!") - @Size(min = 8, max = 8, message = "CEP deve conter 8 digitos!") - private String cepOrigem; - - @NotEmpty(message = "Campo com preenchimento obrigatório!") - @Size(min = 8, max = 8, message = "CEP deve conter 8 digitos!") - private String cepDestino; - - @NotEmpty(message = "Campo com preenchimento obrigatório!") - @Size(min = 0, message = "Peso deve ser maior que zero!") - public Double peso; - - public Double vlTotalFrete; - - public LocalDate dataPrevistaEntrega; - - public LocalDate dataConsulta; - - - public Carga() { - - } - // - public Carga(String cepOrigem, String cepDestino, LocalDate dataPrevistaEntrega, Double vlTotalFrete) { - this.cepOrigem = cepOrigem; - this.cepDestino = cepDestino; - this.dataPrevistaEntrega = dataPrevistaEntrega; - this.vlTotalFrete = vlTotalFrete; - } - - public Long getId() { - return Id; - } - - public void setId(Long id) { - Id = id; - } - - public String getNomeDestinatario() { - return nomeDestinatario; - } - - public void setNomeDestinatario(String nomeDestinatario) { - this.nomeDestinatario = nomeDestinatario; - } - - public String getCepOrigem() { - return cepOrigem; - } - - public void setCepOrigem(String cepOrigem) { - this.cepOrigem = cepOrigem; - } - - public String getCepDestino() { - return cepDestino; - } - - public void setCepDestino(String cepDestino) { - this.cepDestino = cepDestino; - } - - public Double getPeso() { - return peso; - } - - public void setPeso(Double peso) { - this.peso = peso; - } - - public Double getVlTotalFrete() { - return vlTotalFrete; - } - - public void setVlTotalFrete(Double vlTotalFrete) { - this.vlTotalFrete = vlTotalFrete; - } - - public LocalDate getDataPrevistaEntrega() { - return dataPrevistaEntrega; - } - - public void setDataPrevistaEntrega(LocalDate dataPrevistaEntrega) { - this.dataPrevistaEntrega = dataPrevistaEntrega; - } - - public LocalDate getDataConsulta() { - return dataConsulta; - } - - public void setDataConsulta(LocalDate dataConsulta) { - this.dataConsulta = dataConsulta; - } -} diff --git a/src/main/java/br/com/sigaBemApi/DTO/Response/AnaliseResponse.java b/src/main/java/br/com/sigaBemApi/DTO/Response/AnaliseResponse.java index 055d8509..4c0d1748 100644 --- a/src/main/java/br/com/sigaBemApi/DTO/Response/AnaliseResponse.java +++ b/src/main/java/br/com/sigaBemApi/DTO/Response/AnaliseResponse.java @@ -1,8 +1,6 @@ package br.com.sigaBemApi.DTO.Response; -import br.com.sigaBemApi.entity.DAO.Analise; import com.fasterxml.jackson.annotation.JsonAutoDetect; - import javax.validation.Valid; import java.time.LocalDate; @@ -11,7 +9,6 @@ public class AnaliseResponse { /* ATTRIBUTES */ - private Long id; private String cepOrigem; private String cepDestino; @@ -19,11 +16,9 @@ public class AnaliseResponse { private Double vlTotalFrete; private Long peso; private String nomeDestinatario; - /* CONSTRUCTORS */ - public AnaliseResponse(String id, Long peso, String cepOrigem, String cepDestino, LocalDate dataPrevistaEntrega, Double vlTotalFrete) { this.id = Long.valueOf(id); this.cepOrigem = cepOrigem; @@ -32,77 +27,51 @@ public AnaliseResponse(String id, Long peso, String cepOrigem, String cepDestino this.vlTotalFrete = vlTotalFrete; this.peso = peso; this.nomeDestinatario = nomeDestinatario; - } //EDITAR public AnaliseResponse() { } - - /* GETTER'S AND SETTER'S */ - public String getCepOrigem() { return cepOrigem; } - public void setCepOrigem(String cepOrigem) { this.cepOrigem = cepOrigem; } - public String getCepDestino() { return cepDestino; } - public void setCepDestino(String cepDestino) { this.cepDestino = cepDestino; } - public LocalDate getDataPrevistaEntrega() { return dataPrevistaEntrega; } - public void setDataPrevistaEntrega(LocalDate dataPrevistaEntrega) { - this.dataPrevistaEntrega = dataPrevistaEntrega; - } - + this.dataPrevistaEntrega = dataPrevistaEntrega;} public Double getVlTotalFrete() { return vlTotalFrete; } - - public Double setVlTotalFrete(Double vlTotalFrete) { - return vlTotalFrete; - - } - - public AnaliseResponse save(Analise analise) { - // TODO Auto-generated method stub - return null; - } - + public Double setVlTotalFrete(Double vlTotalFrete) {return vlTotalFrete;} public Long getId() { return id; } - public void setId(@Valid Long id) { this.id = id; } - public Long getPeso() { return peso; } - public void setPeso(Long peso) { this.peso = peso; } - public String getNomeDestinatario() { return nomeDestinatario; } - public void setNomeDestinatario(String nomeDestinatario) { this.nomeDestinatario = nomeDestinatario; } -} +} \ No newline at end of file diff --git a/src/main/java/br/com/sigaBemApi/Util/PostalNotFound.java b/src/main/java/br/com/sigaBemApi/Util/PostalNotFound.java index 96d41d87..82d73512 100644 --- a/src/main/java/br/com/sigaBemApi/Util/PostalNotFound.java +++ b/src/main/java/br/com/sigaBemApi/Util/PostalNotFound.java @@ -1,9 +1,7 @@ package br.com.sigaBemApi.Util; -@SuppressWarnings("serial") public class PostalNotFound extends RuntimeException{ public PostalNotFound(String postal) { super("CEP: "+postal+" inválido"); - } } diff --git a/src/main/java/br/com/sigaBemApi/controller/CargaController.java b/src/main/java/br/com/sigaBemApi/controller/CargaController.java index a193c8b1..57597b4f 100644 --- a/src/main/java/br/com/sigaBemApi/controller/CargaController.java +++ b/src/main/java/br/com/sigaBemApi/controller/CargaController.java @@ -1,110 +1,21 @@ package br.com.sigaBemApi.controller; -import br.com.sigaBemApi.DTO.Response.AnaliseResponse; import br.com.sigaBemApi.entity.DAO.Analise; -import br.com.sigaBemApi.entity.Postal; import br.com.sigaBemApi.repository.CargaRepository; -import br.com.sigaBemApi.service.BuscarPostal; import br.com.sigaBemApi.service.CargaService; -import ch.qos.logback.core.rolling.helper.MonoTypedConverter; -import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpEntity; -import org.springframework.http.ResponseEntity; -import org.springframework.util.concurrent.MonoToListenableFutureAdapter; import org.springframework.web.bind.annotation.*; -import javax.validation.Valid; -import static org.springframework.http.ResponseEntity.ok; +import java.util.List; -@AllArgsConstructor @RestController @RequestMapping("/carga") public class CargaController { - @Autowired private CargaService cargaService; - @PostMapping - public Analise buscar(Analise analise){ + @PostMapping("/testar") + public Analise buscarCepCalcularFreteESalvar(@RequestBody Analise analise){ return cargaService.buscarCepCalcularFreteESalvar(analise); } - -// CargaController cargaController; -// -// @GetMapping("carga/cep") -// public MonoToListenableFutureAdapter getAnaliseByCep(@PathVariable String Cep){ - - -// } - - - - } - - - - -// private final CargaService cargaService; -// -// private final CargaRepository cargaRepository; -// -// public CargaController(CargaRepository cargaRepository, CargaService cargaService) { -// this.cargaRepository = cargaRepository; -// this.cargaService = cargaService; -// } -// -//// public CargaService getCargaService() { -//// return cargaService; -//// -//// } -// -// @PostMapping -//// @ResponseStatus(HttpStatus.CREATED) -// public double analiseResponse(@RequestBody AnaliseResponse pacote) { -// return cargaService.calcularValorTotalFrete(pacote); -// } -// -// // @GetMapping -//// public List BucarId(){ -//// return cargaRepository.findAll(); -//// } -// -// @GetMapping("{carga}") -// public HttpEntity buscarId(carga) { -// return buscarId(); -// } -// -// @GetMapping("{carga}") -// public HttpEntity buscarPostal(Postal postal, @PathVariable String carga) { -//// def i = Integer.parseInt("1"); -// return cargaRepository.findById(postal) -// .map(encomenda -> ok(postal)) -// .orElse(ResponseEntity.notFound().build()); -// } -// -// @PutMapping("/{idCarga}") -// public ResponseEntity atualizarCarga(@Valid @PathVariable Long idCarga, -// @RequestBody AnaliseResponse analiseResponse) { -// -// if (!cargaRepository.existsById(idCarga)) { -// return ResponseEntity.notFound().build(); -// } -// -// analiseResponse.setId(idCarga); -// analiseResponse = cargaService.salvarcarga(analiseResponse); -// return ok(analiseResponse); -// } -// -// @DeleteMapping("/{idcarga}") -// public ResponseEntity excluirCarga(@PathVariable Long idCarga) { -// if (!cargaRepository.existsById(idCarga)) { -// return ResponseEntity.notFound().build(); -// } -// cargaService.excluirCarga(idCarga); -// return ResponseEntity.noContent().build(); -// } -// -// -//} \ No newline at end of file diff --git a/src/main/java/br/com/sigaBemApi/controller/PostalController.java b/src/main/java/br/com/sigaBemApi/controller/PostalController.java deleted file mode 100644 index 1c880e93..00000000 --- a/src/main/java/br/com/sigaBemApi/controller/PostalController.java +++ /dev/null @@ -1,4 +0,0 @@ -package br.com.sigaBemApi.controller; - -public class PostalController { -} diff --git a/src/main/java/br/com/sigaBemApi/entity/DAO/Analise.java b/src/main/java/br/com/sigaBemApi/entity/DAO/Analise.java index b225f937..edf593b7 100644 --- a/src/main/java/br/com/sigaBemApi/entity/DAO/Analise.java +++ b/src/main/java/br/com/sigaBemApi/entity/DAO/Analise.java @@ -1,7 +1,6 @@ package br.com.sigaBemApi.entity.DAO; -import br.com.sigaBemApi.DTO.Response.AnaliseResponse; -import org.springframework.lang.NonNull; +import com.sun.istack.NotNull; import javax.persistence.*; import javax.validation.constraints.NotBlank; @@ -14,21 +13,16 @@ public class Analise { // ATTRIBUTES - @javax.persistence.Id + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private long Id; + private Long Id; - @NotBlank - @Column(length = 55) private String nomeDestinatario; - @NotBlank private String cepOrigem; - @NotBlank private String cepDestino; - @NonNull private Double peso; private Double vlTotalFrete; @@ -39,10 +33,9 @@ public class Analise { //CONSTRUCTORS - public Analise() { - } + public Analise() {} - public Analise(long id, String nomeDestinatario, String cepOrigem, String cepDestino, Double peso, Double vlTotalFrete, LocalDate dataPrevistaEntrega, LocalDate dataConsulta) { + public Analise(Long id, String nomeDestinatario, String cepOrigem, String cepDestino, Double peso, Double vlTotalFrete, LocalDate dataPrevistaEntrega, LocalDate dataConsulta) { Id = id; this.nomeDestinatario = nomeDestinatario; this.cepOrigem = cepOrigem; @@ -53,16 +46,13 @@ public Analise(long id, String nomeDestinatario, String cepOrigem, String cepDes this.dataConsulta = dataConsulta; } -//GETTER'S AND SETTER'S - - public Analise(Analise analise) { - } + //GETTER'S AND SETTER'S - public long getId() { + public Long getId() { return Id; } - public void setId(long id) { + public void setId(Long id) { Id = id; } @@ -102,14 +92,14 @@ public Double getVlTotalFrete() { return vlTotalFrete; } - public LocalDate getDataPrevistaEntrega() { - return dataPrevistaEntrega; - } - public void setVlTotalFrete(Double vlTotalFrete) { this.vlTotalFrete = vlTotalFrete; } + public LocalDate getDataPrevistaEntrega() { + return dataPrevistaEntrega; + } + public void setDataPrevistaEntrega(LocalDate dataPrevistaEntrega) { this.dataPrevistaEntrega = dataPrevistaEntrega; } @@ -118,10 +108,11 @@ public LocalDate getDataConsulta() { return dataConsulta; } - public void setDataConsulta(LocalDate dataConsulta){ + public void setDataConsulta(LocalDate dataConsulta) { this.dataConsulta = dataConsulta; } + //METODOS @Override @@ -131,11 +122,8 @@ public boolean equals(Object o) { Analise that = (Analise) o; return Id == that.Id; } - @Override - public int hashCode() { - return Objects.hash(Id); - } + public int hashCode() {return Objects.hash(Id);} @Override public String toString() { @@ -145,8 +133,4 @@ public String toString() { ", dataPrevistaEntrega=" + dataPrevistaEntrega + '}'; } - -//EDITAR - public void setPeso(Long peso) { - } } diff --git a/src/main/java/br/com/sigaBemApi/entity/Postal.java b/src/main/java/br/com/sigaBemApi/entity/Postal.java index 49a016b2..51379d66 100644 --- a/src/main/java/br/com/sigaBemApi/entity/Postal.java +++ b/src/main/java/br/com/sigaBemApi/entity/Postal.java @@ -1,19 +1,12 @@ package br.com.sigaBemApi.entity; - import br.com.sigaBemApi.service.BuscarPostal; + public class Postal extends BuscarPostal { private String cep; - private String logradouro; - private String complemento; - private String bairro; - private String localidade; private String uf; - private String ibge; - private String gia; private String ddd; - private String siafi; public Postal() { @@ -22,107 +15,31 @@ public Postal() { public Postal(String cep, String uf, String ddd) { this.cep = cep; - this.logradouro = logradouro; - this.complemento = complemento; - this.bairro = bairro; - this.localidade = localidade; this.uf = uf; - this.ibge = ibge; - this.gia = gia; this.ddd = ddd; - this.siafi = siafi; - - } - public Postal(String cep, String logradouro, String complemento, String bairro, String localidade, String uf, String ibge, String gia, String ddd, String siafi) { - this.cep = cep; - this.logradouro = logradouro; - this.complemento = complemento; - this.bairro = bairro; - this.localidade = localidade; - this.uf = uf; - this.ibge = ibge; - this.gia = gia; - this.ddd = ddd; - this.siafi = siafi; - } - - public void setDdd(String ddd) { - - this.ddd = ddd; - } public String getCep() { return cep; } + public void setCep(String cep) { this.cep = cep; } - public String getLogradouro() { - return logradouro; - } - public void setLogradouro(String logradouro) { - - this.logradouro = logradouro; - } - public String getComplemento() { - return complemento; - } - public void setComplemento(String complemento) { - - this.complemento = complemento; - } - public String getBairro() { - - return bairro; - } - public void setBairro(String bairro) { - - this.bairro = bairro; - } - public String getLocalidade() { - - return localidade; - } - public void setLocalidade(String localidade) { - - this.localidade = localidade; - } public String getUf() { - return uf; } - public void setUf(String uf) { + public void setUf(String uf) { this.uf = uf; } - public String getIbge() { - - return ibge; - } - public void setIbge(String ibge) { - this.ibge = ibge; - } - public String getGia() { - - return gia; - } - public void setGia(String gia) { - - this.gia = gia; - } public String getDdd() { - return ddd; } - public String getSiafi() { - - return siafi; - } - public void setSiafi(String siafi) { - this.siafi = siafi; + public void setDdd(String ddd) { + this.ddd = ddd; } } diff --git a/src/main/java/br/com/sigaBemApi/repository/CargaRepository.java b/src/main/java/br/com/sigaBemApi/repository/CargaRepository.java index 74392d2f..4751c7b0 100644 --- a/src/main/java/br/com/sigaBemApi/repository/CargaRepository.java +++ b/src/main/java/br/com/sigaBemApi/repository/CargaRepository.java @@ -1,13 +1,10 @@ package br.com.sigaBemApi.repository; -import br.com.sigaBemApi.DTO.Request.Carga; -import br.com.sigaBemApi.DTO.Response.AnaliseResponse; import br.com.sigaBemApi.entity.DAO.Analise; -import br.com.sigaBemApi.entity.Postal; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.stream.DoubleStream; - @Repository -public interface CargaRepository extends JpaRepository {} +public interface CargaRepository extends JpaRepository { + +} diff --git a/src/main/java/br/com/sigaBemApi/service/BuscarPostal.java b/src/main/java/br/com/sigaBemApi/service/BuscarPostal.java index ca9c7a73..19b4afc3 100644 --- a/src/main/java/br/com/sigaBemApi/service/BuscarPostal.java +++ b/src/main/java/br/com/sigaBemApi/service/BuscarPostal.java @@ -2,9 +2,6 @@ import br.com.sigaBemApi.Util.PostalNotFound; import br.com.sigaBemApi.entity.Postal; -import com.fasterxml.jackson.annotation.JsonAutoDetect; -import lombok.extern.slf4j.Slf4j; -import org.jetbrains.annotations.TestOnly; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -12,17 +9,8 @@ import org.springframework.web.util.UriComponentsBuilder; @Service -//@Slf4j public class BuscarPostal { -// private final WebClientAutoConfiguration webClient; -// -// public BuscarPostal(WebClientAutoConfiguration.Builder -// -// public WebClientAutoConfiguration getWebClient() { -// return Builder - - public Postal buscarPostal(String cep) { RestTemplate rt = new RestTemplate(); @@ -33,12 +21,11 @@ public Postal buscarPostal(String cep) { .build(); ResponseEntity response = rt.getForEntity(uri.toString(), Postal.class); - System.out.println(response.getBody().getCep()); +// System.out.println(response.getBody().getCep()); if (response.getBody().getCep() == null) { throw new PostalNotFound("postal: "+cep); } - return response.getBody(); } diff --git a/src/main/java/br/com/sigaBemApi/service/CargaService.java b/src/main/java/br/com/sigaBemApi/service/CargaService.java index 2d8c2333..57700001 100644 --- a/src/main/java/br/com/sigaBemApi/service/CargaService.java +++ b/src/main/java/br/com/sigaBemApi/service/CargaService.java @@ -1,18 +1,13 @@ package br.com.sigaBemApi.service; -import br.com.sigaBemApi.DTO.Request.Carga; -import br.com.sigaBemApi.DTO.Response.AnaliseResponse; import br.com.sigaBemApi.entity.DAO.Analise; import br.com.sigaBemApi.entity.Postal; import br.com.sigaBemApi.repository.CargaRepository; import com.google.common.base.Objects; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; - import java.time.LocalDate; - @Service public class CargaService { @@ -20,7 +15,7 @@ public class CargaService { private BuscarPostal buscarPostal; @Autowired - private CargaRepository cargaRepository; + private CargaRepository repository; public Analise buscarCepCalcularFreteESalvar(Analise analise){ @@ -35,10 +30,9 @@ public Analise buscarCepCalcularFreteESalvar(Analise analise){ LocalDate dataPrevistaEntrega = LocalDate.now(); analise.setDataPrevistaEntrega(dataPrevistaEntrega.plusDays(diasParaEntrega)); + Analise saved = this.repository.save(analise); - //TODO IMPLEMENTAR A PERSISTENCIA NO BANCO DE DADOS. - - return analise; + return saved; } private Double CalcularDesconto(Postal cepOrigem, Postal cepDestino) { @@ -53,8 +47,6 @@ private Double CalcularDesconto(Postal cepOrigem, Postal cepDestino) { return 1.0; } - - private Long calcularPrazoEntrega(Postal cepOrigem, Postal cepDestino) { if (Objects.equal(cepOrigem.getUf(), cepDestino.getUf())) { return 1L; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b137891..52a09f44 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,14 @@ +spring.datasource.url=jdbc:h2:~/test; +spring.datasource.username=sa +spring.datasource.password=123 +spring.datasource.driverClassName=org.h2.Driver +#JPA +#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +#Porta de acesso +server.port=8080 +# Dados de conexao com o banco H2 +spring.h2.console.enabled=true +spring.h2.console.path=/h2-console + +###12/02