|
|
|
@ -1,7 +1,5 @@ |
|
|
|
using ErsatzTV.Core.Domain; |
|
|
|
using ErsatzTV.Core.Domain; |
|
|
|
using ErsatzTV.Core.Domain.Scheduling; |
|
|
|
|
|
|
|
using ErsatzTV.Infrastructure.Data; |
|
|
|
using ErsatzTV.Infrastructure.Data; |
|
|
|
using ErsatzTV.Infrastructure.Extensions; |
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
|
|
|
|
|
|
|
|
namespace ErsatzTV.Application.Scheduling; |
|
|
|
namespace ErsatzTV.Application.Scheduling; |
|
|
|
@ -14,39 +12,49 @@ public class ErasePlayoutItemsHandler(IDbContextFactory<TvContext> dbContextFact |
|
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
Option<Playout> maybePlayout = await dbContext.Playouts |
|
|
|
Option<Playout> maybePlayout = await dbContext.Playouts |
|
|
|
.Include(p => p.Items) |
|
|
|
.TagWithCallSite() |
|
|
|
.Include(p => p.PlayoutHistory) |
|
|
|
.AsNoTracking() |
|
|
|
.Filter(p => p.ScheduleKind == PlayoutScheduleKind.Block || |
|
|
|
.Filter(p => p.ScheduleKind == PlayoutScheduleKind.Block || |
|
|
|
p.ScheduleKind == PlayoutScheduleKind.Sequential || |
|
|
|
p.ScheduleKind == PlayoutScheduleKind.Sequential || |
|
|
|
p.ScheduleKind == PlayoutScheduleKind.Scripted) |
|
|
|
p.ScheduleKind == PlayoutScheduleKind.Scripted) |
|
|
|
.SelectOneAsync(p => p.Id, p => p.Id == request.PlayoutId, cancellationToken); |
|
|
|
.SingleOrDefaultAsync(p => p.Id == request.PlayoutId, cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
foreach (Playout playout in maybePlayout) |
|
|
|
foreach (Playout playout in maybePlayout) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
// find the earliest item that finishes after "now"
|
|
|
|
// find the earliest item that finishes after "now"
|
|
|
|
Option<PlayoutItem> maybeFirstItem = playout.Items |
|
|
|
Option<PlayoutItem> maybeFirstItem = await dbContext.PlayoutItems |
|
|
|
.Filter(i => i.FinishOffset > DateTimeOffset.Now) |
|
|
|
.TagWithCallSite() |
|
|
|
.OrderBy(i => i.StartOffset) |
|
|
|
.AsNoTracking() |
|
|
|
.HeadOrNone(); |
|
|
|
.Where(pi => pi.PlayoutId == playout.Id) |
|
|
|
|
|
|
|
.Where(pi => pi.Finish > DateTime.UtcNow) |
|
|
|
// delete all history starting with that item
|
|
|
|
.OrderBy(i => i.Start) |
|
|
|
// importantly, do NOT delete earlier history
|
|
|
|
.FirstOrDefaultAsync(cancellationToken); |
|
|
|
foreach (PlayoutItem item in maybeFirstItem) |
|
|
|
|
|
|
|
|
|
|
|
foreach (PlayoutItem firstItem in maybeFirstItem) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var toRemove = playout.PlayoutHistory.Filter(h => h.When >= item.Start).ToList(); |
|
|
|
// delete all history starting with that item
|
|
|
|
foreach (PlayoutHistory history in toRemove) |
|
|
|
// importantly, do NOT delete earlier history
|
|
|
|
{ |
|
|
|
await dbContext.PlayoutHistory |
|
|
|
playout.PlayoutHistory.Remove(history); |
|
|
|
.Where(ph => ph.PlayoutId == playout.Id) |
|
|
|
} |
|
|
|
.Where(ph => ph.When >= firstItem.Start) |
|
|
|
|
|
|
|
.ExecuteDeleteAsync(cancellationToken); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// save history changes
|
|
|
|
await dbContext.PlayoutItems |
|
|
|
await dbContext.SaveChangesAsync(cancellationToken); |
|
|
|
.Where(pi => pi.PlayoutId == playout.Id) |
|
|
|
|
|
|
|
.ExecuteDeleteAsync(cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await dbContext.PlayoutGaps |
|
|
|
|
|
|
|
.Where(pg => pg.PlayoutId == playout.Id) |
|
|
|
|
|
|
|
.ExecuteDeleteAsync(cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await dbContext.PlayoutBuildStatus |
|
|
|
|
|
|
|
.Where(pb => pb.PlayoutId == playout.Id) |
|
|
|
|
|
|
|
.ExecuteDeleteAsync(cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
// delete all playout items
|
|
|
|
await transaction.CommitAsync(cancellationToken); |
|
|
|
await dbContext.Database.ExecuteSqlAsync( |
|
|
|
|
|
|
|
$"DELETE FROM PlayoutItem WHERE PlayoutId = {playout.Id}", |
|
|
|
|
|
|
|
cancellationToken); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|