* add chapter statistics and new filler options * refactor playout builder * more refactor prep for filler * rewrite schedulers * refactor collectionkey * add tail filler kind * migrate tail filler to filler preset * optionally show filler * fix playout detail row count * remove duration tail filler options * implement tail and fallback in flood scheduler * implement tail and fallback in one scheduler * implement tail and fallback in multiple scheduler * implement looping fallback filler * more duration tests * start to add post-roll filler to flood * rework playoutitem filler tagging * rework scheduler logging * calculate whether configured filler will fit * implement pre-roll and post-roll duration and count filler * improve duration filler calculation * add minutes to search index * update channel guide to work with new filler * add mid-roll filler * don't clone enumerators for filler calculations * support pre-roll and post-roll pad filler * implement mid-roll pad filler * allow clearing filler selections in schedule editor * fix tests * filler config validation * use consistent time zone for tests
140 lines
5.8 KiB
C#
140 lines
5.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Channels;
|
|
using System.Threading.Tasks;
|
|
using ErsatzTV.Application.Playouts.Commands;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using LanguageExt;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using static ErsatzTV.Application.ProgramSchedules.Mapper;
|
|
using static LanguageExt.Prelude;
|
|
|
|
namespace ErsatzTV.Application.ProgramSchedules.Commands
|
|
{
|
|
public class ReplaceProgramScheduleItemsHandler : ProgramScheduleItemCommandBase,
|
|
IRequestHandler<ReplaceProgramScheduleItems, Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>>>
|
|
{
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
|
|
|
|
public ReplaceProgramScheduleItemsHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> channel)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_channel = channel;
|
|
}
|
|
|
|
public async Task<Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>>> Handle(
|
|
ReplaceProgramScheduleItems request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
|
|
Validation<BaseError, ProgramSchedule> validation = await Validate(dbContext, request);
|
|
return await validation.Apply(ps => PersistItems(dbContext, request, ps));
|
|
}
|
|
|
|
private async Task<IEnumerable<ProgramScheduleItemViewModel>> PersistItems(
|
|
TvContext dbContext,
|
|
ReplaceProgramScheduleItems request,
|
|
ProgramSchedule programSchedule)
|
|
{
|
|
dbContext.RemoveRange(programSchedule.Items);
|
|
programSchedule.Items = request.Items.Map(i => BuildItem(programSchedule, i.Index, i)).ToList();
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
// rebuild any playouts that use this schedule
|
|
foreach (Playout playout in programSchedule.Playouts)
|
|
{
|
|
await _channel.WriteAsync(new BuildPlayout(playout.Id, true));
|
|
}
|
|
|
|
return programSchedule.Items.Map(ProjectToViewModel);
|
|
}
|
|
|
|
private Task<Validation<BaseError, ProgramSchedule>> Validate(
|
|
TvContext dbContext,
|
|
ReplaceProgramScheduleItems request) =>
|
|
ProgramScheduleMustExist(dbContext, request.ProgramScheduleId)
|
|
.BindT(programSchedule => PlayoutModesMustBeValid(request, programSchedule))
|
|
.BindT(programSchedule => CollectionTypesMustBeValid(request, programSchedule))
|
|
.BindT(programSchedule => PlaybackOrdersMustBeValid(request, programSchedule))
|
|
.BindT(programSchedule => FillerConfigurationsMustBeValid(dbContext, request, programSchedule));
|
|
|
|
private static Validation<BaseError, ProgramSchedule> PlayoutModesMustBeValid(
|
|
ReplaceProgramScheduleItems request,
|
|
ProgramSchedule programSchedule) =>
|
|
request.Items.Map(item => PlayoutModeMustBeValid(item, programSchedule)).Sequence()
|
|
.Map(_ => programSchedule);
|
|
|
|
private Validation<BaseError, ProgramSchedule> CollectionTypesMustBeValid(
|
|
ReplaceProgramScheduleItems request,
|
|
ProgramSchedule programSchedule) =>
|
|
request.Items.Map(item => CollectionTypeMustBeValid(item, programSchedule)).Sequence()
|
|
.Map(_ => programSchedule);
|
|
|
|
private static async Task<Validation<BaseError, ProgramSchedule>> FillerConfigurationsMustBeValid(
|
|
TvContext dbContext,
|
|
ReplaceProgramScheduleItems request,
|
|
ProgramSchedule programSchedule)
|
|
{
|
|
foreach (ReplaceProgramScheduleItem item in request.Items)
|
|
{
|
|
Either<BaseError, ProgramSchedule> result = await FillerConfigurationMustBeValid(
|
|
dbContext,
|
|
item,
|
|
programSchedule);
|
|
if (result.IsLeft)
|
|
{
|
|
return result.ToValidation();
|
|
}
|
|
}
|
|
|
|
return programSchedule;
|
|
}
|
|
|
|
private static Validation<BaseError, ProgramSchedule> PlaybackOrdersMustBeValid(
|
|
ReplaceProgramScheduleItems request,
|
|
ProgramSchedule programSchedule)
|
|
{
|
|
var keyOrders = new Dictionary<CollectionKey, System.Collections.Generic.HashSet<PlaybackOrder>>();
|
|
foreach (ReplaceProgramScheduleItem item in request.Items)
|
|
{
|
|
var key = new CollectionKey(
|
|
item.CollectionType,
|
|
item.CollectionId,
|
|
item.MediaItemId,
|
|
item.MultiCollectionId,
|
|
item.SmartCollectionId);
|
|
|
|
if (keyOrders.TryGetValue(key, out System.Collections.Generic.HashSet<PlaybackOrder> playbackOrders))
|
|
{
|
|
playbackOrders.Add(item.PlaybackOrder);
|
|
keyOrders[key] = playbackOrders;
|
|
}
|
|
else
|
|
{
|
|
keyOrders.Add(key, new System.Collections.Generic.HashSet<PlaybackOrder> { item.PlaybackOrder });
|
|
}
|
|
}
|
|
|
|
return Optional(keyOrders.Values.Count(set => set.Count != 1))
|
|
.Filter(count => count == 0)
|
|
.Map(_ => programSchedule)
|
|
.ToValidation<BaseError>("A collection must not use multiple playback orders");
|
|
}
|
|
|
|
private record CollectionKey(
|
|
ProgramScheduleItemCollectionType CollectionType,
|
|
int? CollectionId,
|
|
int? MediaItemId,
|
|
int? MultiCollectionId,
|
|
int? SmartCollectionId);
|
|
}
|
|
}
|