Stream custom live channels using your own media
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

53 lines
1.6 KiB

using EFCore.BulkExtensions;
using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.Playouts;
public class InsertPlayoutGapsHandler(IDbContextFactory<TvContext> dbContextFactory)
: IRequestHandler<InsertPlayoutGaps>
{
public async Task Handle(InsertPlayoutGaps request, CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var toAdd = new List<PlayoutGap>();
IOrderedQueryable<PlayoutItem> query = dbContext.PlayoutItems
.Filter(pi => pi.PlayoutId == request.PlayoutId)
.OrderBy(i => i.Start);
var queue = new Queue<PlayoutItem>(query);
while (queue.Count > 1)
{
PlayoutItem one = queue.Dequeue();
PlayoutItem two = queue.Peek();
DateTimeOffset start = one.FinishOffset;
DateTimeOffset finish = two.StartOffset;
if (start == finish)
{
continue;
}
var gap = new PlayoutGap
{
PlayoutId = request.PlayoutId,
Start = start.UtcDateTime,
Finish = finish.UtcDateTime
};
toAdd.Add(gap);
}
// delete all existing gaps
await dbContext.PlayoutGaps
.Where(pg => pg.PlayoutId == request.PlayoutId)
.ExecuteDeleteAsync(cancellationToken);
// insert new gaps
await dbContext.BulkInsertAsync(toAdd, cancellationToken: cancellationToken);
}
}