-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cadastro de biometria para cartão
- Loading branch information
1 parent
9b790ec
commit 8a908a9
Showing
6 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/br/com/zupacademy/william/proposta/biometria/Biometria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/br/com/zupacademy/william/proposta/biometria/BiometriaRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/CartaoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |