Skip to content

Commit

Permalink
aviso de viagem
Browse files Browse the repository at this point in the history
  • Loading branch information
giovnnvm committed Aug 3, 2021
1 parent 37f2138 commit c8622c2
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package br.com.zupacademy.giovannimoratto.desafioproposta.aviso;

import br.com.zupacademy.giovannimoratto.desafioproposta.cartao.CartaoModel;
import br.com.zupacademy.giovannimoratto.desafioproposta.cartao.CartaoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static org.springframework.http.HttpStatus.NOT_FOUND;

/**
* @Author giovanni.moratto
*/

@RestController
@RequestMapping("/api")
public class AvisoController {

@Autowired
private CartaoRepository cartaoRepository;
@Autowired
private AvisoRepository avisoRepository;

@PostMapping("aviso-viagem/{id}")
public ResponseEntity <?> avisarViagem(@PathVariable Long id, @RequestBody @Valid AvisoRequest bodyRequest,
HttpServletRequest httpRequest) {

String userAgent = httpRequest.getHeader(HttpHeaders.USER_AGENT);
String ip = httpRequest.getRemoteAddr();

CartaoModel cartao = cartaoRepository.findById(id).orElseThrow(()
-> new ResponseStatusException(NOT_FOUND, "Este cartão não existe."));

AvisoModel novoAviso = bodyRequest.toModel(ip, userAgent, cartao);
novoAviso = avisoRepository.save(novoAviso);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package br.com.zupacademy.giovannimoratto.desafioproposta.aviso;

import br.com.zupacademy.giovannimoratto.desafioproposta.cartao.CartaoModel;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;

import static javax.persistence.GenerationType.IDENTITY;

/**
* @Author giovanni.moratto
*/

@Entity
@Table(name = "tb_avisos_de_viagem")
public class AvisoModel {

/* Attributes */
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@Column(nullable = false)
private String destino;
@Column(nullable = false)
private LocalDate validoAte;
@CreationTimestamp
@Column(nullable = false)
private LocalDateTime dataCriacao;
@Column(nullable = false)
private String ipClient;
@Column(nullable = false)
private String userAgent;
@NotNull
@Valid
@ManyToOne
private CartaoModel cartao;

@Deprecated
public AvisoModel() {
}

public AvisoModel(String destino, LocalDate validoAte, String ipClient, String userAgent, CartaoModel cartao) {
this.destino = destino;
this.validoAte = validoAte;
this.ipClient = ipClient;
this.userAgent = userAgent;
this.cartao = cartao;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package br.com.zupacademy.giovannimoratto.desafioproposta.aviso;

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

/**
* @Author giovanni.moratto
*/

@Repository
public interface AvisoRepository extends JpaRepository <AvisoModel, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package br.com.zupacademy.giovannimoratto.desafioproposta.aviso;

import br.com.zupacademy.giovannimoratto.desafioproposta.cartao.CartaoModel;

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

/**
* @Author giovanni.moratto
*/

public class AvisoRequest {

@NotBlank
private final String destino;
@NotNull
@Future
private final LocalDate validoAte;

public AvisoRequest(String destino, LocalDate validoAte) {
this.destino = destino;
this.validoAte = validoAte;
}

public AvisoModel toModel(String ipClient, String userAgent, CartaoModel cartao) {
return new AvisoModel(destino, validoAte, ipClient, userAgent, cartao);
}
}

0 comments on commit c8622c2

Please sign in to comment.