Simplify data representation
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
using Printbase.Application.ProductType;
|
||||
|
||||
namespace Printbase.Application.ProductGroup;
|
||||
|
||||
public class ProductGroupDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public List<ProductTypeDto> Types { get; set; } = [];
|
||||
}
|
||||
|
||||
public class ProductGroupCreateDto
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
public class ProductGroupUpdateDto
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
namespace Printbase.Application.ProductType;
|
||||
|
||||
public class ProductTypeDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public Guid GroupId { get; set; }
|
||||
public string GroupName { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class ProductTypeCreateDto
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public Guid GroupId { get; set; }
|
||||
}
|
||||
|
||||
public class ProductTypeUpdateDto
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
namespace Printbase.Application.Products;
|
||||
|
||||
public class ProductDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public Guid TypeId { get; set; }
|
||||
public string TypeName { get; set; } = string.Empty;
|
||||
public List<ProductVariantDto> Variants { get; set; } = new();
|
||||
}
|
||||
|
||||
public class ProductVariantDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid ProductId { get; set; }
|
||||
public string? Color { get; set; }
|
||||
public string? Size { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public int Stock { get; set; }
|
||||
public decimal EffectivePrice { get; set; }
|
||||
}
|
||||
|
||||
public class ProductCreateDto
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public Guid TypeId { get; set; }
|
||||
public List<ProductVariantCreateDto> Variants { get; set; } = new();
|
||||
}
|
||||
|
||||
public class ProductVariantCreateDto
|
||||
{
|
||||
public string? Color { get; set; }
|
||||
public string? Size { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public int Stock { get; set; }
|
||||
}
|
||||
|
||||
public class ProductUpdateDto
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public Guid TypeId { get; set; }
|
||||
}
|
||||
|
||||
public class ProductVariantUpdateDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string? Color { get; set; }
|
||||
public string? Size { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public decimal? Discount { get; set; }
|
||||
public int Stock { get; set; }
|
||||
}
|
||||
@@ -2,79 +2,10 @@ namespace Printbase.Domain.Entities.Products;
|
||||
|
||||
public class Product
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public string? Description { get; private set; }
|
||||
public decimal? Discount { get; private set; }
|
||||
public Guid TypeId { get; private set; }
|
||||
public IReadOnlyCollection<ProductVariant> Variants => _variants.AsReadOnly();
|
||||
|
||||
private readonly List<ProductVariant> _variants = [];
|
||||
|
||||
private Product() { }
|
||||
|
||||
public Product(Guid id, string name, Guid typeId, string? description = null, decimal? discount = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("Product name cannot be empty", nameof(name));
|
||||
|
||||
if (typeId == Guid.Empty)
|
||||
throw new ArgumentException("Type ID cannot be empty", nameof(typeId));
|
||||
|
||||
Id = id;
|
||||
Name = name;
|
||||
TypeId = typeId;
|
||||
Description = description;
|
||||
Discount = discount;
|
||||
}
|
||||
|
||||
public void AddVariant(ProductVariant variant)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(variant);
|
||||
|
||||
_variants.Add(variant);
|
||||
}
|
||||
|
||||
public void RemoveVariant(Guid variantId)
|
||||
{
|
||||
var variant = _variants.Find(v => v.Id == variantId);
|
||||
if (variant != null)
|
||||
{
|
||||
_variants.Remove(variant);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateName(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("Product name cannot be empty", nameof(name));
|
||||
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public void UpdateDescription(string? description)
|
||||
{
|
||||
Description = description;
|
||||
}
|
||||
|
||||
public void UpdateDiscount(decimal? discount)
|
||||
{
|
||||
if (discount.HasValue && (discount.Value < 0 || discount.Value > 100))
|
||||
throw new ArgumentException("Discount must be between 0 and 100", nameof(discount));
|
||||
|
||||
Discount = discount;
|
||||
}
|
||||
|
||||
public void UpdateType(Guid typeId)
|
||||
{
|
||||
if (typeId == Guid.Empty)
|
||||
throw new ArgumentException("Type ID cannot be empty", nameof(typeId));
|
||||
|
||||
TypeId = typeId;
|
||||
}
|
||||
|
||||
public decimal? GetEffectiveDiscount()
|
||||
{
|
||||
return Discount;
|
||||
}
|
||||
public Guid Id { get; init; }
|
||||
public string Name { get; init; } = string.Empty;
|
||||
public string? Description { get; init; }
|
||||
public Guid TypeId { get; init; }
|
||||
public ProductType Type { get; init; } = null!;
|
||||
public ICollection<ProductVariant> Variants { get; init; } = [];
|
||||
}
|
||||
@@ -2,48 +2,8 @@ namespace Printbase.Domain.Entities.Products;
|
||||
|
||||
public class ProductGroup
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public string? Description { get; private set; }
|
||||
public IReadOnlyCollection<ProductType> Types => _types.AsReadOnly();
|
||||
|
||||
private readonly List<ProductType> _types = new();
|
||||
|
||||
private ProductGroup() { }
|
||||
|
||||
public ProductGroup(Guid id, string name, string? description = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("Group name cannot be empty", nameof(name));
|
||||
|
||||
Id = id;
|
||||
Name = name;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
public void AddType(ProductType type)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(type);
|
||||
|
||||
_types.Add(type);
|
||||
}
|
||||
|
||||
public void RemoveType(Guid typeId)
|
||||
{
|
||||
var type = _types.Find(t => t.Id == typeId);
|
||||
if (type != null) _types.Remove(type);
|
||||
}
|
||||
|
||||
public void UpdateName(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("Group name cannot be empty", nameof(name));
|
||||
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public void UpdateDescription(string? description)
|
||||
{
|
||||
Description = description;
|
||||
}
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public ICollection<ProductType> Types { get; set; } = [];
|
||||
}
|
||||
@@ -2,37 +2,10 @@ namespace Printbase.Domain.Entities.Products;
|
||||
|
||||
public class ProductType
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public string? Description { get; private set; }
|
||||
public Guid GroupId { get; private set; }
|
||||
|
||||
private ProductType() { }
|
||||
|
||||
public ProductType(Guid id, string name, Guid groupId, string? description = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("Type name cannot be empty", nameof(name));
|
||||
|
||||
if (groupId == Guid.Empty)
|
||||
throw new ArgumentException("Group ID cannot be empty", nameof(groupId));
|
||||
|
||||
Id = id;
|
||||
Name = name;
|
||||
GroupId = groupId;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
public void UpdateName(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("Type name cannot be empty", nameof(name));
|
||||
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public void UpdateDescription(string? description)
|
||||
{
|
||||
Description = description;
|
||||
}
|
||||
public Guid Id { get; init; }
|
||||
public string Name { get; init; } = string.Empty;
|
||||
public string? Description { get; init; }
|
||||
public Guid GroupId { get; init; }
|
||||
public ProductGroup Group { get; init; } = null!;
|
||||
public ICollection<Product>? Products { get; init; }
|
||||
}
|
||||
@@ -2,96 +2,12 @@ namespace Printbase.Domain.Entities.Products;
|
||||
|
||||
public class ProductVariant
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public Guid ProductId { get; private set; }
|
||||
public string? Color { get; private set; }
|
||||
public string? Size { get; private set; }
|
||||
public decimal Price { get; private set; }
|
||||
public decimal? Discount { get; private set; }
|
||||
public int Stock { get; private set; }
|
||||
|
||||
private ProductVariant() { }
|
||||
|
||||
public ProductVariant(Guid id, Guid productId, decimal price, string? color = null, string? size = null,
|
||||
decimal? discount = null, int stock = 0)
|
||||
{
|
||||
if (price < 0)
|
||||
throw new ArgumentException("Price cannot be negative", nameof(price));
|
||||
|
||||
Id = id;
|
||||
ProductId = productId;
|
||||
Color = color;
|
||||
Size = size;
|
||||
Price = price;
|
||||
Discount = discount;
|
||||
Stock = stock;
|
||||
}
|
||||
|
||||
public void UpdateColor(string? color)
|
||||
{
|
||||
Color = color;
|
||||
}
|
||||
|
||||
public void UpdateSize(string? size)
|
||||
{
|
||||
Size = size;
|
||||
}
|
||||
|
||||
public void UpdatePrice(decimal price)
|
||||
{
|
||||
if (price < 0) throw new ArgumentException("Price cannot be negative", nameof(price));
|
||||
|
||||
Price = price;
|
||||
}
|
||||
|
||||
public void UpdateDiscount(decimal? discount)
|
||||
{
|
||||
if (discount is < 0 or > 100)
|
||||
throw new ArgumentException("Discount must be between 0 and 100", nameof(discount));
|
||||
|
||||
Discount = discount;
|
||||
}
|
||||
|
||||
public void UpdateStock(int quantity)
|
||||
{
|
||||
if (quantity < 0)
|
||||
throw new ArgumentException("Stock quantity cannot be negative", nameof(quantity));
|
||||
|
||||
Stock = quantity;
|
||||
}
|
||||
|
||||
public void AddStock(int quantity)
|
||||
{
|
||||
if (quantity <= 0)
|
||||
throw new ArgumentException("Quantity to add must be positive", nameof(quantity));
|
||||
|
||||
Stock += quantity;
|
||||
}
|
||||
|
||||
public bool RemoveStock(int quantity)
|
||||
{
|
||||
if (quantity <= 0)
|
||||
throw new ArgumentException("Quantity to remove must be positive", nameof(quantity));
|
||||
|
||||
if (Stock < quantity) return false;
|
||||
|
||||
Stock -= quantity;
|
||||
return true;
|
||||
}
|
||||
|
||||
public decimal GetEffectivePrice(decimal? productDiscount = null)
|
||||
{
|
||||
var effectivePrice = Price;
|
||||
|
||||
var discountToApply = Discount ?? productDiscount;
|
||||
|
||||
if (discountToApply is > 0) effectivePrice -= (effectivePrice * discountToApply.Value / 100);
|
||||
|
||||
return Math.Round(effectivePrice, 2);
|
||||
}
|
||||
|
||||
public decimal? GetEffectiveDiscount(decimal? productDiscount = null)
|
||||
{
|
||||
return Discount ?? productDiscount;
|
||||
}
|
||||
public Guid Id { get; init; }
|
||||
public Guid ProductId { get; init; }
|
||||
public string? Color { get; init; }
|
||||
public string? Size { get; init; }
|
||||
public decimal Price { get; init; }
|
||||
public decimal? Discount { get; init; }
|
||||
public int Stock { get; init; }
|
||||
public Product Product { get; init; } = null!;
|
||||
}
|
||||
@@ -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>();
|
||||
CreateMap<ProductGroupDbEntity, ProductGroup>();
|
||||
CreateMap<ProductTypeDbEntity, ProductType>();
|
||||
CreateMap<ProductVariantDbEntity, ProductVariant>();
|
||||
|
||||
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<Product, ProductDbEntity>();
|
||||
CreateMap<ProductGroup, ProductGroupDbEntity>();
|
||||
CreateMap<ProductType, ProductTypeDbEntity>();
|
||||
CreateMap<ProductVariant, ProductVariantDbEntity>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user