mirror of https://github.com/ErsatzTV/ErsatzTV.git
6 changed files with 172 additions and 11 deletions
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
using ErsatzTV.Core; |
||||
using LanguageExt; |
||||
using MediatR; |
||||
|
||||
namespace ErsatzTV.Application.FFmpegProfiles.Commands |
||||
{ |
||||
public record CopyFFmpegProfile |
||||
(int FFmpegProfileId, string Name) : IRequest<Either<BaseError, FFmpegProfileViewModel>>; |
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using ErsatzTV.Core; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using LanguageExt; |
||||
using MediatR; |
||||
using static ErsatzTV.Application.FFmpegProfiles.Mapper; |
||||
|
||||
namespace ErsatzTV.Application.FFmpegProfiles.Commands |
||||
{ |
||||
public class |
||||
CopyFFmpegProfileHandler : IRequestHandler<CopyFFmpegProfile, Either<BaseError, FFmpegProfileViewModel>> |
||||
{ |
||||
private readonly IFFmpegProfileRepository _ffmpegProfileRepository; |
||||
|
||||
public CopyFFmpegProfileHandler(IFFmpegProfileRepository ffmpegProfileRepository) => |
||||
_ffmpegProfileRepository = ffmpegProfileRepository; |
||||
|
||||
public Task<Either<BaseError, FFmpegProfileViewModel>> Handle( |
||||
CopyFFmpegProfile request, |
||||
CancellationToken cancellationToken) => |
||||
Validate(request) |
||||
.MapT(PerformCopy) |
||||
.Bind(v => v.ToEitherAsync()); |
||||
|
||||
private Task<FFmpegProfileViewModel> PerformCopy(CopyFFmpegProfile request) => |
||||
_ffmpegProfileRepository.Copy(request.FFmpegProfileId, request.Name) |
||||
.Map(ProjectToViewModel); |
||||
|
||||
private Task<Validation<BaseError, CopyFFmpegProfile>> Validate(CopyFFmpegProfile request) => |
||||
ValidateName(request).AsTask().MapT(_ => request); |
||||
|
||||
private Validation<BaseError, string> ValidateName(CopyFFmpegProfile request) => |
||||
request.NotEmpty(x => x.Name) |
||||
.Bind(_ => request.NotLongerThan(50)(x => x.Name)); |
||||
} |
||||
} |
||||
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
@using ErsatzTV.Application.FFmpegProfiles |
||||
@using ErsatzTV.Application.FFmpegProfiles.Commands |
||||
@inject IMediator _mediator |
||||
@inject ISnackbar _snackbar |
||||
@inject ILogger<AddToCollectionDialog> _logger |
||||
|
||||
<MudDialog> |
||||
<DialogContent> |
||||
<EditForm Model="@_dummyModel" OnSubmit="@(_ => Submit())"> |
||||
<MudContainer Class="mb-6"> |
||||
<MudText> |
||||
Enter a name for the new FFmpeg Profile |
||||
</MudText> |
||||
</MudContainer> |
||||
<MudTextFieldString Label="New FFmpeg Profile Name" |
||||
@bind-Text="@_newFFmpegProfileName" |
||||
Class="mb-6 mx-4"> |
||||
</MudTextFieldString> |
||||
</EditForm> |
||||
</DialogContent> |
||||
<DialogActions> |
||||
<MudButton OnClick="Cancel" ButtonType="ButtonType.Reset">Cancel</MudButton> |
||||
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit"> |
||||
Copy Profile |
||||
</MudButton> |
||||
</DialogActions> |
||||
</MudDialog> |
||||
|
||||
@code { |
||||
[CascadingParameter] |
||||
MudDialogInstance MudDialog { get; set; } |
||||
|
||||
[Parameter] |
||||
public int FFmpegProfileId { get; set; } |
||||
|
||||
private record DummyModel; |
||||
private readonly DummyModel _dummyModel = new(); |
||||
|
||||
private string _newFFmpegProfileName; |
||||
|
||||
private bool CanSubmit() => !string.IsNullOrWhiteSpace(_newFFmpegProfileName); |
||||
|
||||
private async Task Submit() |
||||
{ |
||||
if (!CanSubmit()) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
Either<BaseError, FFmpegProfileViewModel> maybeResult = |
||||
await _mediator.Send(new CopyFFmpegProfile(FFmpegProfileId, _newFFmpegProfileName)); |
||||
|
||||
maybeResult.Match( |
||||
ffmpegProfile => |
||||
{ |
||||
MudDialog.Close(DialogResult.Ok(ffmpegProfile)); |
||||
}, |
||||
error => |
||||
{ |
||||
_snackbar.Add(error.Value, Severity.Error); |
||||
_logger.LogError("Error copying FFmpeg Profile: {Error}", error.Value); |
||||
MudDialog.Close(DialogResult.Cancel()); |
||||
}); |
||||
} |
||||
|
||||
private void Cancel(MouseEventArgs e) |
||||
{ |
||||
MudDialog.Cancel(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue