using ErsatzTV.Core.Domain; using ErsatzTV.Infrastructure.Data; using ErsatzTV.Infrastructure.Extensions; using Microsoft.EntityFrameworkCore; using static ErsatzTV.Application.ProgramSchedules.Mapper; namespace ErsatzTV.Application.ProgramSchedules; public class GetProgramScheduleItemsHandler(IDbContextFactory dbContextFactory) : IRequestHandler> { public async Task> Handle( GetProgramScheduleItems request, CancellationToken cancellationToken) { await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); Option maybeProgramSchedule = await dbContext.ProgramSchedules.SelectOneAsync(ps => ps.Id, ps => ps.Id == request.Id, cancellationToken); return await dbContext.ProgramScheduleItems .Filter(psi => psi.ProgramScheduleId == request.Id) .IncludeScheduleItemDetails() .OrderBy(i => i.Index) .ToListAsync(cancellationToken) .Map(programScheduleItems => programScheduleItems.Map(ProjectToViewModel) .Map(psi => EnforceProperties(maybeProgramSchedule, psi)).ToList()); } // shuffled schedule items supports a limited set of property values private static ProgramScheduleItemViewModel EnforceProperties( Option maybeProgramSchedule, ProgramScheduleItemViewModel item) { foreach (ProgramSchedule programSchedule in maybeProgramSchedule) { if (programSchedule.ShuffleScheduleItems) { item = item with { StartType = StartType.Dynamic }; if (item.PlayoutMode == PlayoutMode.Flood) { item = item with { PlayoutMode = PlayoutMode.One }; } } if (item.PlaybackOrder is PlaybackOrder.ShuffleInOrder) { item = item with { FillWithGroupMode = FillWithGroupMode.None }; } if (item.CollectionType is CollectionType.Playlist or CollectionType.RerunFirstRun or CollectionType.RerunRerun) { item = item with { PlaybackOrder = PlaybackOrder.None }; } } return item; } }