Skip to content

Commit 9e30033

Browse files
committed
#186 - Cadastro e consulta de endereço do cliente
1 parent d5fae15 commit 9e30033

23 files changed

+774
-29
lines changed

src/services/JSE.Clientes.API/Controllers/ClientController.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using JSE.Clientes.API.Application.Commands;
2+
using JSE.Clientes.API.Models;
3+
using JSE.Core.Mediator;
4+
using JSE.WebAPI.Core.Controllers;
5+
using JSE.WebAPI.Core.User;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
namespace JSE.Clientes.API.Controllers
9+
{
10+
public class ClientesController : MainController
11+
{
12+
private readonly IClienteRepository _clienteRepository;
13+
private readonly IMediatorHandler _mediator;
14+
private readonly IAspNetUser _user;
15+
16+
public ClientesController(IClienteRepository clienteRepository, IMediatorHandler mediator, IAspNetUser user)
17+
{
18+
_clienteRepository = clienteRepository;
19+
_mediator = mediator;
20+
_user = user;
21+
}
22+
23+
[HttpGet("cliente/endereco")]
24+
public async Task<IActionResult> ObterEndereco()
25+
{
26+
var endereco = await _clienteRepository.ObterEnderecoPorId(_user.ObterUserId());
27+
28+
return endereco == null ? NotFound() : CustomResponse(endereco);
29+
}
30+
31+
[HttpPost("cliente/endereco")]
32+
public async Task<IActionResult> AdicionarEndereco(AdicionarEnderecoCommand endereco)
33+
{
34+
endereco.ClienteId = _user.ObterUserId();
35+
return CustomResponse(await _mediator.SendCommand(endereco));
36+
}
37+
}
38+
}

src/web/JSE.WebApp.MVC/Configuration/DependencyInjectionConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public static void RegisterServices(this IServiceCollection services, IConfigura
3636
.AddTransientHttpErrorPolicy(
3737
p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));
3838

39+
services.AddHttpClient<IClienteService, ClienteService>()
40+
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
41+
.AddPolicyHandler(PollyExtensions.EsperarTentar())
42+
.AddTransientHttpErrorPolicy(
43+
p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));
44+
3945
#endregion
4046
}
4147
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Mvc;
5+
using JSE.WebApp.MVC.Models;
6+
using JSE.WebApp.MVC.Services;
7+
8+
namespace JSE.WebApp.MVC.Controllers
9+
{
10+
[Authorize]
11+
public class ClienteController : MainController
12+
{
13+
private readonly IClienteService _clienteService;
14+
15+
public ClienteController(IClienteService clienteService)
16+
{
17+
_clienteService = clienteService;
18+
}
19+
20+
[HttpPost]
21+
public async Task<IActionResult> NovoEndereco(EnderecoViewModel endereco)
22+
{
23+
var response = await _clienteService.AdicionarEndereco(endereco);
24+
25+
if (ResponsePossuiErros(response)) TempData["Erros"] =
26+
ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage)).ToList();
27+
28+
return RedirectToAction("EnderecoEntrega", "Pedido");
29+
}
30+
}
31+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using JSE.WebAPI.Core.Controllers;
2+
using JSE.WebApp.MVC.Models;
3+
using JSE.WebApp.MVC.Services;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace NSE.WebApp.MVC.Controllers
7+
{
8+
public class PedidoController : MainController
9+
{
10+
private readonly IClienteService _clienteService;
11+
private readonly IComprasBffService _comprasBffService;
12+
13+
public PedidoController(IClienteService clienteService,
14+
IComprasBffService comprasBffService)
15+
{
16+
_clienteService = clienteService;
17+
_comprasBffService = comprasBffService;
18+
}
19+
20+
[HttpGet]
21+
[Route("endereco-de-entrega")]
22+
public async Task<IActionResult> EnderecoEntrega()
23+
{
24+
var carrinho = await _comprasBffService.ObterCarrinho();
25+
if (carrinho.Itens.Count == 0) return RedirectToAction("Index", "Carrinho");
26+
27+
var endereco = await _clienteService.ObterEndereco();
28+
var pedido = _comprasBffService.MapearParaPedido(carrinho, endereco);
29+
30+
return View(pedido);
31+
}
32+
33+
[HttpGet]
34+
[Route("pagamento")]
35+
public async Task<IActionResult> Pagamento()
36+
{
37+
var carrinho = await _comprasBffService.ObterCarrinho();
38+
if (carrinho.Itens.Count == 0) return RedirectToAction("Index", "Carrinho");
39+
40+
var pedido = _comprasBffService.MapearParaPedido(carrinho, null);
41+
42+
return View(pedido);
43+
}
44+
45+
[HttpPost]
46+
[Route("finalizar-pedido")]
47+
public async Task<IActionResult> FinalizarPedido(PedidoTransacaoViewModel pedidoTransacao)
48+
{
49+
if (!ModelState.IsValid) return View("Pagamento", _comprasBffService.MapearParaPedido(
50+
await _comprasBffService.ObterCarrinho(), null));
51+
52+
var retorno = await _comprasBffService.FinalizarPedido(pedidoTransacao);
53+
54+
if (ResponsePossuiErros(retorno))
55+
{
56+
var carrinho = await _comprasBffService.ObterCarrinho();
57+
if (carrinho.Itens.Count == 0) return RedirectToAction("Index", "Carrinho");
58+
59+
var pedidoMap = _comprasBffService.MapearParaPedido(carrinho, null);
60+
return View("Pagamento", pedidoMap);
61+
}
62+
63+
return RedirectToAction("PedidoConcluido");
64+
}
65+
66+
[HttpGet]
67+
[Route("pedido-concluido")]
68+
public async Task<IActionResult> PedidoConcluido()
69+
{
70+
return View("ConfirmacaoPedido", await _comprasBffService.ObterUltimoPedido());
71+
}
72+
73+
[HttpGet("meus-pedidos")]
74+
public async Task<IActionResult> MeusPedidos()
75+
{
76+
return View(await _comprasBffService.ObterListaPorClienteId());
77+
}
78+
}
79+
}

