add copy schedule feature (#1141)
This commit is contained in:
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||
|
||||
## [Unreleased]
|
||||
### Added
|
||||
- Add button to copy/clone schedule from schedules table
|
||||
|
||||
### Fixed
|
||||
- Fix many QSV pipeline bugs
|
||||
- Fix MPEG2 video format with QSV and VAAPI acceleration
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
using ErsatzTV.Core;
|
||||
|
||||
namespace ErsatzTV.Application.ProgramSchedules;
|
||||
|
||||
public record CopyProgramSchedule
|
||||
(int ProgramScheduleId, string Name) : IRequest<Either<BaseError, ProgramScheduleViewModel>>;
|
||||
@@ -0,0 +1,113 @@
|
||||
using ErsatzTV.Core;
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Infrastructure.Data;
|
||||
using ErsatzTV.Infrastructure.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using static ErsatzTV.Application.ProgramSchedules.Mapper;
|
||||
|
||||
namespace ErsatzTV.Application.ProgramSchedules;
|
||||
|
||||
public class
|
||||
CopyProgramScheduleHandler : IRequestHandler<CopyProgramSchedule, Either<BaseError, ProgramScheduleViewModel>>
|
||||
{
|
||||
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
||||
|
||||
public CopyProgramScheduleHandler(IDbContextFactory<TvContext> dbContextFactory) =>
|
||||
_dbContextFactory = dbContextFactory;
|
||||
|
||||
public async Task<Either<BaseError, ProgramScheduleViewModel>> Handle(
|
||||
CopyProgramSchedule request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
Validation<BaseError, ProgramSchedule> validation = await Validate(dbContext, request);
|
||||
return await validation.Apply(p => PerformCopy(dbContext, p, request, cancellationToken));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BaseError.New(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<ProgramScheduleViewModel> PerformCopy(
|
||||
TvContext dbContext,
|
||||
ProgramSchedule schedule,
|
||||
CopyProgramSchedule request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var clone = new ProgramSchedule();
|
||||
await dbContext.AddAsync(clone, cancellationToken);
|
||||
|
||||
clone.Name = request.Name;
|
||||
clone.RandomStartPoint = schedule.RandomStartPoint;
|
||||
clone.ShuffleScheduleItems = schedule.ShuffleScheduleItems;
|
||||
clone.TreatCollectionsAsShows = schedule.TreatCollectionsAsShows;
|
||||
clone.KeepMultiPartEpisodesTogether = schedule.KeepMultiPartEpisodesTogether;
|
||||
|
||||
// no playouts, no alternates
|
||||
clone.Playouts = new List<Playout>();
|
||||
clone.ProgramScheduleAlternates = new List<ProgramScheduleAlternate>();
|
||||
|
||||
// clone all items
|
||||
clone.Items = new List<ProgramScheduleItem>();
|
||||
foreach (ProgramScheduleItem item in schedule.Items)
|
||||
{
|
||||
PropertyValues itemValues = dbContext.Entry(item).CurrentValues.Clone();
|
||||
itemValues["Id"] = 0;
|
||||
|
||||
ProgramScheduleItem itemClone = item switch
|
||||
{
|
||||
ProgramScheduleItemFlood => new ProgramScheduleItemFlood(),
|
||||
ProgramScheduleItemDuration => new ProgramScheduleItemDuration(),
|
||||
ProgramScheduleItemMultiple => new ProgramScheduleItemMultiple(),
|
||||
_ => new ProgramScheduleItemOne()
|
||||
};
|
||||
|
||||
await dbContext.AddAsync(itemClone, cancellationToken);
|
||||
dbContext.Entry(itemClone).CurrentValues.SetValues(itemValues);
|
||||
|
||||
itemClone.ProgramScheduleId = 0;
|
||||
itemClone.ProgramSchedule = clone;
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return ProjectToViewModel(clone);
|
||||
}
|
||||
|
||||
private static async Task<Validation<BaseError, ProgramSchedule>> Validate(
|
||||
TvContext dbContext,
|
||||
CopyProgramSchedule request) =>
|
||||
(await ScheduleMustExist(dbContext, request), await ValidateName(dbContext, request))
|
||||
.Apply((programSchedule, _) => programSchedule);
|
||||
|
||||
private static Task<Validation<BaseError, ProgramSchedule>> ScheduleMustExist(
|
||||
TvContext dbContext,
|
||||
CopyProgramSchedule request) =>
|
||||
dbContext.ProgramSchedules
|
||||
.AsNoTracking()
|
||||
.Include(ps => ps.Items)
|
||||
.SelectOneAsync(p => p.Id, p => p.Id == request.ProgramScheduleId)
|
||||
.Map(o => o.ToValidation<BaseError>("Schedule does not exist."));
|
||||
|
||||
private static async Task<Validation<BaseError, string>> ValidateName(
|
||||
TvContext dbContext,
|
||||
CopyProgramSchedule request)
|
||||
{
|
||||
List<string> allNames = await dbContext.ProgramSchedules
|
||||
.Map(ps => ps.Name)
|
||||
.ToListAsync();
|
||||
|
||||
Validation<BaseError, string> result1 = request.NotEmpty(c => c.Name)
|
||||
.Bind(_ => request.NotLongerThan(50)(c => c.Name));
|
||||
|
||||
var result2 = Optional(request.Name)
|
||||
.Where(name => !allNames.Contains(name))
|
||||
.ToValidation<BaseError>("Schedule name must be unique");
|
||||
|
||||
return (result1, result2).Apply((_, _) => request.Name);
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,10 @@ public class CreateProgramScheduleHandler :
|
||||
CreateProgramSchedule request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
|
||||
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
Validation<BaseError, ProgramSchedule> validation = await Validate(dbContext, request);
|
||||
return await LanguageExtensions.Apply(validation, ps => PersistProgramSchedule(dbContext, ps));
|
||||
return await validation.Apply(ps => PersistProgramSchedule(dbContext, ps));
|
||||
}
|
||||
|
||||
private static async Task<CreateProgramScheduleResult> PersistProgramSchedule(
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
@using ErsatzTV.Application.Configuration
|
||||
@using NaturalSort.Extension
|
||||
@implements IDisposable
|
||||
@inject IDialogService _dialog
|
||||
@inject IMediator _mediator
|
||||
@inject IDialogService Dialog
|
||||
@inject IMediator Mediator
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8">
|
||||
<MudTable Hover="true"
|
||||
@@ -18,7 +19,7 @@
|
||||
</ToolBarContent>
|
||||
<ColGroup>
|
||||
<col/>
|
||||
<col style="width: 180px;"/>
|
||||
<col style="width: 240px;"/>
|
||||
</ColGroup>
|
||||
<HeaderContent>
|
||||
<MudTh>
|
||||
@@ -42,6 +43,11 @@
|
||||
Link="@($"schedules/{context.Id}/items")">
|
||||
</MudIconButton>
|
||||
</MudTooltip>
|
||||
<MudTooltip Text="Copy Schedule">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.ContentCopy"
|
||||
OnClick="@(_ => CopySchedule(context))">
|
||||
</MudIconButton>
|
||||
</MudTooltip>
|
||||
<MudTooltip Text="Delete Schedule">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Delete"
|
||||
OnClick="@(_ => DeleteSchedule(context))">
|
||||
@@ -104,9 +110,9 @@
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_rowsPerPage = await _mediator.Send(new GetConfigElementByKey(ConfigElementKey.SchedulesPageSize), _cts.Token)
|
||||
_rowsPerPage = await Mediator.Send(new GetConfigElementByKey(ConfigElementKey.SchedulesPageSize), _cts.Token)
|
||||
.Map(maybeRows => maybeRows.Match(ce => int.TryParse(ce.Value, out int rows) ? rows : 10, () => 10));
|
||||
_detailRowsPerPage = await _mediator.Send(new GetConfigElementByKey(ConfigElementKey.SchedulesDetailPageSize), _cts.Token)
|
||||
_detailRowsPerPage = await Mediator.Send(new GetConfigElementByKey(ConfigElementKey.SchedulesDetailPageSize), _cts.Token)
|
||||
.Map(maybeRows => maybeRows.Match(ce => int.TryParse(ce.Value, out int rows) ? rows : 10, () => 10));
|
||||
}
|
||||
|
||||
@@ -124,11 +130,11 @@
|
||||
var parameters = new DialogParameters { { "EntityType", "schedule" }, { "EntityName", programSchedule.Name } };
|
||||
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
|
||||
|
||||
IDialogReference dialog = await _dialog.ShowAsync<DeleteDialog>("Delete Schedule", parameters, options);
|
||||
IDialogReference dialog = await Dialog.ShowAsync<DeleteDialog>("Delete Schedule", parameters, options);
|
||||
DialogResult result = await dialog.Result;
|
||||
if (!result.Canceled)
|
||||
{
|
||||
await _mediator.Send(new DeleteProgramSchedule(programSchedule.Id), _cts.Token);
|
||||
await Mediator.Send(new DeleteProgramSchedule(programSchedule.Id), _cts.Token);
|
||||
if (_table != null)
|
||||
{
|
||||
await _table.ReloadServerData();
|
||||
@@ -139,12 +145,25 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CopySchedule(ProgramScheduleViewModel programSchedule)
|
||||
{
|
||||
var parameters = new DialogParameters { { "ProgramScheduleId", programSchedule.Id } };
|
||||
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };
|
||||
|
||||
IDialogReference dialog = await Dialog.ShowAsync<CopyScheduleDialog>("Copy Schedule", parameters, options);
|
||||
DialogResult dialogResult = await dialog.Result;
|
||||
if (!dialogResult.Canceled && dialogResult.Data is ProgramScheduleViewModel data)
|
||||
{
|
||||
NavigationManager.NavigateTo($"schedules/{data.Id}/items");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<TableData<ProgramScheduleViewModel>> ServerReload(TableState state)
|
||||
{
|
||||
await _mediator.Send(new SaveConfigElementByKey(ConfigElementKey.SchedulesPageSize, state.PageSize.ToString()), _cts.Token);
|
||||
await Mediator.Send(new SaveConfigElementByKey(ConfigElementKey.SchedulesPageSize, state.PageSize.ToString()), _cts.Token);
|
||||
|
||||
List<ProgramScheduleViewModel> schedules = await _mediator.Send(new GetAllProgramSchedules(), _cts.Token);
|
||||
List<ProgramScheduleViewModel> schedules = await Mediator.Send(new GetAllProgramSchedules(), _cts.Token);
|
||||
IOrderedEnumerable<ProgramScheduleViewModel> sorted = schedules.OrderBy(s => s.Name, new NaturalSortComparer(StringComparison.CurrentCultureIgnoreCase));
|
||||
|
||||
// TODO: properly page this data
|
||||
@@ -157,9 +176,9 @@
|
||||
|
||||
private async Task<TableData<ProgramScheduleItemViewModel>> DetailServerReload(TableState state)
|
||||
{
|
||||
await _mediator.Send(new SaveConfigElementByKey(ConfigElementKey.SchedulesDetailPageSize, state.PageSize.ToString()), _cts.Token);
|
||||
await Mediator.Send(new SaveConfigElementByKey(ConfigElementKey.SchedulesDetailPageSize, state.PageSize.ToString()), _cts.Token);
|
||||
|
||||
List<ProgramScheduleItemViewModel> scheduleItems = await _mediator.Send(new GetProgramScheduleItems(_selectedSchedule.Id), _cts.Token);
|
||||
List<ProgramScheduleItemViewModel> scheduleItems = await Mediator.Send(new GetProgramScheduleItems(_selectedSchedule.Id), _cts.Token);
|
||||
IOrderedEnumerable<ProgramScheduleItemViewModel> sorted = scheduleItems.OrderBy(s => s.Index);
|
||||
|
||||
// TODO: properly page this data
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
@using ErsatzTV.Application.ProgramSchedules
|
||||
@implements IDisposable
|
||||
@inject IMediator Mediator
|
||||
@inject ISnackbar Snackbar
|
||||
@inject ILogger<CopyScheduleDialog> Logger
|
||||
|
||||
<MudDialog>
|
||||
<DialogContent>
|
||||
<EditForm Model="@_dummyModel" OnSubmit="@(_ => Submit())">
|
||||
<MudContainer Class="mb-6">
|
||||
<MudText>
|
||||
Enter a name for the new Schedule
|
||||
</MudText>
|
||||
</MudContainer>
|
||||
<MudTextField T="string" Label="New Schedule Name"
|
||||
@bind-Text="@_newName"
|
||||
Class="mb-6 mx-4">
|
||||
</MudTextField>
|
||||
</EditForm>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="Cancel" ButtonType="ButtonType.Reset">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit">
|
||||
Copy Schedule
|
||||
</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
private readonly CancellationTokenSource _cts = new();
|
||||
|
||||
[CascadingParameter]
|
||||
MudDialogInstance MudDialog { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public int ProgramScheduleId { get; set; }
|
||||
|
||||
private record DummyModel;
|
||||
|
||||
private readonly DummyModel _dummyModel = new();
|
||||
|
||||
private string _newName;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts.Cancel();
|
||||
_cts.Dispose();
|
||||
}
|
||||
|
||||
private bool CanSubmit() => !string.IsNullOrWhiteSpace(_newName);
|
||||
|
||||
private async Task Submit()
|
||||
{
|
||||
if (!CanSubmit())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Either<BaseError, ProgramScheduleViewModel> maybeResult =
|
||||
await Mediator.Send(new CopyProgramSchedule(ProgramScheduleId, _newName), _cts.Token);
|
||||
|
||||
maybeResult.Match(
|
||||
schedule => { MudDialog.Close(DialogResult.Ok(schedule)); },
|
||||
error =>
|
||||
{
|
||||
Snackbar.Add(error.Value, Severity.Error);
|
||||
Logger.LogError("Error copying Schedule: {Error}", error.Value);
|
||||
MudDialog.Close(DialogResult.Cancel());
|
||||
});
|
||||
}
|
||||
|
||||
private void Cancel(MouseEventArgs e) => MudDialog.Cancel();
|
||||
}
|
||||
Reference in New Issue
Block a user