Skip to content

Commit ae3e7bc

Browse files
committed
#179 - Camada de infraestrutura de pedido
1 parent e1d9ad7 commit ae3e7bc

File tree

7 files changed

+594
-0
lines changed

7 files changed

+594
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
3+
using JSE.Pedidos.Domain.Pedidos;
4+
5+
namespace JSE.Pedidos.Infra.Data.Mappings
6+
{
7+
public class PedidoItemMapping : IEntityTypeConfiguration<PedidoItem>
8+
{
9+
public void Configure(EntityTypeBuilder<PedidoItem> builder)
10+
{
11+
builder.HasKey(c => c.Id);
12+
13+
builder.Property(c => c.ProdutoNome)
14+
.IsRequired()
15+
.HasColumnType("varchar(250)");
16+
17+
// 1 : N => Pedido : Pagamento
18+
builder.HasOne(c => c.Pedido)
19+
.WithMany(c => c.PedidoItems);
20+
21+
builder.ToTable("PedidoItems");
22+
}
23+
}
24+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
3+
using JSE.Pedidos.Domain.Pedidos;
4+
5+
namespace JSE.Pedidos.Infra.Data.Mappings
6+
{
7+
public class PedidoMapping : IEntityTypeConfiguration<Pedido>
8+
{
9+
public void Configure(EntityTypeBuilder<Pedido> builder)
10+
{
11+
builder.HasKey(c => c.Id);
12+
13+
builder.OwnsOne(p => p.Endereco, e =>
14+
{
15+
e.Property(pe => pe.Logradouro)
16+
.HasColumnName("Logradouro");
17+
18+
e.Property(pe => pe.Numero)
19+
.HasColumnName("Numero");
20+
21+
e.Property(pe => pe.Complemento)
22+
.HasColumnName("Complemento");
23+
24+
e.Property(pe => pe.Bairro)
25+
.HasColumnName("Bairro");
26+
27+
e.Property(pe => pe.Cep)
28+
.HasColumnName("Cep");
29+
30+
e.Property(pe => pe.Cidade)
31+
.HasColumnName("Cidade");
32+
33+
e.Property(pe => pe.Estado)
34+
.HasColumnName("Estado");
35+
});
36+
37+
builder.Property(c => c.Codigo)
38+
.HasDefaultValueSql("NEXT VALUE FOR MinhaSequencia");
39+
40+
// 1 : N => Pedido : PedidoItems
41+
builder.HasMany(c => c.PedidoItems)
42+
.WithOne(c => c.Pedido)
43+
.HasForeignKey(c => c.PedidoId);
44+
45+
builder.ToTable("Pedidos");
46+
}
47+
}
48+
}

src/services/JSE.Pedido.Infra/Data/PedidosContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using JSE.Core.DomainObjects;
44
using JSE.Core.Mediator;
55
using JSE.Core.Messages;
6+
using JSE.Pedidos.Domain.Pedidos;
67
using JSE.Pedidos.Domain.Vouchers;
78
using Microsoft.EntityFrameworkCore;
89

@@ -18,6 +19,8 @@ public PedidosContext(DbContextOptions<PedidosContext> options, IMediatorHandler
1819
_mediatorHandler = mediatorHandler;
1920
}
2021

22+
public DbSet<Pedido> Pedidos { get; set; }
23+
public DbSet<PedidoItem> PedidoItems { get; set; }
2124
public DbSet<Voucher> Vouchers { get; set; }
2225

2326
protected override void OnModelCreating(ModelBuilder modelBuilder)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using JSE.Core.Data;
2+
using JSE.Pedidos.Domain.Pedidos;
3+
using Microsoft.EntityFrameworkCore;
4+
using System.Data.Common;
5+
6+
namespace JSE.Pedidos.Infra.Data.Repository
7+
{
8+
public class PedidoRepository : IPedidoRepository
9+
{
10+
private readonly PedidosContext _context;
11+
12+
public PedidoRepository(PedidosContext context)
13+
{
14+
_context = context;
15+
}
16+
17+
public IUnitOfWork UnitOfWork => _context;
18+
19+
public DbConnection ObterConexao() => _context.Database.GetDbConnection();
20+
21+
public async Task<Pedido> ObterPorId(Guid id)
22+
{
23+
return await _context.Pedidos.FindAsync(id);
24+
}
25+
26+
public async Task<IEnumerable<Pedido>> ObterListaPorClienteId(Guid clienteId)
27+
{
28+
return await _context.Pedidos
29+
.Include(p => p.PedidoItems)
30+
.AsNoTracking()
31+
.Where(p => p.ClienteId == clienteId)
32+
.ToListAsync();
33+
}
34+
35+
public void Adicionar(Pedido pedido)
36+
{
37+
_context.Pedidos.Add(pedido);
38+
}
39+
40+
public void Atualizar(Pedido pedido)
41+
{
42+
_context.Pedidos.Update(pedido);
43+
}
44+
45+
46+
public async Task<PedidoItem> ObterItemPorId(Guid id)
47+
{
48+
return await _context.PedidoItems.FindAsync(id);
49+
}
50+
51+
public async Task<PedidoItem> ObterItemPorPedido(Guid pedidoId, Guid produtoId)
52+
{
53+
return await _context.PedidoItems
54+
.FirstOrDefaultAsync(p => p.ProdutoId == produtoId && p.PedidoId == pedidoId);
55+
}
56+
57+
public void Dispose()
58+
{
59+
_context.Dispose();
60+
}
61+
}
62+
}

src/services/JSE.Pedido.Infra/Migrations/20241107135038_Pedidos.Designer.cs

Lines changed: 222 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)