-
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.
- Loading branch information
1 parent
a0ae5e5
commit 3f29e83
Showing
6 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
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
16 changes: 0 additions & 16 deletions
16
src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/aviso/Aviso.java
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
src/main/java/br/com/zupacademy/william/proposta/proposta/cartao/aviso/AvisoViagem.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,38 @@ | ||
package br.com.zupacademy.william.proposta.proposta.cartao.aviso; | ||
|
||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import java.time.LocalDate; | ||
|
||
@Entity | ||
public class AvisoViagem { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String destino; | ||
private LocalDate dataTermino; | ||
|
||
@CreationTimestamp | ||
private LocalDate instanteAviso; | ||
|
||
private String ipRequisicaoDeBloqueio; | ||
private String userAgent; | ||
|
||
private Long idCartao; | ||
|
||
public AvisoViagem(String destino, LocalDate dataTermino, String ipRequisicaoDeBloqueio, String userAgent) { | ||
this.destino = destino; | ||
this.dataTermino = dataTermino; | ||
this.ipRequisicaoDeBloqueio = ipRequisicaoDeBloqueio; | ||
this.userAgent = userAgent; | ||
} | ||
|
||
@Deprecated | ||
public AvisoViagem() { | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../java/br/com/zupacademy/william/proposta/proposta/cartao/aviso/AvisoViagemController.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,41 @@ | ||
package br.com.zupacademy.william.proposta.proposta.cartao.aviso; | ||
|
||
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.*; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.transaction.Transactional; | ||
import javax.validation.Valid; | ||
import java.util.HashMap; | ||
|
||
@RestController | ||
@RequestMapping("/cartoes/{id}/avisos-viagem") | ||
public class AvisoViagemController { | ||
|
||
@Autowired | ||
private CartaoRepository cartaoRepository; | ||
|
||
@Autowired | ||
private HttpServletRequest request; | ||
|
||
@Transactional | ||
@PostMapping | ||
public ResponseEntity criar(@PathVariable Long id, | ||
@RequestBody @Valid AvisoViagemRequest avisoViagemRequest, | ||
@RequestHeader HashMap<String, String> headers) { | ||
Cartao cartao = cartaoRepository.findById(id) | ||
.orElseThrow(() -> new EntidadeNaoEncontradaException(String.format("Não existe um cartão com id %d", id))); | ||
|
||
String ipClienteDaRequisicao = request.getRemoteAddr(); | ||
String userAgent = headers.get("user-agent"); | ||
|
||
AvisoViagem novoAvisoDeViagem = avisoViagemRequest.toModel(ipClienteDaRequisicao, userAgent); | ||
cartao.associarAvisoDeViagem(novoAvisoDeViagem); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...ain/java/br/com/zupacademy/william/proposta/proposta/cartao/aviso/AvisoViagemRequest.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,42 @@ | ||
package br.com.zupacademy.william.proposta.proposta.cartao.aviso; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
|
||
import javax.validation.constraints.Future; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
import java.time.LocalDate; | ||
|
||
public class AvisoViagemRequest { | ||
|
||
@NotBlank | ||
private String destino; | ||
|
||
@NotNull | ||
@Future | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") | ||
private LocalDate dataTermino; | ||
|
||
public AvisoViagemRequest() { | ||
} | ||
|
||
public void setDestino(String destino) { | ||
this.destino = destino; | ||
} | ||
|
||
public void setDataTermino(LocalDate dataTermino) { | ||
this.dataTermino = dataTermino; | ||
} | ||
|
||
public String getDestino() { | ||
return destino; | ||
} | ||
|
||
public LocalDate getDataTermino() { | ||
return dataTermino; | ||
} | ||
|
||
public AvisoViagem toModel(String ipClienteDaRequisicao, String userAgent) { | ||
return new AvisoViagem(this.getDestino(), this.getDataTermino(), ipClienteDaRequisicao, userAgent); | ||
} | ||
} |
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