Skip to content

Commit

Permalink
feat: criar e associar cartao a propostas elegiveis
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidawilliam committed May 7, 2021
1 parent 6fc07e4 commit 895bd03
Show file tree
Hide file tree
Showing 28 changed files with 490 additions and 10 deletions.
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ services:
ports:
- 5432:5432
environment:
POSTGRES_USER: keyclospring.datasource.username=
spring.datasource.password=ak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: password
POSTGRES_DB: keycloak
POSTGRES_HOST: postgres
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.2.0.Final</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableAsync
@EnableScheduling
@EnableFeignClients
@SpringBootApplication
public class PropostaApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.zupacademy.william.proposta.proposta.analisefinanceira;
package br.com.zupacademy.william.proposta.gateway.analisefinanceira;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.zupacademy.william.proposta.proposta.analisefinanceira;
package br.com.zupacademy.william.proposta.gateway.analisefinanceira;

public class AnaliseFinanceiraRequest {

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

public class AnaliseFinanceiraResponse {

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

public enum AnaliseFinanceiraResultado {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class AvisoGatewayResponse {

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

private Long idCartao;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

import br.com.zupacademy.william.proposta.proposta.cartao.bloqueio.Bloqueio;

public class BloqueioGatewayResponse {

private Long id;
private Long idCartao;

public Bloqueio toModel() {
return new Bloqueio();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

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

@FeignClient(name = "cartoes", url = "http://localhost:8888/api/cartoes")
public interface CartaoClient {

@PostMapping
GeracaoCartaoGatewayResponse cadastrarCartao(@RequestBody GeracaoCartaoRequestGateway geracaoCartaoRequestGateway);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

public class CarteiraGatewayResponse {

private Long id;

private Long idCartao;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

import br.com.zupacademy.william.proposta.proposta.cartao.Cartao;

import java.time.LocalDateTime;
import java.util.List;

public class GeracaoCartaoGatewayResponse {

private String id;
private LocalDateTime emitidoEm;
private String titular;
private String idProposta;
private Long limite;
private List<BloqueioGatewayResponse> bloqueios;
private List<AvisoGatewayResponse> avisos;
private List<CarteiraGatewayResponse> carteiras;
private List<ParcelaGatewayResponse> parcelas;
private VencimentoGatewayResponse vencimento;
private String renegociacao;

public GeracaoCartaoGatewayResponse(String id, LocalDateTime emitidoEm, String titular, String idProposta,
Long limite, List<BloqueioGatewayResponse> bloqueios,
List<AvisoGatewayResponse> avisos, List<CarteiraGatewayResponse> carteiras,
List<ParcelaGatewayResponse> parcelas, VencimentoGatewayResponse vencimento,
String renegociacao) {
this.id = id;
this.emitidoEm = emitidoEm;
this.titular = titular;
this.idProposta = idProposta;
this.limite = limite;
this.bloqueios = bloqueios;
this.avisos = avisos;
this.carteiras = carteiras;
this.parcelas = parcelas;
this.vencimento = vencimento;
this.renegociacao = renegociacao;
}

public Cartao toModel() {
return new Cartao(this.id, this.emitidoEm, this.titular, this.limite, this.renegociacao,
Long.parseLong(this.idProposta), this.vencimento.toModel());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

public class GeracaoCartaoRequestGateway {

private String idProposta;
private String documento;
private String nome;

public GeracaoCartaoRequestGateway(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,8 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

public class ParcelaGatewayResponse {

private Long id;

private Long idCartao;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package br.com.zupacademy.william.proposta.gateway.cartao;

import br.com.zupacademy.william.proposta.proposta.cartao.vencimento.Vencimento;

import java.time.LocalDateTime;

public class VencimentoGatewayResponse {

private String id;
private int dia;
private LocalDateTime dataCriacao;

public VencimentoGatewayResponse(String id, int dia, LocalDateTime dataCriacao) {
this.id = id;
this.dia = dia;
this.dataCriacao = dataCriacao;
}

public Vencimento toModel() {
return new Vencimento(id, dia, LocalDateTime.now());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package br.com.zupacademy.william.proposta.proposta;

import br.com.zupacademy.william.proposta.proposta.cartao.Cartao;

import javax.persistence.*;
import java.math.BigDecimal;

Expand All @@ -19,6 +21,10 @@ public class Proposta {
@Enumerated(EnumType.STRING)
private PropostaEstado propostaEstado;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "id_proposta")
private Cartao cartao;

public Proposta(String documento, String email, String nome, String endereco, BigDecimal salario) {
this.documento = documento;
this.email = email;
Expand Down Expand Up @@ -46,4 +52,12 @@ public String getNome() {
public void setEstadoProposta(PropostaEstado propostaEstado) {
this.propostaEstado = propostaEstado;
}

public void vincularCartao(Cartao cartao) {
this.cartao = cartao;
}

public boolean eElegivel() {
return this.propostaEstado.equals(PropostaEstado.ELEGIVEL);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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 br.com.zupacademy.william.proposta.gateway.analisefinanceira.AnaliseFinanceiraClient;
import br.com.zupacademy.william.proposta.gateway.analisefinanceira.AnaliseFinanceiraRequest;
import br.com.zupacademy.william.proposta.proposta.evento.PropostaCriadaEvent;
import feign.FeignException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -19,6 +21,9 @@
@RequestMapping("/propostas")
public class PropostaController {

@Autowired
private ApplicationEventPublisher publisher;

@Autowired
private PropostaRepository propostaRepository;

Expand All @@ -38,7 +43,9 @@ public ResponseEntity criar(UriComponentsBuilder uriComponentsBuilder,
var proposta = propostaRequest.toModel();
var novaProposta = propostaRepository.save(proposta);

//TODO Procurar como refatorar
//TODO
// Estudar como refatorar
// Refatorar também para caso o link não responda (tirar setEstadoProposta do catch)
try {
var validacaoRequest = new AnaliseFinanceiraRequest(
String.valueOf(novaProposta.getId()),
Expand All @@ -51,6 +58,10 @@ public ResponseEntity criar(UriComponentsBuilder uriComponentsBuilder,
novaProposta.setEstadoProposta(PropostaEstado.NAO_ELEGIVEL);
}

if (novaProposta.eElegivel()) {
publisher.publishEvent(new PropostaCriadaEvent(novaProposta));
}

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

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

public interface PropostaRepository extends JpaRepository<Proposta, Long> {
import java.util.List;

public interface PropostaRepository extends
JpaRepository<Proposta, Long>,
JpaSpecificationExecutor<Proposta> {

boolean existsByDocumento(String documento);

@Query("SELECT p from Proposta p left join Cartao c on p.id = c.idProposta where c.id is null and p.propostaEstado = 'ELEGIVEL'")
List<Proposta> buscarPropostasComStatusElegivelQueNaoPossuemCartao();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package br.com.zupacademy.william.proposta.proposta.cartao;

import br.com.zupacademy.william.proposta.proposta.cartao.aviso.Aviso;
import br.com.zupacademy.william.proposta.proposta.cartao.bloqueio.Bloqueio;
import br.com.zupacademy.william.proposta.proposta.cartao.carteira.Carteira;
import br.com.zupacademy.william.proposta.proposta.cartao.parcela.Parcela;
import br.com.zupacademy.william.proposta.proposta.cartao.vencimento.Vencimento;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Entity
public class Cartao {

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

private String numero;
private LocalDateTime emitidoEm;
private String titular;
private Long limite;
private String renegociacao;
private Long idProposta;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "idCartao")
private List<Bloqueio> bloqueios;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "idCartao")
private List<Aviso> avisos;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "idCartao")
private List<Carteira> carteiras;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "idCartao")
private List<Parcela> parcelas;

//TODO
// fazer relacionamento para salvar o id do cartao no vencimento, onetomany foi workaround
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "idCartao")
private List<Vencimento> vencimento;

// public Cartao(String numero, LocalDateTime emitidoEm, String titular, Long limite, String renegociacao,
// Long idProposta, List<Bloqueio> bloqueios, List<Aviso> avisos, List<Carteira> carteiras,
// List<Parcela> parcelas, Vencimento vencimento) {
// this.numero = numero;
// this.emitidoEm = emitidoEm;
// this.titular = titular;
// this.limite = limite;
// this.renegociacao = renegociacao;
// this.idProposta = idProposta;
// this.bloqueios = bloqueios;
// this.avisos = avisos;
// this.carteiras = carteiras;
// this.parcelas = parcelas;
// this.vencimento = vencimento;
// }

//CONSTRUTOR TEMPORARIO
//TODO
// Refatorar para nao receber idProposta e usar o cascade da classe Proposta
public Cartao(String numero, LocalDateTime emitidoEm, String titular, Long limite, String renegociacao,
Long idProposta, Vencimento vencimento) {
this.numero = numero;
this.emitidoEm = emitidoEm;
this.titular = titular;
this.limite = limite;
this.renegociacao = renegociacao;
this.idProposta = idProposta;
this.vencimento = Collections.singletonList(vencimento);
}

@Deprecated
public Cartao() {
}
}
Loading

0 comments on commit 895bd03

Please sign in to comment.