Handler.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Application.Abstractions.YouTube;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.Channel.YouTubePubSub.GetLiveStatus;
  6. internal sealed class Handler(
  7. IAppDbContext db,
  8. IYouTubeLiveStateStore liveStateStore
  9. ) : IQueryHandler<Query, Response>
  10. {
  11. public async Task<Response> Handle(Query request, CancellationToken ct)
  12. {
  13. var liveInfos = await liveStateStore.GetAllLiveAsync();
  14. if (liveInfos.Count == 0)
  15. {
  16. return new Response([]);
  17. }
  18. var ytIds = liveInfos.Select(x => x.ChannelId).ToList();
  19. var channelNames = await db.Channel.AsNoTracking()
  20. .Where(c => c.YouTubeChannelID != null && ytIds.Contains(c.YouTubeChannelID))
  21. .Select(c => new { c.YouTubeChannelID, c.Name })
  22. .ToDictionaryAsync(x => x.YouTubeChannelID!, x => x.Name, ct);
  23. var items = liveInfos.Select(info => new LiveItem(
  24. info.ChannelId,
  25. channelNames.TryGetValue(info.ChannelId, out var name) ? name : null,
  26. info.VideoId,
  27. info.Title,
  28. info.ActualStartTime
  29. )).ToList();
  30. return new Response(items);
  31. }
  32. }