@ -1,5 +1,8 @@
@@ -1,5 +1,8 @@
using ErsatzTV.Core ;
using System.Threading ;
using System.Threading.Channels ;
using ErsatzTV.Core ;
using ErsatzTV.Core.Domain ;
using ErsatzTV.Core.Interfaces.Metadata ;
using ErsatzTV.Infrastructure.Data ;
using ErsatzTV.Infrastructure.Extensions ;
using Microsoft.EntityFrameworkCore ;
@ -8,31 +11,49 @@ namespace ErsatzTV.Application.Channels;
@@ -8,31 +11,49 @@ namespace ErsatzTV.Application.Channels;
public class DeleteChannelHandler : IRequestHandler < DeleteChannel , Either < BaseError , Unit > >
{
private readonly ChannelWriter < IBackgroundServiceRequest > _ workerChannel ;
private readonly IDbContextFactory < TvContext > _d bContextFactory ;
private readonly ILocalFileSystem _l ocalFileSystem ;
public DeleteChannelHandler ( IDbContextFactory < TvContext > dbContextFactory )
public DeleteChannelHandler (
ChannelWriter < IBackgroundServiceRequest > workerChannel ,
IDbContextFactory < TvContext > dbContextFactory ,
ILocalFileSystem localFileSystem )
{
_ workerChannel = workerChannel ;
_d bContextFactory = dbContextFactory ;
_l ocalFileSystem = localFileSystem ;
}
public async Task < Either < BaseError , Unit > > Handle ( DeleteChannel request , CancellationToken cancellationToken )
{
await using TvContext dbContext = await _d bContextFactory . CreateDbContextAsync ( cancellationToken ) ;
Validation < BaseError , Channel > validation = await ChannelMustExist ( dbContext , request ) ;
return await validation . Apply ( c = > DoDeletion ( dbContext , c ) ) ;
Validation < BaseError , Core . Domain . Channel > validation = await ChannelMustExist ( dbContext , request ) ;
return await validation . Apply ( c = > DoDeletion ( dbContext , c , cancellationToken ) ) ;
}
private static async Task < Unit > DoDeletion ( TvContext dbContext , Channel channel )
private async Task < Unit > DoDeletion ( TvContext dbContext , Core . Domain . C hannel channel , CancellationToken cancellationToken )
{
dbContext . Channels . Remove ( channel ) ;
await dbContext . SaveChangesAsync ( ) ;
// delete channel data from channel guide cache
string cacheFile = Path . Combine ( FileSystemLayout . ChannelGuideCacheFolder , $"{channel.Number}.xml" ) ;
if ( _l ocalFileSystem . FileExists ( cacheFile ) )
{
File . Delete ( cacheFile ) ;
}
// refresh channel list to remove channel that has no playout
await _ workerChannel . WriteAsync ( new RefreshChannelList ( ) , cancellationToken ) ;
return Unit . Default ;
}
private static async Task < Validation < BaseError , Channel > > ChannelMustExist ( TvContext dbContext , DeleteChannel deleteChannel )
private static async Task < Validation < BaseError , Core . Domain . C hannel > > ChannelMustExist ( TvContext dbContext , DeleteChannel deleteChannel )
{
Option < Channel > maybeChannel = await dbContext . Channels
Option < Core . Domain . C hannel > maybeChannel = await dbContext . Channels
. SelectOneAsync ( c = > c . Id , c = > c . Id = = deleteChannel . ChannelId ) ;
return maybeChannel . ToValidation < BaseError > ( $"Channel {deleteChannel.ChannelId} does not exist." ) ;
}