forked from fabiomsrs/orange-talents-06-template-proposta
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/aviso/AvisoController.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,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(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/aviso/AvisoModel.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,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; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/aviso/AvisoRepository.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,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> { | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/aviso/AvisoRequest.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,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); | ||
} | ||
} |