diff --git a/src/Printbase.Application/ProductGroup/ProductGroupDto.cs b/src/Printbase.Application/ProductGroup/ProductGroupDto.cs deleted file mode 100644 index 96d9bab..0000000 --- a/src/Printbase.Application/ProductGroup/ProductGroupDto.cs +++ /dev/null @@ -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 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; } -} \ No newline at end of file diff --git a/src/Printbase.Application/ProductType/ProductTypeDto.cs b/src/Printbase.Application/ProductType/ProductTypeDto.cs deleted file mode 100644 index ae5ee58..0000000 --- a/src/Printbase.Application/ProductType/ProductTypeDto.cs +++ /dev/null @@ -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; } -} \ No newline at end of file diff --git a/src/Printbase.Application/Products/ProductDto.cs b/src/Printbase.Application/Products/ProductDto.cs deleted file mode 100644 index ad2fd75..0000000 --- a/src/Printbase.Application/Products/ProductDto.cs +++ /dev/null @@ -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 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 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; } -} \ No newline at end of file diff --git a/src/Printbase.Domain/Entities/Products/Product.cs b/src/Printbase.Domain/Entities/Products/Product.cs index bd7dbdf..1fe8d03 100644 --- a/src/Printbase.Domain/Entities/Products/Product.cs +++ b/src/Printbase.Domain/Entities/Products/Product.cs @@ -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 Variants => _variants.AsReadOnly(); - - private readonly List _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 Variants { get; init; } = []; } \ No newline at end of file diff --git a/src/Printbase.Domain/Entities/Products/ProductGroup.cs b/src/Printbase.Domain/Entities/Products/ProductGroup.cs index 8241e7a..a385996 100644 --- a/src/Printbase.Domain/Entities/Products/ProductGroup.cs +++ b/src/Printbase.Domain/Entities/Products/ProductGroup.cs @@ -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 Types => _types.AsReadOnly(); - - private readonly List _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 Types { get; set; } = []; } \ No newline at end of file diff --git a/src/Printbase.Domain/Entities/Products/ProductType.cs b/src/Printbase.Domain/Entities/Products/ProductType.cs index 1658608..d382195 100644 --- a/src/Printbase.Domain/Entities/Products/ProductType.cs +++ b/src/Printbase.Domain/Entities/Products/ProductType.cs @@ -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? Products { get; init; } } \ No newline at end of file diff --git a/src/Printbase.Domain/Entities/Products/ProductVariant.cs b/src/Printbase.Domain/Entities/Products/ProductVariant.cs index 078f16e..cf56254 100644 --- a/src/Printbase.Domain/Entities/Products/ProductVariant.cs +++ b/src/Printbase.Domain/Entities/Products/ProductVariant.cs @@ -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!; } \ No newline at end of file diff --git a/src/Printbase.Infrastructure/Database/ApplicationDbContext.cs b/src/Printbase.Infrastructure/Database/ApplicationDbContext.cs index fc1f5b7..0012c8a 100644 --- a/src/Printbase.Infrastructure/Database/ApplicationDbContext.cs +++ b/src/Printbase.Infrastructure/Database/ApplicationDbContext.cs @@ -32,10 +32,6 @@ public class ApplicationDbContext(DbContextOptions options .HasForeignKey(t => t.GroupId) .OnDelete(DeleteBehavior.Cascade); - modelBuilder.Entity() - .Property(p => p.Discount) - .HasPrecision(18, 2); - modelBuilder.Entity() .Property(v => v.Price) .HasPrecision(18, 2); diff --git a/src/Printbase.Infrastructure/DbEntities/Products/ProductDbEntity.cs b/src/Printbase.Infrastructure/DbEntities/Products/ProductDbEntity.cs index a0d870b..e0b0c64 100644 --- a/src/Printbase.Infrastructure/DbEntities/Products/ProductDbEntity.cs +++ b/src/Printbase.Infrastructure/DbEntities/Products/ProductDbEntity.cs @@ -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 Variants { get; set; } + public required ICollection Variants { get; init; } = []; } \ No newline at end of file diff --git a/src/Printbase.Infrastructure/DbEntities/Products/ProductTypeDbEntity.cs b/src/Printbase.Infrastructure/DbEntities/Products/ProductTypeDbEntity.cs index 127b334..646f45d 100644 --- a/src/Printbase.Infrastructure/DbEntities/Products/ProductTypeDbEntity.cs +++ b/src/Printbase.Infrastructure/DbEntities/Products/ProductTypeDbEntity.cs @@ -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? Products { get; set; } + public ICollection? Products { get; init; } } \ No newline at end of file diff --git a/src/Printbase.Infrastructure/DbEntities/Products/ProductVariantDbEntity.cs b/src/Printbase.Infrastructure/DbEntities/Products/ProductVariantDbEntity.cs index e158e3a..ab10212 100644 --- a/src/Printbase.Infrastructure/DbEntities/Products/ProductVariantDbEntity.cs +++ b/src/Printbase.Infrastructure/DbEntities/Products/ProductVariantDbEntity.cs @@ -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; } } \ No newline at end of file diff --git a/src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs b/src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs index 3b521af..cc1c52c 100644 --- a/src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs +++ b/src/Printbase.Infrastructure/Mapping/ProductMappingProfile.cs @@ -8,67 +8,14 @@ public class ProductMappingProfile : Profile { public ProductMappingProfile() { - CreateMap() - .ForMember(dest => dest.Variants, opt => opt.MapFrom(src => src.Variants)) - .ForMember(dest => dest.Type, opt => opt.Ignore()); - - CreateMap() - .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(dbVariant); - dest.AddVariant(variant); - } - }); - - CreateMap() - .ForMember(dest => dest.Product, opt => opt.Ignore()); - - CreateMap() - .ConstructUsing((src, _) => new ProductVariant( - src.Id, - src.ProductId, - src.Price, - src.Color, - src.Size, - src.Discount, - src.Stock)); - - CreateMap() - .ForMember(dest => dest.Group, opt => opt.Ignore()) - .ForMember(dest => dest.Products, opt => opt.Ignore()); - - CreateMap() - .ConstructUsing((src, _) => new ProductType( - src.Id, - src.Name, - src.GroupId, - src.Description)); - - CreateMap() - .ForMember(dest => dest.Types, opt => opt.MapFrom(src => src.Types)); - - CreateMap() - .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(dbType); - dest.AddType(type); - } - }); + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); } } \ No newline at end of file