using ErsatzTV.Core.Domain; using ErsatzTV.Infrastructure.Data; using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Application.Libraries; public class GetExternalCollectionsHandler : IRequestHandler> { private readonly IDbContextFactory _dbContextFactory; public GetExternalCollectionsHandler(IDbContextFactory dbContextFactory) => _dbContextFactory = dbContextFactory; public async Task> Handle( GetExternalCollections request, CancellationToken cancellationToken) { List result = new(); await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); result.AddRange(await GetEmbyExternalCollections(dbContext, cancellationToken)); result.AddRange(await GetJellyfinExternalCollections(dbContext, cancellationToken)); result.AddRange(await GetPlexExternalCollections(dbContext, cancellationToken)); return result; } private static async Task> GetEmbyExternalCollections( TvContext dbContext, CancellationToken cancellationToken) { List embyMediaSourceIds = await dbContext.EmbyMediaSources .Filter(ems => ems.Libraries.Any(l => ((EmbyLibrary)l).ShouldSyncItems)) .Map(ems => ems.Id) .ToListAsync(cancellationToken); return embyMediaSourceIds.Map(id => new LibraryViewModel("Emby", 0, "Collections", 0, id, string.Empty)); } private static async Task> GetJellyfinExternalCollections( TvContext dbContext, CancellationToken cancellationToken) { List jellyfinMediaSourceIds = await dbContext.JellyfinMediaSources .Filter(jms => jms.Libraries.Any(l => ((JellyfinLibrary)l).ShouldSyncItems)) .Map(jms => jms.Id) .ToListAsync(cancellationToken); return jellyfinMediaSourceIds.Map( id => new LibraryViewModel("Jellyfin", 0, "Collections", 0, id, string.Empty)); } private static async Task> GetPlexExternalCollections( TvContext dbContext, CancellationToken cancellationToken) { List plexMediaSourceIds = await dbContext.PlexMediaSources .Filter(pms => pms.Libraries.Any(l => ((PlexLibrary)l).ShouldSyncItems)) .Map(pms => pms.Id) .ToListAsync(cancellationToken); return plexMediaSourceIds.Map( id => new LibraryViewModel("Plex", 0, "Collections", 0, id, string.Empty)); } }