using System.Collections.Generic; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; using ErsatzTV.Core; using ErsatzTV.Core.Domain; using ErsatzTV.Core.Interfaces.Repositories; using LanguageExt; using MediatR; namespace ErsatzTV.Application.Jellyfin.Commands { public class SynchronizeJellyfinMediaSourcesHandler : IRequestHandler>> { private readonly ChannelWriter _channel; private readonly IMediaSourceRepository _mediaSourceRepository; public SynchronizeJellyfinMediaSourcesHandler( IMediaSourceRepository mediaSourceRepository, ChannelWriter channel) { _mediaSourceRepository = mediaSourceRepository; _channel = channel; } public async Task>> Handle( SynchronizeJellyfinMediaSources request, CancellationToken cancellationToken) { List mediaSources = await _mediaSourceRepository.GetAllJellyfin(); foreach (JellyfinMediaSource mediaSource in mediaSources) { await _channel.WriteAsync(new SynchronizeJellyfinAdminUserId(mediaSource.Id), cancellationToken); await _channel.WriteAsync(new SynchronizeJellyfinLibraries(mediaSource.Id), cancellationToken); } return mediaSources; } } }