using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Application.Abstractions.YouTube; using Microsoft.EntityFrameworkCore; namespace Application.Features.Admin.Channel.YouTubePubSub.GetLiveStatus; internal sealed class Handler( IAppDbContext db, IYouTubeLiveStateStore liveStateStore ) : IQueryHandler { public async Task Handle(Query request, CancellationToken ct) { var liveInfos = await liveStateStore.GetAllLiveAsync(); if (liveInfos.Count == 0) { return new Response([]); } var ytIds = liveInfos.Select(x => x.ChannelId).ToList(); var channelNames = await db.Channel.AsNoTracking() .Where(c => c.YouTubeChannelID != null && ytIds.Contains(c.YouTubeChannelID)) .Select(c => new { c.YouTubeChannelID, c.Name }) .ToDictionaryAsync(x => x.YouTubeChannelID!, x => x.Name, ct); var items = liveInfos.Select(info => new LiveItem( info.ChannelId, channelNames.TryGetValue(info.ChannelId, out var name) ? name : null, info.VideoId, info.Title, info.ActualStartTime )).ToList(); return new Response(items); } }