Wire the frozen #253 ETag/If-Match/412 recipe onto ProgramSchedule / schedule-items, keeping the PR#258 positional in-place reconcile intact. Backend: - ReplaceProgramScheduleItems command gains Option<int> ExpectedVersion; ReplaceScheduleItemsRequest.ToCommand threads it. - Handler: standalone CheckVersion Either AFTER validation (so 412 isn't flattened to 422), unconditional Version++ before save, guarded save via SaveChangesWithConcurrencyGuard, and 412 propagated without running the post-save reload/enqueue. - ProgramScheduleViewModel + Mapper carry Version. - ScheduleController: GET /items emits ETag; PUT /items parses If-Match (malformed -> 400), threads ExpectedVersion, re-queries for the new ETag, and advertises 400/412. - Sibling config-writers (Add/Delete item, Update schedule) bump Version. Frontend: - schedules.ts: getScheduleItemsWithMeta + replaceScheduleItems(ifMatch) returning ResponseWithMeta. - SchedulesScreen: etagRef threaded through the #242 dirty-guard (set from load + every successful save); 412 opens a conflict ConfirmDialog whose Reload discards the draft and re-runs loadItems. Tests: handler concurrency suite (stale->412 no mutation + fill-group state untouched, match/absent success+bump, no-op still bumps, racing save->412); controller ETag/If-Match/412 cases; SchedulesScreen 412-conflict-dialog test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
99 lines
4.2 KiB
C#
99 lines
4.2 KiB
C#
using System.Threading.Channels;
|
|
using ErsatzTV.Application.Playouts;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Core.Scheduling;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using static ErsatzTV.Application.ProgramSchedules.Mapper;
|
|
|
|
namespace ErsatzTV.Application.ProgramSchedules;
|
|
|
|
public class AddProgramScheduleItemHandler : ProgramScheduleItemCommandBase,
|
|
IRequestHandler<AddProgramScheduleItem, Either<BaseError, ProgramScheduleItemViewModel>>
|
|
{
|
|
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
|
|
public AddProgramScheduleItemHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> channel)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_channel = channel;
|
|
}
|
|
|
|
public async Task<Either<BaseError, ProgramScheduleItemViewModel>> Handle(
|
|
AddProgramScheduleItem request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Option<ProgramSchedule> maybeProgramSchedule =
|
|
await ProgramScheduleMustExist(dbContext, request.ProgramScheduleId, cancellationToken);
|
|
return await maybeProgramSchedule.Match(
|
|
Some: async programSchedule =>
|
|
{
|
|
Validation<BaseError, ProgramSchedule> validation = await Validate(dbContext, request, programSchedule);
|
|
return await validation.Apply(ps => PersistItem(dbContext, request, ps, cancellationToken));
|
|
},
|
|
None: () => Task.FromResult<Either<BaseError, ProgramScheduleItemViewModel>>(
|
|
new NotFoundError("[ProgramScheduleId] does not exist.")));
|
|
}
|
|
|
|
private async Task<ProgramScheduleItemViewModel> PersistItem(
|
|
TvContext dbContext,
|
|
AddProgramScheduleItem request,
|
|
ProgramSchedule programSchedule,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
int nextIndex = programSchedule.Items.Select(i => i.Index).DefaultIfEmpty(0).Max() + 1;
|
|
|
|
ProgramScheduleItem item = BuildItem(programSchedule, nextIndex, request);
|
|
programSchedule.Items.Add(item);
|
|
|
|
// bump the optimistic-concurrency token so this config edit rotates other clients' ETags (#253)
|
|
programSchedule.Version++;
|
|
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
// refresh any playouts that use this schedule
|
|
foreach (Playout playout in programSchedule.Playouts)
|
|
{
|
|
await _channel.WriteAsync(new BuildPlayout(playout.Id, PlayoutBuildMode.Refresh), cancellationToken);
|
|
}
|
|
|
|
// reload with the full navigation graph before projecting: BuildItem creates the watermark/graphics
|
|
// join rows with only their foreign-key ids set, so the tracked entities have null Watermark /
|
|
// GraphicsElement navs that ProjectToViewModel dereferences (would 500 on POST — see #229).
|
|
ProgramScheduleItem persisted = await dbContext.ProgramScheduleItems
|
|
.Filter(psi => psi.Id == item.Id)
|
|
.IncludeScheduleItemDetails()
|
|
.SingleAsync(cancellationToken);
|
|
|
|
return ProjectToViewModel(persisted);
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, ProgramSchedule>> Validate(
|
|
TvContext dbContext,
|
|
AddProgramScheduleItem request,
|
|
ProgramSchedule programSchedule)
|
|
{
|
|
Validation<BaseError, ProgramSchedule> validation =
|
|
PlayoutModeMustBeValid(request, programSchedule)
|
|
.Bind(programSchedule => CollectionTypeMustBeValid(request, programSchedule));
|
|
|
|
return await validation.ToEither().Match(
|
|
Left: error => Task.FromResult<Validation<BaseError, ProgramSchedule>>(
|
|
Fail<BaseError, ProgramSchedule>(error)),
|
|
Right: async validProgramSchedule =>
|
|
{
|
|
Either<BaseError, ProgramSchedule> fillerResult = await FillerConfigurationMustBeValid(
|
|
dbContext,
|
|
request,
|
|
validProgramSchedule);
|
|
return fillerResult.ToValidation();
|
|
});
|
|
}
|
|
}
|