Auto-tune channels can now carry per-content-source rotation weights (weighted
round-robin, e.g. 3x Show A / 1x Show B) and query corrections (exclude /
add-untagged), supplied at bulk-create time via an optional
`sources: [{sourceId, weight, excluded}]` on each AutoTunedChannelRequest.
Design (Option A, reuse #70): when a source is customized the channel is backed
by a system-owned MultiCollection of per-source SmartCollections carrying the
weights, with PlaybackOrder.WeightedShuffle -- the exact path
WeightedShuffleCollectionEnumerator already consumes. All-default weights keep
the #69 single-SmartCollection fair-share shape.
- Discriminators: TV -> live show_title:"X" (episodes carry no parent-show id in
the index); movies -> stable id:{mediaItemId}.
- Materialization is axis-dependent: TV materializes every base show individually
(un-weighted shows keep per-show fair-share) + a live remainder at weight 1;
MovieGenre materializes only touched movies + one count-weighted remainder.
- Remainder = (base) AND NOT (materialized union excluded) -- a partition.
- New nullable OwnedByChannelId on SmartCollection + MultiCollection
(dual-provider migration); owned rows are hidden from the collection lists and
cascade-cleaned on channel delete.
Tests: AutoTuneAxisMap query/partition units; DB-backed weighted-path handler
tests (TV materialize-all, movie count-remainder, exclusion, no-customization
fallback); delete-cleanup. Docs: decisions.md, domain-model.md, api-conventions.md;
OpenAPI trio regenerated.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
75 lines
3.0 KiB
C#
75 lines
3.0 KiB
C#
using System.IO.Abstractions;
|
|
using System.Threading.Channels;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Core.Interfaces.Search;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Channel = ErsatzTV.Core.Domain.Channel;
|
|
|
|
namespace ErsatzTV.Application.Channels;
|
|
|
|
public class BulkDeleteChannelsHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> workerChannel,
|
|
IFileSystem fileSystem,
|
|
ISearchTargets searchTargets)
|
|
: IRequestHandler<BulkDeleteChannels, Either<BaseError, Unit>>
|
|
{
|
|
public async Task<Either<BaseError, Unit>> Handle(
|
|
BulkDeleteChannels request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (request.ChannelIds.Count == 0)
|
|
{
|
|
return Left<BaseError, Unit>(BaseError.New("At least one channel id is required"));
|
|
}
|
|
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
|
|
|
List<int> channelIds = request.ChannelIds.Distinct().ToList();
|
|
List<Channel> channels = await dbContext.Channels
|
|
.Where(c => channelIds.Contains(c.Id))
|
|
.ToListAsync(cancellationToken);
|
|
|
|
if (channels.Count != channelIds.Count)
|
|
{
|
|
var found = channels.Select(c => c.Id).ToHashSet();
|
|
int missingId = channelIds.First(id => !found.Contains(id));
|
|
return Left<BaseError, Unit>(new NotFoundError($"Channel {missingId} does not exist."));
|
|
}
|
|
|
|
dbContext.Channels.RemoveRange(channels);
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
// Clean up the system-owned weighted-auto-tune artifacts these channels created (#425), inside the
|
|
// same transaction — see DeleteChannelHandler for the cascade rationale.
|
|
await dbContext.MultiCollections
|
|
.Where(mc => mc.OwnedByChannelId != null && channelIds.Contains(mc.OwnedByChannelId.Value))
|
|
.ExecuteDeleteAsync(cancellationToken);
|
|
await dbContext.SmartCollections
|
|
.Where(sc => sc.OwnedByChannelId != null && channelIds.Contains(sc.OwnedByChannelId.Value))
|
|
.ExecuteDeleteAsync(cancellationToken);
|
|
|
|
await transaction.CommitAsync(cancellationToken);
|
|
|
|
searchTargets.SearchTargetsChanged();
|
|
|
|
foreach (Channel channel in channels)
|
|
{
|
|
string cacheFile = Path.Combine(FileSystemLayout.ChannelGuideCacheFolder, $"{channel.Number}.xml");
|
|
if (fileSystem.File.Exists(cacheFile))
|
|
{
|
|
fileSystem.File.Delete(cacheFile);
|
|
}
|
|
}
|
|
|
|
// 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 RefreshChannelList(), CancellationToken.None);
|
|
|
|
return Right<BaseError, Unit>(Unit.Default);
|
|
}
|
|
}
|