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>
98 lines
4.4 KiB
C#
98 lines
4.4 KiB
C#
using System.Threading.Channels;
|
|
using ErsatzTV.Application.Playouts;
|
|
using ErsatzTV.Application.Search;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Interfaces.Repositories;
|
|
using ErsatzTV.Core.Scheduling;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.MediaCollections;
|
|
|
|
public class AddImageToCollectionHandler : IRequestHandler<AddImageToCollection, Either<BaseError, Unit>>
|
|
{
|
|
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly IMediaCollectionRepository _mediaCollectionRepository;
|
|
private readonly ChannelWriter<ISearchIndexBackgroundServiceRequest> _searchChannel;
|
|
|
|
public AddImageToCollectionHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
IMediaCollectionRepository mediaCollectionRepository,
|
|
ChannelWriter<IBackgroundServiceRequest> channel,
|
|
ChannelWriter<ISearchIndexBackgroundServiceRequest> searchChannel)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_mediaCollectionRepository = mediaCollectionRepository;
|
|
_channel = channel;
|
|
_searchChannel = searchChannel;
|
|
}
|
|
|
|
public async Task<Either<BaseError, Unit>> Handle(
|
|
AddImageToCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, Parameters> validation = await Validate(dbContext, request, cancellationToken);
|
|
return await validation.Apply(parameters => ApplyAddImageRequest(dbContext, parameters));
|
|
}
|
|
|
|
private async Task<Unit> ApplyAddImageRequest(TvContext dbContext, Parameters parameters)
|
|
{
|
|
// No-op on an idempotent re-add: don't rotate the ETag or fan out rebuilds for an item that is
|
|
// already a member (also avoids a duplicate CollectionItem row) — #269.
|
|
if (parameters.Collection.MediaItems.Any(mi => mi.Id == parameters.Image.Id))
|
|
{
|
|
return Unit.Default;
|
|
}
|
|
|
|
parameters.Collection.MediaItems.Add(parameters.Image);
|
|
|
|
// Rotate the collection ETag so an open custom-order editor's If-Match invalidates (#269);
|
|
// force-write past a concurrent replace-all Version bump — this add takes no If-Match, so a
|
|
// benign race must not 500 (#253/#269 §7a). Post-commit enqueues run on CancellationToken.None.
|
|
parameters.Collection.Version++;
|
|
await dbContext.SaveChangesForcingVersion(CancellationToken.None);
|
|
|
|
await _searchChannel.WriteAsync(new ReindexMediaItems([parameters.Image.Id]), CancellationToken.None);
|
|
|
|
// refresh all playouts that use this collection
|
|
foreach (int playoutId in await _mediaCollectionRepository
|
|
.PlayoutIdsUsingCollection(parameters.Collection.Id))
|
|
{
|
|
await _channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh), CancellationToken.None);
|
|
}
|
|
|
|
return Unit.Default;
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, Parameters>> Validate(
|
|
TvContext dbContext,
|
|
AddImageToCollection request,
|
|
CancellationToken cancellationToken) =>
|
|
(await CollectionMustExist(dbContext, request, cancellationToken),
|
|
await ValidateImage(dbContext, request, cancellationToken))
|
|
.Apply((collection, episode) => new Parameters(collection, episode));
|
|
|
|
private static Task<Validation<BaseError, Collection>> CollectionMustExist(
|
|
TvContext dbContext,
|
|
AddImageToCollection request,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Collections
|
|
.Include(c => c.MediaItems)
|
|
.SelectOneAsync(c => c.Id, c => c.Id == request.CollectionId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Collection does not exist."));
|
|
|
|
private static Task<Validation<BaseError, Image>> ValidateImage(
|
|
TvContext dbContext,
|
|
AddImageToCollection request,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Images
|
|
.SelectOneAsync(m => m.Id, e => e.Id == request.ImageId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Image does not exist"));
|
|
|
|
private sealed record Parameters(Collection Collection, Image Image);
|
|
}
|