Files
ersatztv/ErsatzTV.Application/MediaCollections/Commands/UpdateCollectionHandler.cs
T
timothyandtimothy 6462c36983
Build ErsatzTV Image / CI image pin matches docker/ci (push) Has been skipped
Build ErsatzTV Image / Docs update reminder (push) Has been skipped
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (push) Has been skipped
Build ErsatzTV Image / decisions.md append-only (push) Has been skipped
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (push) Has been skipped
Build ErsatzTV Image / Build & test (.NET) (push) Has been cancelled
Build ErsatzTV Image / Build & push image (amd64) (push) Has been cancelled
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (push) Has been cancelled
Build ErsatzTV Image / Functional E2E (curl contracts) (push) Has been cancelled
fix(310): strip UTF-8 BOM from the legacy .cs files #269 touched (#449)
Co-authored-by: Timothy <timothy.look@gmail.com>
Co-committed-by: Timothy <timothy.look@gmail.com>
2026-07-18 16:15:35 +00:00

120 lines
4.9 KiB
C#

using System.Threading.Channels;
using ErsatzTV.Application.Playouts;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Interfaces.Search;
using ErsatzTV.Core.Scheduling;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.MediaCollections;
public class UpdateCollectionHandler : IRequestHandler<UpdateCollection, Either<BaseError, Unit>>
{
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
private readonly IDbContextFactory<TvContext> _dbContextFactory;
private readonly IMediaCollectionRepository _mediaCollectionRepository;
private readonly ISearchTargets _searchTargets;
public UpdateCollectionHandler(
IDbContextFactory<TvContext> dbContextFactory,
IMediaCollectionRepository mediaCollectionRepository,
ChannelWriter<IBackgroundServiceRequest> channel,
ISearchTargets searchTargets)
{
_dbContextFactory = dbContextFactory;
_mediaCollectionRepository = mediaCollectionRepository;
_channel = channel;
_searchTargets = searchTargets;
}
public async Task<Either<BaseError, Unit>> Handle(
UpdateCollection request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<Collection> maybeCollection = await CollectionMustExist(dbContext, request, cancellationToken);
return await maybeCollection.Match(
Some: async collection =>
{
Validation<BaseError, Collection> validation = await Validate(dbContext, request, collection);
return await validation.Apply(c => ApplyUpdateRequest(dbContext, c, request, cancellationToken));
},
None: () => Task.FromResult<Either<BaseError, Unit>>(
new NotFoundError($"Collection {request.CollectionId} does not exist.")));
}
private async Task<Unit> ApplyUpdateRequest(
TvContext dbContext,
Collection c,
UpdateCollection request,
CancellationToken cancellationToken)
{
c.Name = request.Name;
foreach (bool useCustomPlaybackOrder in request.UseCustomPlaybackOrder)
{
c.UseCustomPlaybackOrder = useCustomPlaybackOrder;
}
// Only rotate the ETag when the name/flag actually changed — a no-op re-submit must not bump the
// Version (spurious rebuilds / editor invalidation) — #269. When it did change, force-write past a
// concurrent bump from the custom-order editor (this writer takes no If-Match, so a benign race
// must not 500) — #253/#269 §7a.
if (dbContext.ChangeTracker.HasChanges())
{
c.Version++;
await dbContext.SaveChangesForcingVersion(cancellationToken);
if (request.UseCustomPlaybackOrder.IsSome)
{
// post-commit side effect runs on CancellationToken.None so a late request cancellation
// can't abort it after the commit landed (#254)
// refresh all playouts that use this collection
foreach (int playoutId in await _mediaCollectionRepository.PlayoutIdsUsingCollection(
request.CollectionId))
{
await _channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh), CancellationToken.None);
}
}
}
_searchTargets.SearchTargetsChanged();
return Unit.Default;
}
private static async Task<Validation<BaseError, Collection>> Validate(
TvContext dbContext,
UpdateCollection request,
Collection collection) =>
(await ValidateName(dbContext, request)).Map(_ => collection);
private static Task<Option<Collection>> CollectionMustExist(
TvContext dbContext,
UpdateCollection updateCollection,
CancellationToken cancellationToken) =>
dbContext.Collections
.SelectOneAsync(c => c.Id, c => c.Id == updateCollection.CollectionId, cancellationToken)
.Map(identity);
private static async Task<Validation<BaseError, string>> ValidateName(
TvContext dbContext,
UpdateCollection updateCollection)
{
Validation<BaseError, string> result1 = updateCollection.NotEmpty(c => c.Name)
.Bind(_ => updateCollection.NotLongerThan(50)(c => c.Name));
bool duplicateName = await dbContext.Collections
.AnyAsync(c => c.Id != updateCollection.CollectionId && c.Name == updateCollection.Name);
Validation<BaseError, Unit> result2 = duplicateName
? Fail<BaseError, Unit>("Collection name must be unique")
: Success<BaseError, Unit>(Unit.Default);
return (result1, result2).Apply((_, _) => updateCollection.Name);
}
}