|
|
@ -1,12 +1,10 @@ |
|
|
|
using MediatR; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.EntityFrameworkCore.Metadata; |
|
|
|
using Microsoft.EntityFrameworkCore.Metadata.Builders; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; |
|
|
|
using Ordering.Infrastructure; |
|
|
|
using Ordering.Infrastructure.EntityConfigurations; |
|
|
|
using System; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
@ -17,7 +15,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure |
|
|
|
: DbContext,IUnitOfWork |
|
|
|
|
|
|
|
{ |
|
|
|
const string DEFAULT_SCHEMA = "ordering"; |
|
|
|
public const string DEFAULT_SCHEMA = "ordering"; |
|
|
|
|
|
|
|
public DbSet<Order> Orders { get; set; } |
|
|
|
|
|
|
@ -40,6 +38,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure |
|
|
|
} |
|
|
|
|
|
|
|
private OrderingContext(DbContextOptions options) : base (options) { } |
|
|
|
|
|
|
|
public OrderingContext(DbContextOptions options, IMediator mediator) : base(options) |
|
|
|
{ |
|
|
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); |
|
|
@ -50,193 +49,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure |
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder) |
|
|
|
{ |
|
|
|
|
|
|
|
modelBuilder.Entity<ClientRequest>(ConfigureRequests); |
|
|
|
modelBuilder.Entity<PaymentMethod>(ConfigurePayment); |
|
|
|
modelBuilder.Entity<Order>(ConfigureOrder); |
|
|
|
modelBuilder.Entity<OrderItem>(ConfigureOrderItems); |
|
|
|
modelBuilder.Entity<CardType>(ConfigureCardTypes); |
|
|
|
modelBuilder.Entity<OrderStatus>(ConfigureOrderStatus); |
|
|
|
modelBuilder.Entity<Buyer>(ConfigureBuyer); |
|
|
|
} |
|
|
|
|
|
|
|
private void ConfigureRequests(EntityTypeBuilder<ClientRequest> requestConfiguration) |
|
|
|
{ |
|
|
|
requestConfiguration.ToTable("requests", DEFAULT_SCHEMA); |
|
|
|
requestConfiguration.HasKey(cr => cr.Id); |
|
|
|
requestConfiguration.Property(cr => cr.Name).IsRequired(); |
|
|
|
requestConfiguration.Property(cr => cr.Time).IsRequired(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ConfigureBuyer(EntityTypeBuilder<Buyer> buyerConfiguration) |
|
|
|
{ |
|
|
|
buyerConfiguration.ToTable("buyers", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
buyerConfiguration.HasKey(b => b.Id); |
|
|
|
|
|
|
|
buyerConfiguration.Ignore(b => b.DomainEvents); |
|
|
|
|
|
|
|
buyerConfiguration.Property(b => b.Id) |
|
|
|
.ForSqlServerUseSequenceHiLo("buyerseq", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
buyerConfiguration.Property(b=>b.IdentityGuid) |
|
|
|
.HasMaxLength(200) |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
buyerConfiguration.HasIndex("IdentityGuid") |
|
|
|
.IsUnique(true); |
|
|
|
|
|
|
|
buyerConfiguration.HasMany(b => b.PaymentMethods) |
|
|
|
.WithOne() |
|
|
|
.HasForeignKey("BuyerId") |
|
|
|
.OnDelete(DeleteBehavior.Cascade); |
|
|
|
|
|
|
|
var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods)); |
|
|
|
|
|
|
|
navigation.SetPropertyAccessMode(PropertyAccessMode.Field); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigurePayment(EntityTypeBuilder<PaymentMethod> paymentConfiguration) |
|
|
|
{ |
|
|
|
paymentConfiguration.ToTable("paymentmethods", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
paymentConfiguration.HasKey(b => b.Id); |
|
|
|
|
|
|
|
paymentConfiguration.Ignore(b => b.DomainEvents); |
|
|
|
|
|
|
|
paymentConfiguration.Property(b => b.Id) |
|
|
|
.ForSqlServerUseSequenceHiLo("paymentseq", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
paymentConfiguration.Property<int>("BuyerId") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
paymentConfiguration.Property<string>("CardHolderName") |
|
|
|
.HasMaxLength(200) |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
paymentConfiguration.Property<string>("Alias") |
|
|
|
.HasMaxLength(200) |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
paymentConfiguration.Property<string>("CardNumber") |
|
|
|
.HasMaxLength(25) |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
paymentConfiguration.Property<DateTime>("Expiration") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
paymentConfiguration.Property<int>("CardTypeId") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
paymentConfiguration.HasOne(p => p.CardType) |
|
|
|
.WithMany() |
|
|
|
.HasForeignKey("CardTypeId"); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureOrder(EntityTypeBuilder<Order> orderConfiguration) |
|
|
|
{ |
|
|
|
orderConfiguration.ToTable("orders", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
orderConfiguration.HasKey(o => o.Id); |
|
|
|
|
|
|
|
orderConfiguration.Ignore(b => b.DomainEvents); |
|
|
|
|
|
|
|
orderConfiguration.Property(o => o.Id) |
|
|
|
.ForSqlServerUseSequenceHiLo("orderseq", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
orderConfiguration.OwnsOne(o => o.Address); |
|
|
|
|
|
|
|
orderConfiguration.Property<DateTime>("OrderDate").IsRequired(); |
|
|
|
orderConfiguration.Property<int?>("BuyerId").IsRequired(false); |
|
|
|
orderConfiguration.Property<int>("OrderStatusId").IsRequired(); |
|
|
|
orderConfiguration.Property<int?>("PaymentMethodId").IsRequired(false); |
|
|
|
orderConfiguration.Property<string>("Description").IsRequired(false); |
|
|
|
|
|
|
|
var navigation = orderConfiguration.Metadata.FindNavigation(nameof(Order.OrderItems)); |
|
|
|
// DDD Patterns comment:
|
|
|
|
//Set as Field (New since EF 1.1) to access the OrderItem collection property through its field
|
|
|
|
navigation.SetPropertyAccessMode(PropertyAccessMode.Field); |
|
|
|
|
|
|
|
orderConfiguration.HasOne<PaymentMethod>() |
|
|
|
.WithMany() |
|
|
|
.HasForeignKey("PaymentMethodId") |
|
|
|
.IsRequired(false) |
|
|
|
.OnDelete(DeleteBehavior.Restrict); |
|
|
|
|
|
|
|
orderConfiguration.HasOne<Buyer>() |
|
|
|
.WithMany() |
|
|
|
.IsRequired(false) |
|
|
|
.HasForeignKey("BuyerId"); |
|
|
|
|
|
|
|
orderConfiguration.HasOne(o => o.OrderStatus) |
|
|
|
.WithMany() |
|
|
|
.HasForeignKey("OrderStatusId"); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureOrderItems(EntityTypeBuilder<OrderItem> orderItemConfiguration) |
|
|
|
{ |
|
|
|
orderItemConfiguration.ToTable("orderItems", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
orderItemConfiguration.HasKey(o => o.Id); |
|
|
|
|
|
|
|
orderItemConfiguration.Ignore(b => b.DomainEvents); |
|
|
|
|
|
|
|
orderItemConfiguration.Property(o => o.Id) |
|
|
|
.ForSqlServerUseSequenceHiLo("orderitemseq"); |
|
|
|
|
|
|
|
orderItemConfiguration.Property<int>("OrderId") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
orderItemConfiguration.Property<decimal>("Discount") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
orderItemConfiguration.Property<int>("ProductId") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
orderItemConfiguration.Property<string>("ProductName") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
orderItemConfiguration.Property<decimal>("UnitPrice") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
orderItemConfiguration.Property<int>("Units") |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
orderItemConfiguration.Property<string>("PictureUrl") |
|
|
|
.IsRequired(false); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureOrderStatus(EntityTypeBuilder<OrderStatus> orderStatusConfiguration) |
|
|
|
{ |
|
|
|
orderStatusConfiguration.ToTable("orderstatus", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
orderStatusConfiguration.HasKey(o => o.Id); |
|
|
|
|
|
|
|
orderStatusConfiguration.Property(o => o.Id) |
|
|
|
.HasDefaultValue(1) |
|
|
|
.ValueGeneratedNever() |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
orderStatusConfiguration.Property(o => o.Name) |
|
|
|
.HasMaxLength(200) |
|
|
|
.IsRequired(); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureCardTypes(EntityTypeBuilder<CardType> cardTypesConfiguration) |
|
|
|
{ |
|
|
|
cardTypesConfiguration.ToTable("cardtypes", DEFAULT_SCHEMA); |
|
|
|
|
|
|
|
cardTypesConfiguration.HasKey(ct => ct.Id); |
|
|
|
|
|
|
|
cardTypesConfiguration.Property(ct => ct.Id) |
|
|
|
.HasDefaultValue(1) |
|
|
|
.ValueGeneratedNever() |
|
|
|
.IsRequired(); |
|
|
|
|
|
|
|
cardTypesConfiguration.Property(ct => ct.Name) |
|
|
|
.HasMaxLength(200) |
|
|
|
.IsRequired(); |
|
|
|
modelBuilder.ApplyConfiguration(new ClientRequestEntityTypeConfiguration()); |
|
|
|
modelBuilder.ApplyConfiguration(new PaymentMethodEntityTypeConfiguration()); |
|
|
|
modelBuilder.ApplyConfiguration(new OrderEntityTypeConfiguration()); |
|
|
|
modelBuilder.ApplyConfiguration(new OrderItemEntityTypeConfiguration()); |
|
|
|
modelBuilder.ApplyConfiguration(new CardTypeEntityTypeConfiguration()); |
|
|
|
modelBuilder.ApplyConfiguration(new OrderStatusEntityTypeConfiguration()); |
|
|
|
modelBuilder.ApplyConfiguration(new BuyerEntityTypeConfiguration()); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken)) |
|
|
|