clear block playout items without clearing history (#1581)
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
namespace ErsatzTV.Application.Scheduling;
|
||||
|
||||
public record EraseBlockPlayoutItems(int PlayoutId) : IRequest;
|
||||
@@ -0,0 +1,50 @@
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Core.Domain.Scheduling;
|
||||
using ErsatzTV.Infrastructure.Data;
|
||||
using ErsatzTV.Infrastructure.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ErsatzTV.Application.Scheduling;
|
||||
|
||||
public class EraseBlockPlayoutItemsHandler(IDbContextFactory<TvContext> dbContextFactory)
|
||||
: IRequestHandler<EraseBlockPlayoutItems>
|
||||
{
|
||||
public async Task Handle(EraseBlockPlayoutItems request, CancellationToken cancellationToken)
|
||||
{
|
||||
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
Option<Playout> maybePlayout = await dbContext.Playouts
|
||||
.Include(p => p.Items)
|
||||
.Include(p => p.PlayoutHistory)
|
||||
.Filter(p => p.ProgramSchedulePlayoutType == ProgramSchedulePlayoutType.Block)
|
||||
.SelectOneAsync(p => p.Id, p => p.Id == request.PlayoutId);
|
||||
|
||||
foreach (Playout playout in maybePlayout)
|
||||
{
|
||||
// find the earliest item that finishes after "now"
|
||||
Option<PlayoutItem> maybeFirstItem = playout.Items
|
||||
.Filter(i => i.FinishOffset > DateTimeOffset.Now)
|
||||
.OrderBy(i => i.StartOffset)
|
||||
.HeadOrNone();
|
||||
|
||||
// delete all history starting with that item
|
||||
// importantly, do NOT delete earlier history
|
||||
foreach (PlayoutItem item in maybeFirstItem)
|
||||
{
|
||||
var toRemove = playout.PlayoutHistory.Filter(h => h.When >= item.Start).ToList();
|
||||
foreach (PlayoutHistory history in toRemove)
|
||||
{
|
||||
playout.PlayoutHistory.Remove(history);
|
||||
}
|
||||
}
|
||||
|
||||
// save history changes
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// delete all playout items
|
||||
await dbContext.Database.ExecuteSqlAsync(
|
||||
$"DELETE FROM PlayoutItem WHERE PlayoutId = {playout.Id}",
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,8 @@
|
||||
</div>
|
||||
@if (context.PlayoutType == ProgramSchedulePlayoutType.Flood)
|
||||
{
|
||||
<div style="width: 48px"></div>
|
||||
<div style="width: 48px"></div>
|
||||
<MudTooltip Text="Edit Alternate Schedules">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.EditCalendar"
|
||||
Disabled="@EntityLocker.IsPlayoutLocked(context.PlayoutId)"
|
||||
@@ -107,9 +109,23 @@
|
||||
OnClick="@(_ => EditExternalJsonFile(context))">
|
||||
</MudIconButton>
|
||||
</MudTooltip>
|
||||
<div style="width: 48px"></div>
|
||||
<div style="width: 48px"></div>
|
||||
}
|
||||
else if (context.PlayoutType == ProgramSchedulePlayoutType.Block)
|
||||
{
|
||||
<MudTooltip Text="Erase Items">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Clear"
|
||||
Disabled="@EntityLocker.IsPlayoutLocked(context.PlayoutId)"
|
||||
OnClick="@(_ => EraseItems(context))">
|
||||
</MudIconButton>
|
||||
</MudTooltip>
|
||||
<MudTooltip Text="Erase Items and History">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.ClearAll"
|
||||
Disabled="@EntityLocker.IsPlayoutLocked(context.PlayoutId)"
|
||||
OnClick="@(_ => EraseHistory(context))">
|
||||
</MudIconButton>
|
||||
</MudTooltip>
|
||||
<MudTooltip Text="Edit Templates">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Edit"
|
||||
Disabled="@EntityLocker.IsPlayoutLocked(context.PlayoutId)"
|
||||
@@ -122,12 +138,7 @@
|
||||
OnClick="@(_ => ResetPlayout(context))">
|
||||
</MudIconButton>
|
||||
</MudTooltip>
|
||||
<MudTooltip Text="Erase Items and History">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Clear"
|
||||
Disabled="@EntityLocker.IsPlayoutLocked(context.PlayoutId)"
|
||||
OnClick="@(_ => EraseHistory(context))">
|
||||
</MudIconButton>
|
||||
</MudTooltip>
|
||||
<div style="width: 48px"></div>
|
||||
}
|
||||
<MudTooltip Text="Delete Playout">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Delete"
|
||||
@@ -287,6 +298,19 @@
|
||||
}
|
||||
}
|
||||
|
||||
private async Task EraseItems(PlayoutNameViewModel playout)
|
||||
{
|
||||
if (playout.PlayoutType is ProgramSchedulePlayoutType.Block)
|
||||
{
|
||||
await Mediator.Send(new EraseBlockPlayoutItems(playout.PlayoutId), _cts.Token);
|
||||
}
|
||||
|
||||
if (_selectedPlayoutId == playout.PlayoutId)
|
||||
{
|
||||
await PlayoutSelected(playout);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task EraseHistory(PlayoutNameViewModel playout)
|
||||
{
|
||||
if (playout.PlayoutType is ProgramSchedulePlayoutType.Block)
|
||||
|
||||
Reference in New Issue
Block a user