CI's Formatting job failed: 19 touched files carried a BOM, which .editorconfig forbids (charset=utf-8). Pure encoding change — one byte per file, no semantic diff (verified: every hunk is `-namespace` -> `+namespace`). Self-inflicted. The patches that edited these legacy files wrote them back as utf-8-sig to "preserve the existing style", but the #311 fix-as-you-touch gate requires a file to be normalized when you touch it — that is the whole point of scoping the gate to changed files instead of reformatting the ~2500 legacy BOM files at once. dotnet format leaves the EF-generated Designer/snapshot files alone as generated code, and its verify skips them the same way, so they stay as ef emitted them. Two corrections to what I believed going in: - `dotnet format --include` does NOT no-op here. It reported `error CHARSET` for each file and exit 2, reproducing CI exactly, and fixed them in place. The note claiming otherwise is wrong for this invocation. - My first BOM check reported all files clean. The od pattern was wrong; reading the first three bytes directly found 19. A detector that can only say "ok" is worse than no detector. Core.Tests 565, ErsatzTV.Tests 1673, Architecture.Tests 5 — all passed. API artifacts still in sync. Refs #70 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
119 lines
4.9 KiB
C#
119 lines
4.9 KiB
C#
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Interfaces.Search;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using static ErsatzTV.Application.MediaCollections.Mapper;
|
|
|
|
namespace ErsatzTV.Application.MediaCollections;
|
|
|
|
public class CreateMultiCollectionHandler :
|
|
IRequestHandler<CreateMultiCollection, Either<BaseError, MultiCollectionViewModel>>
|
|
{
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly ISearchTargets _searchTargets;
|
|
|
|
public CreateMultiCollectionHandler(IDbContextFactory<TvContext> dbContextFactory, ISearchTargets searchTargets)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_searchTargets = searchTargets;
|
|
}
|
|
|
|
public async Task<Either<BaseError, MultiCollectionViewModel>> Handle(
|
|
CreateMultiCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, MultiCollection> validation = await Validate(dbContext, request);
|
|
return await validation.Apply(c => PersistCollection(dbContext, c));
|
|
}
|
|
|
|
private async Task<MultiCollectionViewModel> PersistCollection(
|
|
TvContext dbContext,
|
|
MultiCollection multiCollection)
|
|
{
|
|
await dbContext.MultiCollections.AddAsync(multiCollection);
|
|
await dbContext.SaveChangesAsync();
|
|
_searchTargets.SearchTargetsChanged();
|
|
await dbContext.Entry(multiCollection)
|
|
.Collection(c => c.MultiCollectionItems)
|
|
.Query()
|
|
.Include(i => i.Collection)
|
|
.LoadAsync();
|
|
await dbContext.Entry(multiCollection)
|
|
.Collection(c => c.MultiCollectionSmartItems)
|
|
.Query()
|
|
.Include(i => i.SmartCollection)
|
|
.LoadAsync();
|
|
return ProjectToViewModel(multiCollection);
|
|
}
|
|
|
|
private static Task<Validation<BaseError, MultiCollection>> Validate(
|
|
TvContext dbContext,
|
|
CreateMultiCollection request) =>
|
|
ValidateName(dbContext, request)
|
|
.BindT(name => ValidateWeights(request).Map(_ => name))
|
|
.MapT(name => new MultiCollection
|
|
{
|
|
Name = name,
|
|
MultiCollectionItems = request.Items.Bind(i =>
|
|
{
|
|
if (i.CollectionId.HasValue)
|
|
{
|
|
return Some(
|
|
new MultiCollectionItem
|
|
{
|
|
CollectionId = i.CollectionId.Value,
|
|
ScheduleAsGroup = i.ScheduleAsGroup,
|
|
PlaybackOrder = i.PlaybackOrder,
|
|
Weight = i.Weight
|
|
});
|
|
}
|
|
|
|
return Option<MultiCollectionItem>.None;
|
|
})
|
|
.ToList(),
|
|
MultiCollectionSmartItems = request.Items.Bind(i =>
|
|
{
|
|
if (i.SmartCollectionId.HasValue)
|
|
{
|
|
return Some(
|
|
new MultiCollectionSmartItem
|
|
{
|
|
SmartCollectionId = i.SmartCollectionId.Value,
|
|
ScheduleAsGroup = i.ScheduleAsGroup,
|
|
PlaybackOrder = i.PlaybackOrder,
|
|
Weight = i.Weight
|
|
});
|
|
}
|
|
|
|
return Option<MultiCollectionSmartItem>.None;
|
|
})
|
|
.ToList()
|
|
});
|
|
|
|
// Bounds are shared with the update path so the two cannot drift: an out-of-range weight silently
|
|
// removes the source from the rotation (0) or overflows the rotation arithmetic (huge). See #70.
|
|
private static Validation<BaseError, Unit> ValidateWeights(CreateMultiCollection request) =>
|
|
request.Items.All(i => MultiCollectionItemWeight.IsValid(i.Weight))
|
|
? Unit.Default
|
|
: BaseError.New(MultiCollectionItemWeight.ValidationMessage);
|
|
|
|
private static async Task<Validation<BaseError, string>> ValidateName(
|
|
TvContext dbContext,
|
|
CreateMultiCollection createMultiCollection)
|
|
{
|
|
Validation<BaseError, string> result1 = createMultiCollection.NotEmpty(c => c.Name)
|
|
.Bind(_ => createMultiCollection.NotLongerThan(50)(c => c.Name));
|
|
|
|
bool duplicateName = await dbContext.MultiCollections
|
|
.AnyAsync(c => c.Name == createMultiCollection.Name);
|
|
|
|
Validation<BaseError, Unit> result2 = duplicateName
|
|
? Fail<BaseError, Unit>("MultiCollection name must be unique")
|
|
: Success<BaseError, Unit>(Unit.Default);
|
|
|
|
return (result1, result2).Apply((_, _) => createMultiCollection.Name);
|
|
}
|
|
}
|