Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 5s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m11s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m51s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Fans the frozen Block recipe (api-conventions §7a) across the five Diff/Scalar replace-all endpoints, completing the #253 PR2→PR4 arc's implementable core: - #6 Collection custom-order, #7 Playout alternate-schedules, #8 Playout templates (shared Playout.Version), #9 MultiCollection, #10 RerunCollection — each: pre-check 412 as a standalone Either after validation (H2, subtype survives the Join flatten), unconditional Version++ (M1), guarded save, controller If-Match/ETag/400/412, SPA editor ETag round-trip + 412 conflict dialog. - H1: the two Playout handlers' catch(Exception)→422 restructured so the guard's PreconditionFailedError returns before the catch (412, not 422). - M2: RerunCollection/Collection refresh runs unconditionally on save; MultiCollection keeps its name-only→no-rebuild optimization by bumping on the first (name) save. - H3: UpdateDefaultDecoHandler bulk-bumps Playout.Version via .SetProperty. - Shared ConcurrencyHeaders.MalformedIfMatchProblem() for the 400 guard. - Deferred (→ #269): same-root non-bulk sibling config writers' ETag rotation. Tests: per-handler pre-check/bump concurrency tests (Playout ×2 incl. non-vacuous racing-save backstop, Rerun, Multi incl. name-only-no-rebuild M2, Collection); controller tests get a DefaultHttpContext for the header read/write. Docs: api-conventions §7a fan-out status, spa-conventions §4a list-editor note, decisions. Refs #253 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
88 lines
3.3 KiB
C#
88 lines
3.3 KiB
C#
using System.CommandLine.Parsing;
|
|
using System.IO.Abstractions;
|
|
using System.Threading.Channels;
|
|
using ErsatzTV.Application.Channels;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.Playouts;
|
|
|
|
public class
|
|
UpdateScriptedPlayoutHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> workerChannel,
|
|
IFileSystem fileSystem)
|
|
: IRequestHandler<UpdateScriptedPlayout,
|
|
Either<BaseError, PlayoutNameViewModel>>
|
|
{
|
|
public async Task<Either<BaseError, PlayoutNameViewModel>> Handle(
|
|
UpdateScriptedPlayout request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, Playout> validation = await Validate(dbContext, request, cancellationToken);
|
|
return await validation.Apply(playout => ApplyUpdateRequest(dbContext, request, playout, cancellationToken));
|
|
}
|
|
|
|
private async Task<PlayoutNameViewModel> ApplyUpdateRequest(
|
|
TvContext dbContext,
|
|
UpdateScriptedPlayout request,
|
|
Playout playout,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
playout.ScheduleFile = request.ScheduleFile;
|
|
|
|
if (await dbContext.SaveChangesAsync(cancellationToken) > 0)
|
|
{
|
|
// post-commit side effect runs on CancellationToken.None so a late request cancellation
|
|
// can't abort it after the commit landed (#254)
|
|
await workerChannel.WriteAsync(new RefreshChannelData(playout.Channel.Number), CancellationToken.None);
|
|
}
|
|
|
|
return new PlayoutNameViewModel(
|
|
playout.Id,
|
|
playout.ScheduleKind,
|
|
playout.Channel.Name,
|
|
playout.Channel.Number,
|
|
playout.Channel.PlayoutMode,
|
|
playout.ProgramSchedule?.Name ?? string.Empty,
|
|
playout.ScheduleFile,
|
|
playout.DailyRebuildTime,
|
|
playout.BuildStatus,
|
|
playout.DecoId,
|
|
playout.Deco?.Name,
|
|
playout.Version);
|
|
}
|
|
|
|
private async Task<Validation<BaseError, Playout>> Validate(
|
|
TvContext dbContext,
|
|
UpdateScriptedPlayout request,
|
|
CancellationToken cancellationToken) =>
|
|
(ValidateScheduleFile(request), await PlayoutMustExist(dbContext, request, cancellationToken))
|
|
.Apply((_, playout) => playout);
|
|
|
|
private Validation<BaseError, string> ValidateScheduleFile(UpdateScriptedPlayout request)
|
|
{
|
|
var args = CommandLineParser.SplitCommandLine(request.ScheduleFile).ToList();
|
|
string scriptFile = args[0];
|
|
if (!fileSystem.File.Exists(scriptFile))
|
|
{
|
|
return BaseError.New("Scripted schedule does not exist!");
|
|
}
|
|
|
|
return request.ScheduleFile;
|
|
}
|
|
|
|
private static Task<Validation<BaseError, Playout>> PlayoutMustExist(
|
|
TvContext dbContext,
|
|
UpdateScriptedPlayout updatePlayout,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Playouts
|
|
.Include(p => p.Channel)
|
|
.SelectOneAsync(p => p.Id, p => p.Id == updatePlayout.PlayoutId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Playout does not exist."));
|
|
}
|