View.cshtml.cs 3.3 KB

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