using ErsatzTV.Core; using ErsatzTV.Core.Domain; using ErsatzTV.Infrastructure.Data; using ErsatzTV.Infrastructure.Extensions; using Microsoft.EntityFrameworkCore; namespace ErsatzTV.Application.FFmpegProfiles; public class DeleteFFmpegProfileHandler : IRequestHandler> { private readonly IDbContextFactory _dbContextFactory; public DeleteFFmpegProfileHandler(IDbContextFactory dbContextFactory) => _dbContextFactory = dbContextFactory; public async Task> Handle( DeleteFFmpegProfile request, CancellationToken cancellationToken) { await using TvContext dbContext = _dbContextFactory.CreateDbContext(); Validation validation = await FFmpegProfileMustExist(dbContext, request); return await validation.Apply(p => DoDeletion(dbContext, p)); } private static async Task DoDeletion(TvContext dbContext, FFmpegProfile ffmpegProfile) { dbContext.FFmpegProfiles.Remove(ffmpegProfile); await dbContext.SaveChangesAsync(); 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")); }