src/web/JSE.WebApp.MVC/Extensions/RazorHelpers.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ public static string HashEmailForGravatar(this RazorPage page, string email)
2020

2121
public static string FormatoMoeda(this RazorPage page, decimal valor)
2222
{
23-
return valor > 0 ? string.Format(Thread.CurrentThread.CurrentCulture, "{0:C}", valor) : "Gratuito";
23+
return FormatoMoeda(valor);
24+
}
25+
26+
private static string FormatoMoeda(decimal valor)
27+
{
28+
return string.Format(Thread.CurrentThread.CurrentCulture, "{0:C}", valor);
2429
}
2530

2631
public static string MensagemEstoque(this RazorPage page, int quantidade)
2732
{
2833
return quantidade > 0 ? $"Apenas {quantidade} em estoque!" : "Produto esgotado!";
2934
}
3035

31-
3236
public static string UnidadesPorProduto(this RazorPage page, int unidades)
3337
{
3438
return unidades > 1 ? $"{unidades} unidades" : $"{unidades} unidade";
@@ -46,5 +50,43 @@ public static string SelectOptionsPorQuantidade(this RazorPage page, int quantid
4650

4751
return sb.ToString();
4852
}
53+
54+
public static string UnidadesPorProdutoValorTotal(this RazorPage page, int unidades, decimal valor)
55+
{
56+
return $"{unidades}x {FormatoMoeda(valor)} = Total: {FormatoMoeda(valor * unidades)}";
57+
}
58+
59+
public static string ExibeStatus(this RazorPage page, int status)
60+
{
61+
var statusMensagem = "";
62+
var statusClasse = "";
63+
64+
switch (status)
65+
{
66+
case 1:
67+
statusClasse = "info";
68+
statusMensagem = "Em aprovação";
69+
break;
70+
case 2:
71+
statusClasse = "primary";
72+
statusMensagem = "Aprovado";
73+
break;
74+
case 3:
75+
statusClasse = "danger";
76+
statusMensagem = "Recusado";
77+
break;
78+
case 4:
79+
statusClasse = "success";
80+
statusMensagem = "Entregue";
81+
break;
82+
case 5:
83+
statusClasse = "warning";
84+
statusMensagem = "Cancelado";
85+
break;
86+
87+
}
88+
89+
return $"<span class='badge badge-{statusClasse}'>{statusMensagem}</span>";
90+
}
4991
}
5092
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.ComponentModel;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace JSE.WebApp.MVC.Models
5+
{
6+
public class EnderecoViewModel
7+
{
8+
[Required]
9+
public string Logradouro { get; set; }
10+
[Required]
11+
[DisplayName("Número")]
12+
public string Numero { get; set; }
13+
public string Complemento { get; set; }
14+
[Required]
15+
public string Bairro { get; set; }
16+
[Required]
17+
[DisplayName("CEP")]
18+
public string Cep { get; set; }
19+
[Required]
20+
public string Cidade { get; set; }
21+
[Required]
22+
public string Estado { get; set; }
23+
24+
public override string ToString()
25+
{
26+
return $"{Logradouro}, {Numero} {Complemento} - {Bairro} - {Cidade} - {Estado}";
27+
}
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace JSE.WebApp.MVC.Models
2+
{
3+
public class PedidoTransacaoViewModel
4+
{
5+
#region Pedido
6+
7+
public decimal ValorTotal { get; set; }
8+
public decimal Desconto { get; set; }
9+
public string VoucherCodigo { get; set; }
10+
public bool VoucherUtilizado { get; set; }
11+
12+
public List<ItemCarrinhoViewModel> Itens { get; set; } = new List<ItemCarrinhoViewModel>();
13+
14+
#endregion
15+
16+
#region Endereco
17+
18+
public EnderecoViewModel Endereco { get; set; }
19+
20+
#endregion
21+
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace JSE.WebApp.MVC.Models
2+
{
3+
public class PedidoViewModel
4+
{
5+
#region Pedido
6+
7+
public int Codigo { get; set; }
8+
9+
// Autorizado = 1,
10+
// Pago = 2,
11+
// Recusado = 3,
12+
// Entregue = 4,
13+
// Cancelado = 5
14+
public int Status { get; set; }
15+
public DateTime Data { get; set; }
16+
public decimal ValorTotal { get; set; }
17+
18+
public decimal Desconto { get; set; }
19+
public bool VoucherUtilizado { get; set; }
20+
21+
public List<ItemPedidoViewModel> PedidoItems { get; set; } = new List<ItemPedidoViewModel>();
22+
23+
#endregion
24+
25+
#region Item Pedido
26+
27+
public class ItemPedidoViewModel
28+
{
29+
public Guid ProdutoId { get; set; }
30+
public string Nome { get; set; }
31+
public int Quantidade { get; set; }
32+
public decimal Valor { get; set; }
33+
public string Imagem { get; set; }
34+
}
35+
36+
#endregion
37+
38+
#region Endereco
39+
40+
public EnderecoViewModel Endereco { get; set; }
41+
42+
#endregion
43+
}
44+
}

0 commit comments

Comments
 (0)