using ErsatzTV.Core.Domain; using ErsatzTV.Infrastructure.Data; using ErsatzTV.Infrastructure.Extensions; using Microsoft.EntityFrameworkCore; using static ErsatzTV.Application.FFmpegProfiles.Mapper; namespace ErsatzTV.Application.FFmpegProfiles; public class NewFFmpegProfileHandler : IRequestHandler { private readonly IDbContextFactory _dbContextFactory; public NewFFmpegProfileHandler(IDbContextFactory dbContextFactory) => _dbContextFactory = dbContextFactory; public async Task Handle(NewFFmpegProfile request, CancellationToken cancellationToken) { await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); int defaultResolutionId = await dbContext.ConfigElements .GetValue(ConfigElementKey.FFmpegDefaultResolutionId) .IfNoneAsync(0); List allResolutions = await dbContext.Resolutions .ToListAsync(cancellationToken); Option maybeDefaultResolution = allResolutions.Find(r => r.Id == defaultResolutionId); Resolution defaultResolution = maybeDefaultResolution.Match(identity, () => allResolutions.Head()); return ProjectToViewModel(FFmpegProfile.New("New Profile", defaultResolution)); } }