From 8a908a9c03a95e09f420ec3e088d46e552fc0a0d Mon Sep 17 00:00:00 2001 From: William Almeida Date: Mon, 10 May 2021 13:24:51 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20cadastro=20de=20biometria=20para=20cart?= =?UTF-8?q?=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../william/proposta/biometria/Biometria.java | 29 ++++++++++++ .../biometria/BiometriaController.java | 45 +++++++++++++++++++ .../biometria/BiometriaRepository.java | 6 +++ .../proposta/biometria/BiometriaRequest.java | 19 ++++++++ .../proposta/proposta/cartao/Cartao.java | 4 ++ .../proposta/cartao/CartaoRepository.java | 6 +++ 6 files changed, 109 insertions(+) create mode 100644 src/main/java/br/com/zupacademy/william/proposta/biometria/Biometria.java create mode 100644 src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaController.java create mode 100644 src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRepository.java create mode 100644 src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRequest.java create mode 100644 src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/CartaoRepository.java diff --git a/src/main/java/br/com/zupacademy/william/proposta/biometria/Biometria.java b/src/main/java/br/com/zupacademy/william/proposta/biometria/Biometria.java new file mode 100644 index 00000000..b2fe9ab3 --- /dev/null +++ b/src/main/java/br/com/zupacademy/william/proposta/biometria/Biometria.java @@ -0,0 +1,29 @@ +package br.com.zupacademy.william.proposta.biometria; + +import br.com.zupacademy.william.proposta.proposta.cartao.Cartao; + +import javax.persistence.*; +import java.util.Base64; + +@Entity +public class Biometria { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private byte[] fingerPrint; + + @ManyToOne + @JoinColumn(name = "id_cartao") + private Cartao cartao; + + public Biometria(String fingerPrint, Cartao cartao) { + this.fingerPrint = Base64.getEncoder().encode(fingerPrint.getBytes()); + this.cartao = cartao; + } + + public Long getId() { + return id; + } +} diff --git a/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaController.java b/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaController.java new file mode 100644 index 00000000..b53d72ce --- /dev/null +++ b/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaController.java @@ -0,0 +1,45 @@ +package br.com.zupacademy.william.proposta.biometria; + +import br.com.zupacademy.william.proposta.exception.EntidadeNaoEncontradaException; +import br.com.zupacademy.william.proposta.proposta.cartao.Cartao; +import br.com.zupacademy.william.proposta.proposta.cartao.CartaoRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.transaction.Transactional; +import javax.validation.Valid; +import java.net.URI; + +@RestController +@RequestMapping("/cartoes/{id}/biometrias") +public class BiometriaController { + + @Autowired + private BiometriaRepository biometriaRepository; + + @Autowired + private CartaoRepository cartaoRepository; + + @Transactional + @PostMapping + public ResponseEntity criar(@PathVariable("id") Long idCartao, + @Valid BiometriaRequest biometriaRequest, + UriComponentsBuilder uriComponentsBuilder) { + Cartao cartao = cartaoRepository.findById(idCartao) + .orElseThrow(() -> new EntidadeNaoEncontradaException(String.format("Não existe um cartão com id %d", idCartao))); + + Biometria biometria = biometriaRequest.toModel(cartao); + Biometria novaBiometria = biometriaRepository.save(biometria); + + URI uriDoRecurso = uriComponentsBuilder + .path("cartoes/{id}/biometrias/{id}") + .build(cartao.getId(), novaBiometria.getId()); + + return ResponseEntity.created(uriDoRecurso).build(); + } +} diff --git a/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRepository.java b/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRepository.java new file mode 100644 index 00000000..096b82bc --- /dev/null +++ b/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRepository.java @@ -0,0 +1,6 @@ +package br.com.zupacademy.william.proposta.biometria; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BiometriaRepository extends JpaRepository { +} diff --git a/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRequest.java b/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRequest.java new file mode 100644 index 00000000..c49def55 --- /dev/null +++ b/src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRequest.java @@ -0,0 +1,19 @@ +package br.com.zupacademy.william.proposta.biometria; + +import br.com.zupacademy.william.proposta.proposta.cartao.Cartao; + +import javax.validation.constraints.NotBlank; + +public class BiometriaRequest { + + @NotBlank + private String fingerPrint; + + public BiometriaRequest(String fingerPrint) { + this.fingerPrint = fingerPrint; + } + + public Biometria toModel(Cartao cartao) { + return new Biometria(fingerPrint, cartao); + } +} diff --git a/src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/Cartao.java b/src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/Cartao.java index 360639cb..eba07139 100644 --- a/src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/Cartao.java +++ b/src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/Cartao.java @@ -84,4 +84,8 @@ public Cartao(String numero, LocalDateTime emitidoEm, String titular, Long limit @Deprecated public Cartao() { } + + public Long getId() { + return id; + } } diff --git a/src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/CartaoRepository.java b/src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/CartaoRepository.java new file mode 100644 index 00000000..86cee253 --- /dev/null +++ b/src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/CartaoRepository.java @@ -0,0 +1,6 @@ +package br.com.zupacademy.william.proposta.proposta.cartao; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CartaoRepository extends JpaRepository { +}