Complete the #253 optimistic-concurrency contract's cross-editor ETag rotation tail. The non-If-Match config siblings mutated editor-visible state without bumping Version, so a concurrent editor of the same root never invalidated. Now the Collection Add*/Remove handlers bump Collection.Version, and UpdateCollection / UpdatePlayout / the three ScheduleFile writers (which already force-wrote past a concurrent bump) now bump too — all via SaveChangesForcingVersion (no If-Match → force write, never 412/500). No-op idempotence (Fable-caught trap): these gate reindex/BuildPlayout fan-out on SaveChanges()>0, so an unconditional bump would fire spurious rebuilds on an idempotent re-add / same-value re-submit. Each now short-circuits a genuine no-op before the bump — Add handlers by an explicit membership check (also fixing a latent duplicate-CollectionItem insert), scalar writers by ChangeTracker.HasChanges(). Corrects #269's framing: the Add*ToCollection family is not repository-mediated (IMediaCollectionRepository is read-only); each handler writes via its own dbContext, so the scanner's separate membership path is unaffected (a background scan does not rotate the editor ETag). Tests: CollectionEtagRotationTests + PlayoutScheduleFileEtagRotationTests (rotation, no-op-without-bump-or-rebuild, force-write-past-concurrent-bump), no-op guard proven non-vacuous by inverting the membership check. Docs: api-conventions §7a + decisions.md. No new status codes / no OpenAPI change (these endpoints take no If-Match, never 412). The #265 RFC-7232 If-Match parser refinement is a separate PR. fixes #269 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
86 lines
3.5 KiB
C#
86 lines
3.5 KiB
C#
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
|
|
UpdateExternalJsonPlayoutHandler : IRequestHandler<UpdateExternalJsonPlayout,
|
|
Either<BaseError, PlayoutNameViewModel>>
|
|
{
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly ChannelWriter<IBackgroundServiceRequest> _workerChannel;
|
|
|
|
public UpdateExternalJsonPlayoutHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> workerChannel)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_workerChannel = workerChannel;
|
|
}
|
|
|
|
public async Task<Either<BaseError, PlayoutNameViewModel>> Handle(
|
|
UpdateExternalJsonPlayout 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,
|
|
UpdateExternalJsonPlayout request,
|
|
Playout playout,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
playout.ScheduleFile = request.ScheduleFile;
|
|
|
|
// Rotate the playout ETag only when the schedule-file 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 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);
|
|
|
|
// 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 static Task<Validation<BaseError, Playout>> Validate(
|
|
TvContext dbContext,
|
|
UpdateExternalJsonPlayout request,
|
|
CancellationToken cancellationToken) =>
|
|
PlayoutMustExist(dbContext, request, cancellationToken);
|
|
|
|
private static Task<Validation<BaseError, Playout>> PlayoutMustExist(
|
|
TvContext dbContext,
|
|
UpdateExternalJsonPlayout 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."));
|
|
}
|