BroadcastSession.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Domain.Entities.Members.ValueObject;
  4. namespace Domain.Entities.Members;
  5. /// <summary>
  6. /// 1회 방송 세션 (라이브 시작~종료) — YouTube 라이브 지표 수집 단위
  7. /// </summary>
  8. public sealed class BroadcastSession
  9. {
  10. [ForeignKey(nameof(ChannelID))]
  11. public Channel Channel { get; private set; } = null!;
  12. [Key]
  13. public int ID { get; private set; }
  14. public int ChannelID { get; private set; }
  15. public BroadcastPlatform Platform { get; private set; } = BroadcastPlatform.YouTube;
  16. public string VideoID { get; private set; } = default!;
  17. public string? Title { get; private set; }
  18. public DateTime? StartAt { get; private set; }
  19. public DateTime? EndAt { get; private set; }
  20. public int DurationSec { get; private set; }
  21. public long FinalViews { get; private set; }
  22. public long FinalLikes { get; private set; }
  23. public bool IsFinalized { get; private set; }
  24. public DateTime? UpdatedAt { get; private set; }
  25. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  26. private BroadcastSession() { }
  27. private BroadcastSession(int channelID, BroadcastPlatform platform, string videoID)
  28. {
  29. if (channelID <= 0)
  30. {
  31. throw new ArgumentOutOfRangeException(nameof(channelID));
  32. }
  33. if (string.IsNullOrWhiteSpace(videoID))
  34. {
  35. throw new ArgumentException("VideoID is required.", nameof(videoID));
  36. }
  37. ChannelID = channelID;
  38. Platform = platform;
  39. VideoID = videoID;
  40. }
  41. public static BroadcastSession Create(int channelID, BroadcastPlatform platform, string videoID)
  42. {
  43. return new(channelID, platform, videoID);
  44. }
  45. public void UpdateStats(
  46. string? title,
  47. DateTime? startAt,
  48. DateTime? endAt,
  49. long finalViews,
  50. long finalLikes
  51. ) {
  52. Title = title;
  53. StartAt = startAt;
  54. EndAt = endAt;
  55. if (startAt.HasValue && endAt.HasValue && endAt.Value > startAt.Value)
  56. {
  57. DurationSec = (int)(endAt.Value - startAt.Value).TotalSeconds;
  58. }
  59. FinalViews = finalViews;
  60. FinalLikes = finalLikes;
  61. UpdatedAt = DateTime.UtcNow;
  62. }
  63. public void MarkFinalized()
  64. {
  65. IsFinalized = true;
  66. UpdatedAt = DateTime.UtcNow;
  67. }
  68. }