diff --git a/ErsatzTV.Application/Channels/Commands/DeleteChannel.cs b/ErsatzTV.Application/Channels/Commands/DeleteChannel.cs index f97ec657..4facf8d7 100644 --- a/ErsatzTV.Application/Channels/Commands/DeleteChannel.cs +++ b/ErsatzTV.Application/Channels/Commands/DeleteChannel.cs @@ -2,4 +2,4 @@ namespace ErsatzTV.Application.Channels; -public record DeleteChannel(int ChannelId) : IRequest>; +public record DeleteChannel(int ChannelId) : IRequest>; diff --git a/ErsatzTV.Application/Channels/Commands/DeleteChannelHandler.cs b/ErsatzTV.Application/Channels/Commands/DeleteChannelHandler.cs index 9a3dbc7e..fbb13998 100644 --- a/ErsatzTV.Application/Channels/Commands/DeleteChannelHandler.cs +++ b/ErsatzTV.Application/Channels/Commands/DeleteChannelHandler.cs @@ -1,23 +1,39 @@ using ErsatzTV.Core; -using ErsatzTV.Core.Interfaces.Repositories; +using ErsatzTV.Core.Domain; +using ErsatzTV.Infrastructure.Data; +using ErsatzTV.Infrastructure.Extensions; +using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Application.Channels; -public class DeleteChannelHandler : IRequestHandler> +public class DeleteChannelHandler : IRequestHandler> { - private readonly IChannelRepository _channelRepository; + private readonly IDbContextFactory _dbContextFactory; - public DeleteChannelHandler(IChannelRepository channelRepository) => _channelRepository = channelRepository; + public DeleteChannelHandler(IDbContextFactory dbContextFactory) + { + _dbContextFactory = dbContextFactory; + } - public async Task> Handle(DeleteChannel request, CancellationToken cancellationToken) => - (await ChannelMustExist(request)) - .Map(DoDeletion) - .ToEither(); + public async Task> Handle(DeleteChannel request, CancellationToken cancellationToken) + { + await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); + Validation validation = await ChannelMustExist(dbContext, request); + return await validation.Apply(c => DoDeletion(dbContext, c)); + } - private Task DoDeletion(int channelId) => _channelRepository.Delete(channelId); + private static async Task DoDeletion(TvContext dbContext, Channel channel) + { + dbContext.Channels.Remove(channel); + await dbContext.SaveChangesAsync(); + + return Unit.Default; + } - private async Task> ChannelMustExist(DeleteChannel deleteChannel) => - (await _channelRepository.Get(deleteChannel.ChannelId)) - .ToValidation($"Channel {deleteChannel.ChannelId} does not exist.") - .Map(c => c.Id); + private static async Task> ChannelMustExist(TvContext dbContext, DeleteChannel deleteChannel) + { + Option maybeChannel = await dbContext.Channels + .SelectOneAsync(c => c.Id, c => c.Id == deleteChannel.ChannelId); + return maybeChannel.ToValidation($"Channel {deleteChannel.ChannelId} does not exist."); + } } diff --git a/ErsatzTV.Core/Interfaces/Repositories/IChannelRepository.cs b/ErsatzTV.Core/Interfaces/Repositories/IChannelRepository.cs index 5b53ec2b..76fb371e 100644 --- a/ErsatzTV.Core/Interfaces/Repositories/IChannelRepository.cs +++ b/ErsatzTV.Core/Interfaces/Repositories/IChannelRepository.cs @@ -7,6 +7,4 @@ public interface IChannelRepository Task> Get(int id); Task> GetByNumber(string number); Task> GetAll(); - Task> GetAllForGuide(); - Task Delete(int channelId); } diff --git a/ErsatzTV.Infrastructure/Data/Repositories/ChannelRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/ChannelRepository.cs index 86367d2b..0df4abfd 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/ChannelRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/ChannelRepository.cs @@ -44,75 +44,4 @@ public class ChannelRepository : IChannelRepository .Include(c => c.Playouts) .ToListAsync(); } - - public async Task> GetAllForGuide() - { - await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(); - return await dbContext.Channels - .AsNoTracking() - .Include(c => c.Artwork) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as Episode).EpisodeMetadata) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as Episode).Season) - .ThenInclude(s => s.Show) - .ThenInclude(s => s.ShowMetadata) - .ThenInclude(sm => sm.Artwork) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as Episode).Season) - .ThenInclude(s => s.Show) - .ThenInclude(s => s.ShowMetadata) - .ThenInclude(em => em.Genres) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as Movie).MovieMetadata) - .ThenInclude(mm => mm.Artwork) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as Movie).MovieMetadata) - .ThenInclude(mm => mm.Genres) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as MusicVideo).MusicVideoMetadata) - .ThenInclude(mm => mm.Artwork) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as MusicVideo).MusicVideoMetadata) - .ThenInclude(mvm => mvm.Genres) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as MusicVideo).Artist) - .ThenInclude(a => a.ArtistMetadata) - .ThenInclude(am => am.Genres) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as OtherVideo).OtherVideoMetadata) - .ThenInclude(vm => vm.Artwork) - .Include(c => c.Playouts) - .ThenInclude(p => p.Items) - .ThenInclude(i => i.MediaItem) - .ThenInclude(i => (i as Song).SongMetadata) - .ThenInclude(vm => vm.Artwork) - .ToListAsync(); - } - - public async Task Delete(int channelId) - { - await using TvContext dbContext = _dbContextFactory.CreateDbContext(); - Channel channel = await dbContext.Channels.FindAsync(channelId); - dbContext.Channels.Remove(channel); - await dbContext.SaveChangesAsync(); - } }