OrderItem.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. namespace Domain.Entities.Store;
  4. /// <summary>
  5. /// 주문 라인. Order N:1 + Product N:1.
  6. /// Digital 상품인 경우 IssuedCouponCodeID 로 발급된 쿠폰 코드 추적.
  7. /// </summary>
  8. public class OrderItem
  9. {
  10. [ForeignKey(nameof(OrderID))]
  11. public virtual Order? Order { get; private set; }
  12. [ForeignKey(nameof(ProductID))]
  13. public virtual Product? Product { get; private set; }
  14. [ForeignKey(nameof(IssuedCouponCodeID))]
  15. public virtual CouponCode? IssuedCouponCode { get; private set; }
  16. [Key]
  17. public int ID { get; private set; }
  18. public int OrderID { get; private set; }
  19. public int ProductID { get; private set; }
  20. public int Quantity { get; private set; }
  21. /// <summary>주문 시점 상품 단가 (KRW). Product.Price 변경되어도 보존.</summary>
  22. public int UnitPrice { get; private set; }
  23. /// <summary>Digital 상품 발급 쿠폰 코드 (Physical 상품은 null).</summary>
  24. public int? IssuedCouponCodeID { get; private set; }
  25. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  26. private OrderItem() { }
  27. public static OrderItem Create(int productID, int quantity, int unitPrice)
  28. {
  29. if (productID <= 0)
  30. {
  31. throw new ArgumentOutOfRangeException(nameof(productID));
  32. }
  33. if (quantity <= 0)
  34. {
  35. throw new ArgumentOutOfRangeException(nameof(quantity), "Quantity must be positive.");
  36. }
  37. if (unitPrice < 0)
  38. {
  39. throw new ArgumentOutOfRangeException(nameof(unitPrice), "UnitPrice must be non-negative.");
  40. }
  41. return new OrderItem
  42. {
  43. ProductID = productID,
  44. Quantity = quantity,
  45. UnitPrice = unitPrice
  46. };
  47. }
  48. public void AssignCouponCode(int couponCodeID)
  49. {
  50. if (couponCodeID <= 0)
  51. {
  52. throw new ArgumentOutOfRangeException(nameof(couponCodeID));
  53. }
  54. IssuedCouponCodeID = couponCodeID;
  55. }
  56. }