Files
ersatztv/ErsatzTV.Application/Playouts/Commands/UpdateScriptedPlayoutHandler.cs
T
timothyandClaude Fable 5 50b13afac3 feat(api): REST endpoints for playout alternate schedules and templates (#144 S6 (#162))
Adds per-playout REST for classic-playout alternate schedules and
block-playout templates, plus the default-deco read-side deferred from S3:

- GET/PUT /api/playouts/{id}/alternate-schedules (Classic only; 422 otherwise)
- GET/PUT /api/playouts/{id}/templates (Block only; 422 otherwise)
- PlayoutResponseModel gains decoId/decoName (GetPlayoutById includes Deco)

PUT assigns Index from array order (top = highest priority, last = catch-all
default), mirroring the Blazor editors. Alternate-schedule PUT requires a
non-empty list and existing ProgramScheduleIds; template PUT requires existing
TemplateIds and any supplied DecoTemplateId. Regenerates the OpenAPI spec.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 20:29:29 +02:00

85 lines
3.1 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)
{
await workerChannel.WriteAsync(new RefreshChannelData(playout.Channel.Number), cancellationToken);
}
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);
}
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."));
}