Add base product DbEntities and domain Entities

This commit is contained in:
lumijiez
2025-05-04 00:53:16 +03:00
parent 6e98b7dc22
commit 7fd2af2d5a
13 changed files with 379 additions and 15 deletions

View File

@@ -0,0 +1,80 @@
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;
}
}

View File

@@ -0,0 +1,49 @@
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;
}
}

View File

@@ -0,0 +1,38 @@
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;
}
}

View File

@@ -0,0 +1,97 @@
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;
}
}