Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 6s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 7s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Failing after 2m30s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 4m23s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m44s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m47s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Independent review fix commit (cold fork MERGEABLE-WITH-NITS + Codex BLOCKED, 2 Highs):
- Codex H1: a comma (0x2C) is a valid etagc and can appear INSIDE a quoted opaque-tag
("3,5" is ONE tag). The old Split(',') broke it into two malformed tokens → 400. Replaced
with a quote-aware position scanner that treats a comma as a separator only outside the
quotes; "3,5" is now one valid non-canonical tag → 412.
- Codex H2: RFC 7230 OWS is SP/HTAB only. string.Trim() also strips NBSP and other Unicode
whitespace, letting " * " masquerade as the "*" force-write escape. Trim only
(' ', '\t'); such input is now Malformed → 400.
- Fork nit: corrected the canonical-guard comment (interior-whitespace tags are rejected by
IsEtagc, not NumberStyles.None).
- CI Formatting gate: de-BOM the 8 touched legacy Application .cs (charset=utf-8, #311/#310).
- Tests: added comma-in-tag ("3,5", "x,y","3"), empty-element tolerance, NBSP-not-OWS,
trailing-junk, lowercase-weak, wildcard-in-list cases. Full ErsatzTV.Tests green (1556).
Refs #253 #197
114 lines
5.1 KiB
C#
114 lines
5.1 KiB
C#
using System.Threading.Channels;
|
|
using ErsatzTV.Application.Playouts;
|
|
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 UpdateRerunCollectionHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
IMediaCollectionRepository mediaCollectionRepository,
|
|
ChannelWriter<IBackgroundServiceRequest> channel)
|
|
: IRequestHandler<UpdateRerunCollection, Either<BaseError, Unit>>
|
|
{
|
|
public async Task<Either<BaseError, Unit>> Handle(
|
|
UpdateRerunCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, RerunCollection> validation = await Validate(dbContext, request, cancellationToken);
|
|
|
|
// Optimistic-concurrency check as a standalone Either AFTER validation, never via Apply (which
|
|
// Join()s the error Seq and would flatten PreconditionFailedError to a 422) — issue #253 §7a.
|
|
Either<BaseError, RerunCollection> validated = LanguageExtensions.ToEither(validation)
|
|
.Bind(c => c.CheckVersion(request.ExpectedVersions));
|
|
|
|
return await validated.Match(
|
|
Right: c => ApplyUpdateRequest(dbContext, c, request, cancellationToken),
|
|
Left: error => Task.FromResult<Either<BaseError, Unit>>(error));
|
|
}
|
|
|
|
private async Task<Either<BaseError, Unit>> ApplyUpdateRequest(
|
|
TvContext dbContext,
|
|
RerunCollection c,
|
|
UpdateRerunCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
c.Name = request.Name;
|
|
c.CollectionType = request.CollectionType;
|
|
c.CollectionId = request.Collection?.Id;
|
|
c.MultiCollectionId = request.MultiCollection?.Id;
|
|
c.SmartCollectionId = request.SmartCollection?.Id;
|
|
c.MediaItemId = request.MediaItem?.MediaItemId;
|
|
c.FirstRunPlaybackOrder = request.FirstRunPlaybackOrder;
|
|
c.RerunPlaybackOrder = request.RerunPlaybackOrder;
|
|
|
|
// Unconditional bump (issue #253 §7a / M1) then guarded save, which maps a racing
|
|
// DbUpdateConcurrencyException to PreconditionFailedError (→ 412).
|
|
c.Version++;
|
|
Either<BaseError, Unit> saveResult = await dbContext.SaveChangesWithConcurrencyGuard(cancellationToken);
|
|
if (saveResult.IsLeft)
|
|
{
|
|
return saveResult;
|
|
}
|
|
|
|
// Refresh all playouts that use this rerun collection. The old `SaveChangesAsync() > 0` gate is
|
|
// always true once the version bumps unconditionally (M2), so run the refresh on any successful
|
|
// save. Post-commit enqueue on CancellationToken.None so a late cancellation can't drop the
|
|
// rebuild after the commit landed (#254).
|
|
foreach (int playoutId in await mediaCollectionRepository.PlayoutIdsUsingRerunCollection(
|
|
request.RerunCollectionId))
|
|
{
|
|
await channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh), CancellationToken.None);
|
|
}
|
|
|
|
return Unit.Default;
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, RerunCollection>> Validate(
|
|
TvContext dbContext,
|
|
UpdateRerunCollection request,
|
|
CancellationToken cancellationToken) =>
|
|
(await RerunCollectionMustExist(dbContext, request, cancellationToken),
|
|
await ValidateName(dbContext, request),
|
|
await RerunCollectionSelectionValidation.SelectedEntityMustExist(
|
|
dbContext,
|
|
request.CollectionType,
|
|
request.Collection?.Id,
|
|
request.MultiCollection?.Id,
|
|
request.SmartCollection?.Id,
|
|
request.MediaItem?.MediaItemId,
|
|
cancellationToken))
|
|
.Apply((collectionToUpdate, _, _) => collectionToUpdate);
|
|
|
|
private static Task<Validation<BaseError, RerunCollection>> RerunCollectionMustExist(
|
|
TvContext dbContext,
|
|
UpdateRerunCollection updateCollection,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.RerunCollections
|
|
.SelectOneAsync(c => c.Id, c => c.Id == updateCollection.RerunCollectionId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Rerun collection does not exist."));
|
|
|
|
private static async Task<Validation<BaseError, string>> ValidateName(
|
|
TvContext dbContext,
|
|
UpdateRerunCollection updateCollection)
|
|
{
|
|
Validation<BaseError, string> result1 = updateCollection.NotEmpty(c => c.Name)
|
|
.Bind(_ => updateCollection.NotLongerThan(50)(c => c.Name));
|
|
|
|
bool duplicateName = await dbContext.RerunCollections
|
|
.AnyAsync(c => c.Id != updateCollection.RerunCollectionId && c.Name == updateCollection.Name);
|
|
|
|
Validation<BaseError, Unit> result2 = duplicateName
|
|
? Fail<BaseError, Unit>("Rerun collection name must be unique")
|
|
: Success<BaseError, Unit>(Unit.Default);
|
|
|
|
return (result1, result2).Apply((_, _) => updateCollection.Name);
|
|
}
|
|
}
|