Skip to content

Commit

Permalink
feat: cadastro de biometria para cartão
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidawilliam committed May 10, 2021
1 parent 9b790ec commit 8a908a9
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package br.com.zupacademy.william.proposta.biometria;

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

public interface BiometriaRepository extends JpaRepository<Biometria, Long> {
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ public Cartao(String numero, LocalDateTime emitidoEm, String titular, Long limit
@Deprecated
public Cartao() {
}

public Long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package br.com.zupacademy.william.proposta.proposta.cartao;

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

public interface CartaoRepository extends JpaRepository<Cartao, Long> {
}

0 comments on commit 8a908a9

Please sign in to comment.