Task 3 of ersatztv#71 (reshuffle-playout). Appends Seed as the last positional field on PlayoutNameViewModel, threading it through the Mapper list projection, GetPlayoutByIdHandler detail projection, and the four Update*PlayoutHandler VM constructions, then exposing it on PlayoutListItemResponseModel/PlayoutResponseModel via the controller's ToResponse/ToListItemResponse mappers. ChannelControllerTests.MakePlayout was an additional construction site not listed in the task brief (positional-record break); updated to append 0 for Seed alongside the brief's ErsatzTV.Tests changes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
80 lines
3.1 KiB
C#
80 lines
3.1 KiB
C#
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.Playouts;
|
|
|
|
public class UpdatePlayoutHandler : IRequestHandler<UpdatePlayout, Either<BaseError, PlayoutNameViewModel>>
|
|
{
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
|
|
public UpdatePlayoutHandler(IDbContextFactory<TvContext> dbContextFactory) =>
|
|
_dbContextFactory = dbContextFactory;
|
|
|
|
public async Task<Either<BaseError, PlayoutNameViewModel>> Handle(
|
|
UpdatePlayout 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 static async Task<PlayoutNameViewModel> ApplyUpdateRequest(
|
|
TvContext dbContext,
|
|
UpdatePlayout request,
|
|
Playout playout,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
playout.DailyRebuildTime = null;
|
|
|
|
foreach (TimeSpan dailyRebuildTime in request.DailyRebuildTime)
|
|
{
|
|
playout.DailyRebuildTime = dailyRebuildTime;
|
|
}
|
|
|
|
// Rotate the playout ETag only when the daily-rebuild-time actually changed — a no-op re-submit
|
|
// must not bump (#269). When it changed, force-write past a concurrent Version bump from a
|
|
// replace-all editor (this settings writer takes no If-Match, so a benign race must not 500) —
|
|
// #253/#269 §7a.
|
|
if (dbContext.ChangeTracker.HasChanges())
|
|
{
|
|
playout.Version++;
|
|
await dbContext.SaveChangesForcingVersion(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,
|
|
playout.Version,
|
|
playout.Seed);
|
|
}
|
|
|
|
private static Task<Validation<BaseError, Playout>> Validate(
|
|
TvContext dbContext,
|
|
UpdatePlayout request,
|
|
CancellationToken cancellationToken) =>
|
|
PlayoutMustExist(dbContext, request, cancellationToken);
|
|
|
|
private static Task<Validation<BaseError, Playout>> PlayoutMustExist(
|
|
TvContext dbContext,
|
|
UpdatePlayout updatePlayout,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Playouts
|
|
.Include(p => p.Channel)
|
|
.Include(p => p.ProgramSchedule)
|
|
.SelectOneAsync(p => p.Id, p => p.Id == updatePlayout.PlayoutId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Playout does not exist."));
|
|
}
|