View.cshtml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Application.Abstractions.YouTube;
  2. using SharedKernel.Extensions;
  3. using MediatR;
  4. using Microsoft.AspNetCore.Mvc.RazorPages;
  5. namespace Admin.Pages.Channel.List;
  6. public class ViewModel(IMediator mediator, IYouTubeApiService youTubeApi, IYouTubeLiveStateStore liveStateStore, IYouTubeChannelCache channelCache) : PageModel
  7. {
  8. // ── DB 데이터 ────────────────────────────────────────────────
  9. public int ID { get; set; }
  10. public int MemberID { get; set; }
  11. public string? MemberInfo { get; set; }
  12. public string SID { get; set; } = default!;
  13. public string Name { get; set; } = default!;
  14. public string? Handle { get; set; }
  15. public string YouTubeUrl { get; set; } = default!;
  16. public decimal PlatformFeeRate { get; set; }
  17. public bool IsVerified { get; set; }
  18. public bool IsActive { get; set; }
  19. public string? UpdatedAt { get; set; }
  20. public string CreatedAt { get; set; } = default!;
  21. // ── YouTube API 데이터 ───────────────────────────────────────
  22. public YouTubeChannelInfo? YouTubeChannel { get; set; }
  23. public bool YouTubeApiFailed { get; set; }
  24. public string? YouTubeApiError { get; set; }
  25. // ── 라이브 상태 (PubSub 기반 — Redis 조회만, API 호출 없음) ──
  26. public YouTubeLiveStreamInfo? LiveStream { get; set; }
  27. public async Task OnGetAsync(int id, CancellationToken ct)
  28. {
  29. var result = await mediator.Send(new GetChannel.Query(id), ct);
  30. if (result is null)
  31. {
  32. return;
  33. }
  34. ID = result.ID;
  35. MemberID = result.MemberID;
  36. MemberInfo = $"[{result.MemberID}] {result.MemberEmail}, {result.MemberName ?? result.MemberSID ?? "-"}";
  37. SID = result.SID;
  38. Name = result.Name;
  39. Handle = result.Handle;
  40. YouTubeUrl = result.YouTubeUrl;
  41. PlatformFeeRate = result.PlatformFeeRate;
  42. IsVerified = result.IsVerified;
  43. IsActive = result.IsActive;
  44. UpdatedAt = result.UpdatedAt.GetDateAt();
  45. CreatedAt = result.CreatedAt.GetDateAt();
  46. // SID = YouTube 채널 ID → 채널 정보 조회 (1 unit)
  47. await FetchYouTubeChannelAsync(result.SID, ct);
  48. // 라이브 상태는 Redis에서만 조회 (API 호출 없음, 0 unit)
  49. LiveStream = await liveStateStore.GetLiveAsync(result.SID);
  50. }
  51. private async Task FetchYouTubeChannelAsync(string channelId, CancellationToken ct)
  52. {
  53. try
  54. {
  55. YouTubeChannel = await youTubeApi.GetChannelByIdAsync(channelId, ct);
  56. // YouTube API 조회 성공 시 Redis 캐시에 저장 (24시간 TTL)
  57. if (YouTubeChannel is not null)
  58. {
  59. await channelCache.SetAsync(YouTubeChannel);
  60. }
  61. if (YouTubeChannel is null)
  62. {
  63. YouTubeApiFailed = true;
  64. YouTubeApiError = $"채널 정보를 찾을 수 없습니다. 조회한 SID: \"{channelId}\" — DB Config의 YouTube API Key 설정을 확인하세요.";
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. YouTubeApiFailed = true;
  70. YouTubeApiError = $"YouTube API 조회 실패: {ex.Message}";
  71. }
  72. }
  73. }