Skip to content

Commit

Permalink
feat: analise financeira para proposta
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidawilliam committed May 5, 2021
1 parent e3ab71e commit 4daaff5
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 9 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.2</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class PropostaApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package br.com.zupacademy.william.proposta.proposta;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.math.BigDecimal;

@Entity
Expand All @@ -19,6 +16,9 @@ public class Proposta {
private String endereco;
private BigDecimal salario;

@Enumerated(EnumType.STRING)
private PropostaEstado propostaEstado;

public Proposta(String documento, String email, String nome, String endereco, BigDecimal salario) {
this.documento = documento;
this.email = email;
Expand All @@ -34,4 +34,16 @@ public Proposta() {
public Long getId() {
return id;
}

public String getDocumento() {
return documento;
}

public String getNome() {
return nome;
}

public void setEstadoProposta(PropostaEstado propostaEstado) {
this.propostaEstado = propostaEstado;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package br.com.zupacademy.william.proposta.proposta;

import br.com.zupacademy.william.proposta.proposta.analisefinanceira.AnaliseFinanceiraClient;
import br.com.zupacademy.william.proposta.proposta.analisefinanceira.AnaliseFinanceiraRequest;
import feign.FeignException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -19,21 +22,36 @@ public class PropostaController {
@Autowired
private PropostaRepository propostaRepository;

@Autowired
private AnaliseFinanceiraClient analiseFinanceiraClient;

@Transactional
@PostMapping
public ResponseEntity criar(UriComponentsBuilder uriComponentsBuilder,
@RequestBody @Valid PropostaRequest propostaRequest) {

boolean documentoJaCadastrado = propostaRepository.existsByDocumento(propostaRequest.getDocumento());

if (documentoJaCadastrado) {
return ResponseEntity.unprocessableEntity().build();
}

Proposta proposta = propostaRequest.toModel();
Proposta novaProposta = propostaRepository.save(proposta);
URI enderecoRecurso = uriComponentsBuilder.path("/propostas/{id}").build(novaProposta.getId());
var proposta = propostaRequest.toModel();
var novaProposta = propostaRepository.save(proposta);

//TODO Procurar como refatorar
try {
var validacaoRequest = new AnaliseFinanceiraRequest(
String.valueOf(novaProposta.getId()),
novaProposta.getDocumento(),
novaProposta.getNome());

analiseFinanceiraClient.avaliarProposta(validacaoRequest);
novaProposta.setEstadoProposta(PropostaEstado.ELEGIVEL);
} catch (FeignException e) {
novaProposta.setEstadoProposta(PropostaEstado.NAO_ELEGIVEL);
}

URI enderecoRecurso = uriComponentsBuilder.path("/propostas/{id}").build(novaProposta.getId());
return ResponseEntity.created(enderecoRecurso).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.zupacademy.william.proposta.proposta;

public enum PropostaEstado {

ELEGIVEL,
NAO_ELEGIVEL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.com.zupacademy.william.proposta.proposta.analisefinanceira;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "financeiro", url = "http://localhost:9999/api")
public interface AnaliseFinanceiraClient {

@PostMapping(value = "/solicitacao")
AnaliseFinanceiraResponse avaliarProposta(@RequestBody AnaliseFinanceiraRequest analiseFinanceiraRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package br.com.zupacademy.william.proposta.proposta.analisefinanceira;

public class AnaliseFinanceiraRequest {

/**
* descomentar jsonproperty caso spring.jackson.property-naming-strategy=SNAKE_CASE
*/
// @JsonProperty("idProposta")
private String idProposta;
private String documento;
private String nome;

public AnaliseFinanceiraRequest(String idProposta, String documento, String nome) {
this.idProposta = idProposta;
this.documento = documento;
this.nome = nome;
}

public String getIdProposta() {
return idProposta;
}

public String getDocumento() {
return documento;
}

public String getNome() {
return nome;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package br.com.zupacademy.william.proposta.proposta.analisefinanceira;

public class AnaliseFinanceiraResponse {

private String idProposta;
private String documento;
private String nome;
private AnaliseFinanceiraResultado resultadoSolicitacao;

public AnaliseFinanceiraResponse(String idProposta, String documento, String nome, AnaliseFinanceiraResultado resultadoSolicitacao) {
this.idProposta = idProposta;
this.documento = documento;
this.nome = nome;
this.resultadoSolicitacao = resultadoSolicitacao;
}

public String getIdProposta() {
return idProposta;
}

public String getDocumento() {
return documento;
}

public String getNome() {
return nome;
}

public AnaliseFinanceiraResultado getResultadoSolicitacao() {
return resultadoSolicitacao;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.zupacademy.william.proposta.proposta.analisefinanceira;

public enum AnaliseFinanceiraResultado {

COM_RESTRICAO,
SEM_RESTRICAO
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spring.h2.console.path=/h2-console

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jackson.property-naming-strategy=SNAKE_CASE
#spring.jackson.property-naming-strategy=SNAKE_CASE

spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
Expand Down

0 comments on commit 4daaff5

Please sign in to comment.