Channel.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. namespace Domain.Entities.Members
  4. {
  5. public class Channel
  6. {
  7. [ForeignKey(nameof(MemberID))]
  8. public virtual Member Member { get; private set; } = null!;
  9. [Key]
  10. public int ID { get; private set; }
  11. public int MemberID { get; private set; }
  12. public string SID { get; private set; } = default!;
  13. public string Name { get; private set; } = default!;
  14. public string? Handle { get; private set; }
  15. public string YouTubeUrl { get; private set; } = default!;
  16. public string? YouTubeChannelID { get; private set; }
  17. public string? Description { get; private set; }
  18. public string? ThumbnailUrl { get; private set; }
  19. public string? BannerUrl { get; private set; }
  20. public long SubscriberCount { get; private set; }
  21. public long VideoCount { get; private set; }
  22. public long ViewCount { get; private set; }
  23. public string? Email { get; private set; }
  24. public DateTime? YouTubePublishedAt { get; private set; }
  25. public decimal PlatformFeeRate { get; private set; } = 0;
  26. public bool IsVerified { get; private set; } = false;
  27. public bool IsActive { get; private set; } = true;
  28. public string WidgetToken { get; private set; } = GenerateWidgetToken();
  29. public DateTime? UpdatedAt { get; private set; }
  30. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  31. private Channel() { }
  32. private Channel(int memberID, string sid, string name, string youtubeUrl)
  33. {
  34. if (memberID <= 0)
  35. {
  36. throw new ArgumentOutOfRangeException(nameof(memberID));
  37. }
  38. if (string.IsNullOrWhiteSpace(sid))
  39. {
  40. throw new ArgumentException("SID is required.", nameof(sid));
  41. }
  42. if (sid.Length != 24)
  43. {
  44. throw new ArgumentOutOfRangeException(nameof(sid));
  45. }
  46. if (string.IsNullOrWhiteSpace(name))
  47. {
  48. throw new ArgumentException("Name is required.", nameof(name));
  49. }
  50. if (name.Length > 200)
  51. {
  52. throw new ArgumentOutOfRangeException(nameof(name));
  53. }
  54. if (string.IsNullOrWhiteSpace(youtubeUrl))
  55. {
  56. throw new ArgumentException("YouTubeUrl is required.", nameof(youtubeUrl));
  57. }
  58. if (youtubeUrl.Length > 255)
  59. {
  60. throw new ArgumentOutOfRangeException(nameof(youtubeUrl));
  61. }
  62. MemberID = memberID;
  63. SID = sid;
  64. Name = name;
  65. YouTubeUrl = youtubeUrl;
  66. }
  67. public static Channel Create(int memberID, string sid, string name, string youtubeUrl)
  68. {
  69. return new(memberID, sid, name, youtubeUrl);
  70. }
  71. public void Update(string name, string? handle, string youtubeUrl, decimal platformFeeRate, bool isVerified, bool isActive)
  72. {
  73. if (string.IsNullOrWhiteSpace(name))
  74. {
  75. throw new ArgumentException("Name is required.", nameof(name));
  76. }
  77. if (name.Length > 200)
  78. {
  79. throw new ArgumentOutOfRangeException(nameof(name));
  80. }
  81. if (handle is not null && handle.Length > 30)
  82. {
  83. throw new ArgumentOutOfRangeException(nameof(handle));
  84. }
  85. if (string.IsNullOrWhiteSpace(youtubeUrl))
  86. {
  87. throw new ArgumentException("YouTubeUrl is required.", nameof(youtubeUrl));
  88. }
  89. if (youtubeUrl.Length > 255)
  90. {
  91. throw new ArgumentOutOfRangeException(nameof(youtubeUrl));
  92. }
  93. if (platformFeeRate < 0 || platformFeeRate > 100)
  94. {
  95. throw new ArgumentOutOfRangeException(nameof(platformFeeRate));
  96. }
  97. Name = name;
  98. Handle = handle;
  99. YouTubeUrl = youtubeUrl;
  100. PlatformFeeRate = platformFeeRate;
  101. IsVerified = isVerified;
  102. IsActive = isActive;
  103. UpdatedAt = DateTime.UtcNow;
  104. }
  105. public void UpdateYouTubeInfo(
  106. string youtubeChannelID,
  107. string name,
  108. string? handle,
  109. string? description,
  110. string? thumbnailUrl,
  111. string? bannerUrl,
  112. long subscriberCount,
  113. long videoCount,
  114. long viewCount,
  115. string? email,
  116. DateTime? youtubePublishedAt
  117. ) {
  118. YouTubeChannelID = youtubeChannelID;
  119. Name = name;
  120. Handle = handle;
  121. Description = description;
  122. ThumbnailUrl = thumbnailUrl;
  123. BannerUrl = bannerUrl;
  124. SubscriberCount = subscriberCount;
  125. VideoCount = videoCount;
  126. ViewCount = viewCount;
  127. Email = email;
  128. IsActive = true;
  129. YouTubePublishedAt = youtubePublishedAt;
  130. UpdatedAt = DateTime.UtcNow;
  131. }
  132. public void Deactivate()
  133. {
  134. IsActive = false;
  135. UpdatedAt = DateTime.UtcNow;
  136. }
  137. public void ResetWidgetToken()
  138. {
  139. WidgetToken = GenerateWidgetToken();
  140. UpdatedAt = DateTime.UtcNow;
  141. }
  142. private static string GenerateWidgetToken()
  143. {
  144. return Guid.NewGuid().ToString("N");
  145. }
  146. }
  147. }