Skip to content

Commit

Permalink
feat: aviso viagem
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomsrs committed Jul 14, 2021
1 parent 3c58d4e commit 3cf7843
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package br.com.zupacademy.fabiano.proposta.controller;

import br.com.zupacademy.fabiano.proposta.dto.AvisoViagemRegisterDto;
import br.com.zupacademy.fabiano.proposta.dto.BiometriaRegisterDto;
import br.com.zupacademy.fabiano.proposta.dto.SolicitacaoDto;
import br.com.zupacademy.fabiano.proposta.modelo.AvisoViagem;
import br.com.zupacademy.fabiano.proposta.modelo.Biometria;
import br.com.zupacademy.fabiano.proposta.modelo.BloqueioCartao;
import br.com.zupacademy.fabiano.proposta.modelo.Cartao;
import br.com.zupacademy.fabiano.proposta.repository.AvisoViagemRepository;
import br.com.zupacademy.fabiano.proposta.repository.BiometriaRepository;
import br.com.zupacademy.fabiano.proposta.repository.BloqueioCartaoRepository;
import br.com.zupacademy.fabiano.proposta.repository.CartaoRepository;
Expand Down Expand Up @@ -35,6 +37,9 @@ public class CartaoController {
@Autowired
BloqueioCartaoRepository bloqueioCartaoRepository;

@Autowired
AvisoViagemRepository avisoViagemRepository;

@Value("${cartao.host}")
private String urlSistemaCartao;

Expand Down Expand Up @@ -85,4 +90,22 @@ public ResponseEntity<?> criarBloqueio(@PathVariable("id") Long id, HttpServletR
}
return ResponseEntity.notFound().build();
}

@PostMapping("/{id}/avisos-viagem")
public ResponseEntity<?> criarAvisoViagem(@PathVariable("id") Long id,
HttpServletRequest request,
@RequestHeader(value = "User-Agent") String userAgent,
@RequestBody @Valid AvisoViagemRegisterDto dto,
UriComponentsBuilder uriBuilder){
Optional<Cartao> optionalCartao = cartaoRepository.findById(id);

if(optionalCartao.isPresent()){
Cartao cartao = optionalCartao.get();
AvisoViagem avisoViagem = dto.converter(userAgent,request.getRemoteAddr(),cartao);
avisoViagemRepository.save(avisoViagem);
URI uri = uriBuilder.path("/{id}/avisos-viagem").buildAndExpand(avisoViagem.getId()).toUri();
return ResponseEntity.created(uri).body(avisoViagem);
}
return ResponseEntity.notFound().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package br.com.zupacademy.fabiano.proposta.dto;

import br.com.zupacademy.fabiano.proposta.modelo.AvisoViagem;
import br.com.zupacademy.fabiano.proposta.modelo.Cartao;
import com.fasterxml.jackson.annotation.JsonFormat;

import javax.validation.constraints.Future;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;

public class AvisoViagemRegisterDto {
@NotNull
@NotEmpty
private String destino;
@NotNull
@Future
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate terminoViagem;

public AvisoViagemRegisterDto() {
}

public AvisoViagemRegisterDto(@NotNull @NotEmpty String destino,
@NotNull @Future @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") LocalDate terminoViagem) {
this.destino = destino;
this.terminoViagem = terminoViagem;
}

public AvisoViagem converter(String userAgent, String remoteAddr, Cartao cartao) {
return new AvisoViagem(this.destino, this.terminoViagem, userAgent, remoteAddr, cartao);
}

public String getDestino() {
return destino;
}

public LocalDate getTerminoViagem() {
return terminoViagem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package br.com.zupacademy.fabiano.proposta.modelo;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
public class AvisoViagem {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
@NotNull
@NotEmpty
private String destinho;
@NotNull
private LocalDate terminoViagem;
@CreationTimestamp
private LocalDateTime createdAt;
@NotNull
@NotEmpty
private String userAgent;
@NotNull
@NotEmpty
private String ipCliente;
@NotNull
@ManyToOne
private Cartao cartao;

public AvisoViagem() {
}

public AvisoViagem(String destinho,
@NotNull LocalDate terminoViagem,
@NotNull @NotEmpty String userAgent,
@NotNull @NotEmpty String ipCliente,
@NotNull Cartao cartao) {
this.destinho = destinho;
this.terminoViagem = terminoViagem;
this.userAgent = userAgent;
this.ipCliente = ipCliente;
this.cartao = cartao;
}

public String getId() {
return id;
}

public String getDestinho() {
return destinho;
}

public LocalDate getTerminoViagem() {
return terminoViagem;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public String getUserAgent() {
return userAgent;
}

public String getIpCliente() {
return ipCliente;
}

public Cartao getCartao() {
return cartao;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.zupacademy.fabiano.proposta.repository;

import br.com.zupacademy.fabiano.proposta.modelo.AvisoViagem;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AvisoViagemRepository extends JpaRepository<AvisoViagem, String> {
}

0 comments on commit 3cf7843

Please sign in to comment.