-
Notifications
You must be signed in to change notification settings - Fork 59
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
7 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/br/com/zupacademy/fabiano/mercadolivre/controller/CompraController.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,49 @@ | ||
package br.com.zupacademy.fabiano.mercadolivre.controller; | ||
|
||
import br.com.zupacademy.fabiano.mercadolivre.dto.CompraDto; | ||
import br.com.zupacademy.fabiano.mercadolivre.modelo.Compra; | ||
import br.com.zupacademy.fabiano.mercadolivre.modelo.Produto; | ||
import br.com.zupacademy.fabiano.mercadolivre.modelo.Usuario; | ||
import br.com.zupacademy.fabiano.mercadolivre.repository.CompraRepository; | ||
import br.com.zupacademy.fabiano.mercadolivre.repository.ProdutoRepository; | ||
import br.com.zupacademy.fabiano.mercadolivre.utils.EnviadorEmail; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequestMapping("/compras") | ||
public class CompraController { | ||
@Autowired | ||
CompraRepository repository; | ||
@Autowired | ||
ProdutoRepository produtoRepository; | ||
@Autowired | ||
EnviadorEmail sendGrid; | ||
|
||
@PostMapping | ||
public ResponseEntity<?> comprar(@RequestBody @Valid CompraDto dto){ | ||
Usuario usuario = (Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
Produto produto = produtoRepository.findById(dto.getProduto()).get(); | ||
|
||
if(produto.abaterCompra(dto.getQuantidade())){ | ||
produtoRepository.save(produto); | ||
Compra compra = dto.converter(produtoRepository, usuario); | ||
repository.save(compra); | ||
|
||
sendGrid.enviaEmail(usuario); | ||
return ResponseEntity.status(HttpStatus.FOUND).body(compra.getGatewayPagamento().getUrl(compra.getId())); | ||
} | ||
|
||
return ResponseEntity.badRequest().body("produto quantidade insuficience"); | ||
} | ||
|
||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/br/com/zupacademy/fabiano/mercadolivre/dto/CompraDto.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,59 @@ | ||
package br.com.zupacademy.fabiano.mercadolivre.dto; | ||
|
||
import br.com.zupacademy.fabiano.mercadolivre.modelo.*; | ||
import br.com.zupacademy.fabiano.mercadolivre.repository.ProdutoRepository; | ||
import br.com.zupacademy.fabiano.mercadolivre.validator.ChecaExistencia; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Positive; | ||
|
||
public class CompraDto { | ||
@NotNull | ||
@Positive | ||
private Integer quantidade; | ||
@NotNull | ||
private GatewayPagamento gatewayPagamento; | ||
private StatusCompra status = StatusCompra.INICIADA; | ||
@NotNull | ||
@ChecaExistencia(identityField = "id", instanceClass = Produto.class) | ||
private Long produto; | ||
|
||
public CompraDto() { | ||
} | ||
|
||
public CompraDto(@NotNull @Positive Integer quantidade, | ||
@NotNull GatewayPagamento gatewayPagamento, | ||
@NotNull Long produto) { | ||
this.quantidade = quantidade; | ||
this.gatewayPagamento = gatewayPagamento; | ||
this.status = StatusCompra.INICIADA; | ||
this.produto = produto; | ||
} | ||
|
||
public Compra converter(ProdutoRepository repository, Usuario usuario){ | ||
Produto produto = repository.findById(this.produto).get(); | ||
return new Compra( | ||
this.quantidade, | ||
this.gatewayPagamento, | ||
this.status, | ||
produto, | ||
usuario | ||
); | ||
} | ||
|
||
public Integer getQuantidade() { | ||
return quantidade; | ||
} | ||
|
||
public GatewayPagamento getGatewayPagamento() { | ||
return gatewayPagamento; | ||
} | ||
|
||
public StatusCompra getStatus() { | ||
return status; | ||
} | ||
|
||
public Long getProduto() { | ||
return produto; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/br/com/zupacademy/fabiano/mercadolivre/modelo/Compra.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,47 @@ | ||
package br.com.zupacademy.fabiano.mercadolivre.modelo; | ||
|
||
import javax.persistence.*; | ||
import javax.validation.constraints.NotNull; | ||
|
||
@Entity | ||
public class Compra { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@NotNull | ||
private Integer quantidade; | ||
@NotNull | ||
@Enumerated(EnumType.STRING) | ||
private GatewayPagamento gatewayPagamento; | ||
@Enumerated(EnumType.STRING) | ||
private StatusCompra status = StatusCompra.INICIADA; | ||
@NotNull | ||
@ManyToOne | ||
private Produto produto; | ||
@NotNull | ||
@ManyToOne | ||
private Usuario comprador; | ||
|
||
public Compra() { | ||
} | ||
|
||
public Compra(@NotNull Integer quantidade, | ||
@NotNull GatewayPagamento gatewayPagamento, | ||
@NotNull StatusCompra status, | ||
@NotNull Produto produto, | ||
@NotNull Usuario comprador) { | ||
this.quantidade = quantidade; | ||
this.gatewayPagamento = gatewayPagamento; | ||
this.status = status; | ||
this.produto = produto; | ||
this.comprador = comprador; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public GatewayPagamento getGatewayPagamento() { | ||
return gatewayPagamento; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/br/com/zupacademy/fabiano/mercadolivre/modelo/GatewayPagamento.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,18 @@ | ||
package br.com.zupacademy.fabiano.mercadolivre.modelo; | ||
|
||
public enum GatewayPagamento { | ||
|
||
PAYPAL, | ||
PAGSEGURO; | ||
|
||
private String url; | ||
|
||
static { | ||
PAYPAL.url = "paypal.com?buyerId="; | ||
PAGSEGURO.url = "pagseguro.com?returnId="; | ||
} | ||
|
||
public String getUrl(Long id) { | ||
return this.url + id.toString() + "&redirectUrl=retornoPosPagamento"; | ||
} | ||
} |
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/fabiano/mercadolivre/modelo/StatusCompra.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.fabiano.mercadolivre.modelo; | ||
|
||
public enum StatusCompra { | ||
INICIADA, | ||
FINALIZADA; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/br/com/zupacademy/fabiano/mercadolivre/repository/CompraRepository.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,7 @@ | ||
package br.com.zupacademy.fabiano.mercadolivre.repository; | ||
|
||
import br.com.zupacademy.fabiano.mercadolivre.modelo.Compra; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CompraRepository extends JpaRepository<Compra, Long> { | ||
} |