using ErsatzTV.Core; using ErsatzTV.Core.Domain; using ErsatzTV.Core.Interfaces.Search; using ErsatzTV.Infrastructure.Data; using ErsatzTV.Infrastructure.Extensions; using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Application.FFmpegProfiles; public class DeleteFFmpegProfileHandler : IRequestHandler> { private readonly IDbContextFactory _dbContextFactory; private readonly ISearchTargets _searchTargets; public DeleteFFmpegProfileHandler(IDbContextFactory dbContextFactory, ISearchTargets searchTargets) { _dbContextFactory = dbContextFactory; _searchTargets = searchTargets; } public async Task> Handle( DeleteFFmpegProfile request, CancellationToken cancellationToken) { await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); Validation validation = await FFmpegProfileMustExist(dbContext, request); return await validation.Apply(p => DoDeletion(dbContext, p)); } private async Task DoDeletion(TvContext dbContext, FFmpegProfile ffmpegProfile) { dbContext.FFmpegProfiles.Remove(ffmpegProfile); await dbContext.SaveChangesAsync(); _searchTargets.SearchTargetsChanged(); return Unit.Default; } private static Task> FFmpegProfileMustExist( TvContext dbContext, DeleteFFmpegProfile request) => dbContext.FFmpegProfiles .SelectOneAsync(p => p.Id, p => p.Id == request.FFmpegProfileId) .Map(o => o.ToValidation($"FFmpegProfile {request.FFmpegProfileId} does not exist")); }