Skip to content

Commit 52fcf4b

Browse files
committed
#182 - Utilizar o Dapper na Query Stack - pt I
1 parent 526f668 commit 52fcf4b

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using JSE.Pedidos.API.Application.DTO;
2+
3+
namespace JSE.Pedidos.API.Application.Queries
4+
{
5+
public interface IPedidoQueries
6+
{
7+
Task<PedidoDTO> ObterUltimoPedido(Guid clienteId);
8+
Task<IEnumerable<PedidoDTO>> ObterListaPorClienteId(Guid clienteId);
9+
}
10+
11+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Dapper;
6+
using JSE.Pedidos.API.Application.DTO;
7+
using JSE.Pedidos.Domain.Pedidos;
8+
9+
namespace JSE.Pedidos.API.Application.Queries
10+
{
11+
12+
public class PedidoQueries : IPedidoQueries
13+
{
14+
private readonly IPedidoRepository _pedidoRepository;
15+
16+
public PedidoQueries(IPedidoRepository pedidoRepository)
17+
{
18+
_pedidoRepository = pedidoRepository;
19+
}
20+
21+
public async Task<PedidoDTO> ObterUltimoPedido(Guid clienteId)
22+
{
23+
const string sql = @"SELECT
24+
P.ID AS 'ProdutoId', P.CODIGO, P.VOUCHERUTILIZADO, P.DESCONTO, P.VALORTOTAL,P.PEDIDOSTATUS,
25+
P.LOGRADOURO,P.NUMERO, P.BAIRRO, P.CEP, P.COMPLEMENTO, P.CIDADE, P.ESTADO,
26+
PIT.ID AS 'ProdutoItemId',PIT.PRODUTONOME, PIT.QUANTIDADE, PIT.PRODUTOIMAGEM, PIT.VALORUNITARIO
27+
FROM PEDIDOS P
28+
INNER JOIN PEDIDOITEMS PIT ON P.ID = PIT.PEDIDOID
29+
WHERE P.CLIENTEID = @clienteId
30+
AND P.DATACADASTRO between DATEADD(minute, -3, GETDATE()) and DATEADD(minute, 0, GETDATE())
31+
AND P.PEDIDOSTATUS = 1
32+
ORDER BY P.DATACADASTRO DESC";
33+
34+
var pedido = await _pedidoRepository.ObterConexao()
35+
.QueryAsync<dynamic>(sql, new { clienteId });
36+
37+
return MapearPedido(pedido);
38+
}
39+
40+
public async Task<IEnumerable<PedidoDTO>> ObterListaPorClienteId(Guid clienteId)
41+
{
42+
var pedidos = await _pedidoRepository.ObterListaPorClienteId(clienteId);
43+
44+
return pedidos.Select(PedidoDTO.ParaPedidoDTO);
45+
}
46+
47+
private PedidoDTO MapearPedido(dynamic result)
48+
{
49+
var pedido = new PedidoDTO
50+
{
51+
Codigo = result[0].CODIGO,
52+
Status = result[0].PEDIDOSTATUS,
53+
ValorTotal = result[0].VALORTOTAL,
54+
Desconto = result[0].DESCONTO,
55+
VoucherUtilizado = result[0].VOUCHERUTILIZADO,
56+
57+
PedidoItems = new List<PedidoItemDTO>(),
58+
Endereco = new EnderecoDTO
59+
{
60+
Logradouro = result[0].LOGRADOURO,
61+
Bairro = result[0].BAIRRO,
62+
Cep = result[0].CEP,
63+
Cidade = result[0].CIDADE,
64+
Complemento = result[0].COMPLEMENTO,
65+
Estado = result[0].ESTADO,
66+
Numero = result[0].NUMERO
67+
}
68+
};
69+
70+
foreach (var item in result)
71+
{
72+
var pedidoItem = new PedidoItemDTO
73+
{
74+
Nome = item.PRODUTONOME,
75+
Valor = item.VALORUNITARIO,
76+
Quantidade = item.QUANTIDADE,
77+
Imagem = item.PRODUTOIMAGEM
78+
};
79+
80+
pedido.PedidoItems.Add(pedidoItem);
81+
}
82+
83+
return pedido;
84+
}
85+
}
86+
87+
}

src/services/JSE.Pedido.API/JSE.Pedidos.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Dapper" Version="2.1.35" />
1415
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.10">
1516
<PrivateAssets>all</PrivateAssets>
1617
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

src/services/JSE.Pedido.Domain/Pedidos/IPedidoRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public interface IPedidoRepository : IRepository<Pedido>
1010
void Adicionar(Pedido pedido);
1111
void Atualizar(Pedido pedido);
1212

13+
DbConnection ObterConexao();
14+
1315
// Pedido Item
1416
Task<PedidoItem> ObterItemPorId(Guid id);
1517
Task<PedidoItem> ObterItemPorPedido(Guid pedidoId, Guid produtoId);

0 commit comments

Comments
 (0)