Simplify data representation
This commit is contained in:
@@ -32,10 +32,6 @@ public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
|
||||
.HasForeignKey(t => t.GroupId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<ProductDbEntity>()
|
||||
.Property(p => p.Discount)
|
||||
.HasPrecision(18, 2);
|
||||
|
||||
modelBuilder.Entity<ProductVariantDbEntity>()
|
||||
.Property(v => v.Price)
|
||||
.HasPrecision(18, 2);
|
||||
|
||||
@@ -7,23 +7,20 @@ namespace Printbase.Infrastructure.DbEntities.Products;
|
||||
public class ProductDbEntity
|
||||
{
|
||||
[Key, Required]
|
||||
public Guid Id { get; set; }
|
||||
public Guid Id { get; init; }
|
||||
|
||||
[MaxLength(50), Required]
|
||||
public required string Name { get; set; }
|
||||
public required string Name { get; init; }
|
||||
|
||||
[MaxLength(1000)]
|
||||
public string? Description { get; set; }
|
||||
|
||||
[Column(TypeName = "decimal(18,2)")]
|
||||
public decimal? Discount { get; set; }
|
||||
public string? Description { get; init; }
|
||||
|
||||
[Required]
|
||||
public Guid TypeId { get; set; }
|
||||
public Guid TypeId { get; init; }
|
||||
|
||||
[ForeignKey(nameof(TypeId)), Required]
|
||||
public required ProductTypeDbEntity Type { get; set; }
|
||||
|
||||
public required ProductTypeDbEntity Type { get; init; }
|
||||
|
||||
[InverseProperty(nameof(ProductVariantDbEntity.Product)), Required]
|
||||
public required ICollection<ProductVariantDbEntity> Variants { get; set; }
|
||||
public required ICollection<ProductVariantDbEntity> Variants { get; init; } = [];
|
||||
}
|
||||
@@ -7,20 +7,20 @@ namespace Printbase.Infrastructure.DbEntities.Products;
|
||||
public class ProductTypeDbEntity
|
||||
{
|
||||
[Key, Required]
|
||||
public Guid Id { get; set; }
|
||||
public Guid Id { get; init; }
|
||||
|
||||
[MaxLength(50), Required]
|
||||
public required string Name { get; set; }
|
||||
public required string Name { get; init; }
|
||||
|
||||
[MaxLength(255)]
|
||||
public string? Description { get; set; }
|
||||
public string? Description { get; init; }
|
||||
|
||||
[Required]
|
||||
public Guid GroupId { get; set; }
|
||||
public Guid GroupId { get; init; }
|
||||
|
||||
[ForeignKey(nameof(GroupId)), Required]
|
||||
public required ProductGroupDbEntity Group { get; set; }
|
||||
public required ProductGroupDbEntity Group { get; init; }
|
||||
|
||||
[InverseProperty(nameof(ProductDbEntity.Type))]
|
||||
public ICollection<ProductDbEntity>? Products { get; set; }
|
||||
public ICollection<ProductDbEntity>? Products { get; init; }
|
||||
}
|
||||
@@ -7,26 +7,26 @@ namespace Printbase.Infrastructure.DbEntities.Products;
|
||||
public class ProductVariantDbEntity
|
||||
{
|
||||
[Key, Required]
|
||||
public Guid Id { get; set; }
|
||||
public Guid Id { get; init; }
|
||||
|
||||
[Required]
|
||||
public Guid ProductId { get; set; }
|
||||
public Guid ProductId { get; init; }
|
||||
|
||||
[MaxLength(50)]
|
||||
public string? Color { get; set; }
|
||||
public string? Color { get; init; }
|
||||
|
||||
[MaxLength(20)]
|
||||
public string? Size { get; set; }
|
||||
public string? Size { get; init; }
|
||||
|
||||
[Column(TypeName = "decimal(18,2)"), Required]
|
||||
public decimal Price { get; set; }
|
||||
public decimal Price { get; init; }
|
||||
|
||||
[Column(TypeName = "decimal(18,2)")]
|
||||
public decimal? Discount { get; set; }
|
||||
public decimal? Discount { get; init; }
|
||||
|
||||
[Required]
|
||||
public int Stock { get; set; }
|
||||
public int Stock { get; init; }
|
||||
|
||||
[ForeignKey(nameof(ProductId)), Required]
|
||||
public required ProductDbEntity Product { get; set; }
|
||||
public required ProductDbEntity Product { get; init; }
|
||||
}
|
||||
@@ -8,67 +8,14 @@ public class ProductMappingProfile : Profile
|
||||
{
|
||||
public ProductMappingProfile()
|
||||
{
|
||||
CreateMap<Product, ProductDbEntity>()
|
||||
.ForMember(dest => dest.Variants, opt => opt.MapFrom(src => src.Variants))
|
||||
.ForMember(dest => dest.Type, opt => opt.Ignore());
|
||||
|
||||
CreateMap<ProductDbEntity, Product>()
|
||||
.ConstructUsing((src, _) => new Product(
|
||||
src.Id,
|
||||
src.Name,
|
||||
src.TypeId,
|
||||
src.Description,
|
||||
src.Discount))
|
||||
.ForMember(dest => dest.Variants, opt => opt.Ignore())
|
||||
.AfterMap((src, dest, ctx) =>
|
||||
{
|
||||
foreach (var dbVariant in src.Variants)
|
||||
{
|
||||
var variant = ctx.Mapper.Map<ProductVariant>(dbVariant);
|
||||
dest.AddVariant(variant);
|
||||
}
|
||||
});
|
||||
|
||||
CreateMap<ProductVariant, ProductVariantDbEntity>()
|
||||
.ForMember(dest => dest.Product, opt => opt.Ignore());
|
||||
|
||||
CreateMap<ProductVariantDbEntity, ProductVariant>()
|
||||
.ConstructUsing((src, _) => new ProductVariant(
|
||||
src.Id,
|
||||
src.ProductId,
|
||||
src.Price,
|
||||
src.Color,
|
||||
src.Size,
|
||||
src.Discount,
|
||||
src.Stock));
|
||||
|
||||
CreateMap<ProductType, ProductTypeDbEntity>()
|
||||
.ForMember(dest => dest.Group, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.Products, opt => opt.Ignore());
|
||||
|
||||
CreateMap<ProductTypeDbEntity, ProductType>()
|
||||
.ConstructUsing((src, _) => new ProductType(
|
||||
src.Id,
|
||||
src.Name,
|
||||
src.GroupId,
|
||||
src.Description));
|
||||
|
||||
CreateMap<ProductGroup, ProductGroupDbEntity>()
|
||||
.ForMember(dest => dest.Types, opt => opt.MapFrom(src => src.Types));
|
||||
|
||||
CreateMap<ProductGroupDbEntity, ProductGroup>()
|
||||
.ConstructUsing((src, _) => new ProductGroup(
|
||||
src.Id,
|
||||
src.Name,
|
||||
src.Description))
|
||||
.ForMember(dest => dest.Types, opt => opt.Ignore())
|
||||
.AfterMap((src, dest, ctx) =>
|
||||
{
|
||||
foreach (var dbType in src.Types)
|
||||
{
|
||||
var type = ctx.Mapper.Map<ProductType>(dbType);
|
||||
dest.AddType(type);
|
||||
}
|
||||
});
|
||||
CreateMap<ProductDbEntity, Product>();
|
||||
CreateMap<ProductGroupDbEntity, ProductGroup>();
|
||||
CreateMap<ProductTypeDbEntity, ProductType>();
|
||||
CreateMap<ProductVariantDbEntity, ProductVariant>();
|
||||
|
||||
CreateMap<Product, ProductDbEntity>();
|
||||
CreateMap<ProductGroup, ProductGroupDbEntity>();
|
||||
CreateMap<ProductType, ProductTypeDbEntity>();
|
||||
CreateMap<ProductVariant, ProductVariantDbEntity>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user