Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m27s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 10m0s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 35s
Independent adversarial review of PR #266 found two sibling handlers with the identical post-commit `_smartCollectionCache.Refresh(cancellationToken)` pattern that the sweep missed (only UpdateSmartCollection was caught). Same audit#22 F4 class: a late client-disconnect after the commit lands would throw and leave the in-memory smart-collection cache stale vs the committed DB. → CancellationToken.None. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
66 lines
2.6 KiB
C#
66 lines
2.6 KiB
C#
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Core.Interfaces.Search;
|
|
using ErsatzTV.Core.Search;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.MediaCollections;
|
|
|
|
public class DeleteSmartCollectionHandler : IRequestHandler<DeleteSmartCollection, Either<BaseError, Unit>>
|
|
{
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly ISearchTargets _searchTargets;
|
|
private readonly ISmartCollectionCache _smartCollectionCache;
|
|
|
|
public DeleteSmartCollectionHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ISearchTargets searchTargets,
|
|
ISmartCollectionCache smartCollectionCache)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_searchTargets = searchTargets;
|
|
_smartCollectionCache = smartCollectionCache;
|
|
}
|
|
|
|
public async Task<Either<BaseError, Unit>> Handle(
|
|
DeleteSmartCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Option<SmartCollection> maybeSmartCollection = await SmartCollectionMustExist(
|
|
dbContext,
|
|
request,
|
|
cancellationToken);
|
|
return await maybeSmartCollection.Match(
|
|
Some: smartCollection => DoDeletion(dbContext, smartCollection, cancellationToken).Map(Right<BaseError, Unit>),
|
|
None: () => Task.FromResult<Either<BaseError, Unit>>(
|
|
new NotFoundError($"SmartCollection {request.SmartCollectionId} does not exist.")));
|
|
}
|
|
|
|
private async Task<Unit> DoDeletion(
|
|
TvContext dbContext,
|
|
SmartCollection smartCollection,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
dbContext.SmartCollections.Remove(smartCollection);
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
_searchTargets.SearchTargetsChanged();
|
|
|
|
// post-commit side effect runs on CancellationToken.None so a late request cancellation
|
|
// can't abort it after the commit landed (#254)
|
|
await _smartCollectionCache.Refresh(CancellationToken.None);
|
|
return Unit.Default;
|
|
}
|
|
|
|
private static Task<Option<SmartCollection>> SmartCollectionMustExist(
|
|
TvContext dbContext,
|
|
DeleteSmartCollection request,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.SmartCollections
|
|
.SelectOneAsync(c => c.Id, c => c.Id == request.SmartCollectionId, cancellationToken)
|
|
.Map(identity);
|
|
}
|