littleshop/LittleShop/DTOs/ProductDto.cs
SysAdmin d9efababa6 Feature: Add product variant price override support
Enables individual variants to have their own prices, overriding the base product price.

**Database Changes:**
- Added Price (decimal?, nullable) to ProductVariants table
- Added ProductVariantId to OrderItems table with foreign key relationship
- Created index on OrderItems.ProductVariantId for performance

**API Changes:**
- ProductVariantDto: Added Price field
- CreateProductVariantDto: Added Price field with validation
- UpdateProductVariantDto: Added Price field
- OrderItemDto: Added ProductVariantId and ProductVariantName
- CreateOrderItemDto: Added ProductVariantId

**Business Logic:**
- OrderService: Variant price overrides base price (but multi-buy takes precedence)
- ProductService: All variant CRUD operations support Price field

**Admin UI:**
- CreateVariant: Price input with £ symbol and base price placeholder
- EditVariant: Price editing with £ symbol
- ProductVariants list: Shows variant price or "(base)" indicator

**Client Library:**
- Updated all DTOs to match server-side changes
- Full support for variant pricing in order creation

**Migration:**
- EF Core migration: 20251003173458_AddVariantPricing
- Backward compatible: NULL values supported for existing data

**Use Case:**
Products with size/color variants can now have different prices:
- Small T-shirt: £15.00 (variant override)
- Medium T-shirt: £18.00 (uses base price)
- Large T-shirt: £20.00 (variant override)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-03 18:35:43 +01:00

216 lines
5.9 KiB
C#

using System.ComponentModel.DataAnnotations;
using LittleShop.Enums;
namespace LittleShop.DTOs;
public class ProductDto
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public decimal Price { get; set; }
public decimal Weight { get; set; }
public ProductWeightUnit WeightUnit { get; set; }
public int StockQuantity { get; set; }
public Guid CategoryId { get; set; }
public string CategoryName { get; set; } = string.Empty;
public Guid? VariantCollectionId { get; set; }
public string? VariantsJson { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public bool IsActive { get; set; }
public List<ProductPhotoDto> Photos { get; set; } = new();
public List<ProductMultiBuyDto> MultiBuys { get; set; } = new();
public List<ProductVariantDto> Variants { get; set; } = new();
}
public class ProductPhotoDto
{
public Guid Id { get; set; }
public string FileName { get; set; } = string.Empty;
public string FilePath { get; set; } = string.Empty;
public string? AltText { get; set; }
public int SortOrder { get; set; }
}
public class CreateProductDto
{
[Required]
[StringLength(200)]
public string Name { get; set; } = string.Empty;
[Required(AllowEmptyStrings = true)]
public string Description { get; set; } = string.Empty;
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Price must be greater than 0")]
public decimal Price { get; set; }
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Weight must be greater than 0")]
public decimal Weight { get; set; }
public ProductWeightUnit WeightUnit { get; set; } = ProductWeightUnit.Grams;
[Range(0, int.MaxValue)]
public int StockQuantity { get; set; } = 0;
[Required(ErrorMessage = "Please select a category")]
public Guid CategoryId { get; set; }
public Guid? VariantCollectionId { get; set; }
public string? VariantsJson { get; set; }
}
public class UpdateProductDto
{
[StringLength(200)]
public string? Name { get; set; }
public string? Description { get; set; }
[Range(0.01, double.MaxValue)]
public decimal? Price { get; set; }
public decimal? Weight { get; set; }
public ProductWeightUnit? WeightUnit { get; set; }
[Range(0, int.MaxValue)]
public int? StockQuantity { get; set; }
public Guid? CategoryId { get; set; }
public Guid? VariantCollectionId { get; set; }
public string? VariantsJson { get; set; }
public bool? IsActive { get; set; }
}
public class CreateProductPhotoDto
{
[Required]
public Guid ProductId { get; set; }
[Required]
public string PhotoUrl { get; set; } = string.Empty;
public string? AltText { get; set; }
public int DisplayOrder { get; set; }
}
public class ProductMultiBuyDto
{
public Guid Id { get; set; }
public Guid ProductId { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal PricePerUnit { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class ProductVariantDto
{
public Guid Id { get; set; }
public Guid ProductId { get; set; }
public string Name { get; set; } = string.Empty;
public string VariantType { get; set; } = "Standard";
public int SortOrder { get; set; }
public int StockLevel { get; set; }
public decimal? Price { get; set; } // If null, uses product.Price
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class CreateProductMultiBuyDto
{
[Required]
public Guid ProductId { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
[Required]
[Range(1, int.MaxValue, ErrorMessage = "Quantity must be at least 1")]
public int Quantity { get; set; }
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Price must be greater than 0")]
public decimal Price { get; set; }
[Range(0, int.MaxValue)]
public int SortOrder { get; set; }
}
public class CreateProductVariantDto
{
[Required]
public Guid ProductId { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; } = string.Empty;
[StringLength(50)]
public string VariantType { get; set; } = "Standard";
[Range(0, int.MaxValue)]
public int SortOrder { get; set; }
[Range(0, int.MaxValue)]
public int StockLevel { get; set; } = 0;
[Range(0.01, double.MaxValue, ErrorMessage = "Price must be greater than 0")]
public decimal? Price { get; set; } // Optional: If null, uses product.Price
}
public class UpdateProductMultiBuyDto
{
[StringLength(100)]
public string? Name { get; set; }
public string? Description { get; set; }
[Range(1, int.MaxValue)]
public int? Quantity { get; set; }
[Range(0.01, double.MaxValue)]
public decimal? Price { get; set; }
[Range(0, int.MaxValue)]
public int? SortOrder { get; set; }
public bool? IsActive { get; set; }
}
public class UpdateProductVariantDto
{
[StringLength(100)]
public string? Name { get; set; }
[StringLength(50)]
public string? VariantType { get; set; }
[Range(0, int.MaxValue)]
public int? SortOrder { get; set; }
[Range(0, int.MaxValue)]
public int? StockLevel { get; set; }
[Range(0.01, double.MaxValue, ErrorMessage = "Price must be greater than 0")]
public decimal? Price { get; set; } // Optional: If null, uses product.Price
public bool? IsActive { get; set; }
}