Clears the still-live findings from #172 (verified against main; #2/#4/#7 and the auth/search/Trakt tail were already deliberate-documented or fixed since 2026-07-07). - Null/empty Name → 500 (10 create/replace handlers). Block/Template/DecoTemplate/Deco Create+Replace/Update + UpdateFFmpegProfile did `request.Name.Length > 50` on a client-nullable string → unhandled NullReferenceException → HTTP 500 (no global exception filter). Now `string.IsNullOrWhiteSpace(request.Name) || .Length > 50` → 422; also rejects empty/whitespace names, matching the group-create handlers' NotEmpty behavior. CreatePlaylist coalesces null→"" at the DTO so it was an empty-name persist, not a 500; guarded the same way. - ReplaceTemplateItems overlap validation iterated with an `item == otherItem` record value-equality skip, so two exact-duplicate items were value-equal and bypassed the intersection check (both persisted). Now index-based (i != j) so duplicates register as a self-intersection and are rejected 422. - Trimmed the unreachable 404 ProducesResponseType from POST /api/blocks/groups and POST /api/templates/groups (a create has no parent lookup that can 404); v1.json regenerated. - Regression tests: all 10 name-guard paths + the duplicate-items path (19 cases). - Docs: decisions.md entry + api-conventions.md §3b null-safe-validation bullet. fixes #172 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
71 lines
2.7 KiB
C#
71 lines
2.7 KiB
C#
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain.Scheduling;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.Scheduling;
|
|
|
|
public class CreateDecoHandler(IDbContextFactory<TvContext> dbContextFactory)
|
|
: IRequestHandler<CreateDeco, Either<BaseError, DecoViewModel>>
|
|
{
|
|
public async Task<Either<BaseError, DecoViewModel>> Handle(
|
|
CreateDeco request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, Deco> validation = await Validate(dbContext, request);
|
|
return await validation.Apply(profile => PersistDeco(dbContext, profile));
|
|
}
|
|
|
|
private static async Task<DecoViewModel> PersistDeco(TvContext dbContext, Deco deco)
|
|
{
|
|
await dbContext.Decos.AddAsync(deco);
|
|
await dbContext.SaveChangesAsync();
|
|
await dbContext.Entry(deco).Reference(d => d.DecoGroup).LoadAsync();
|
|
return Mapper.ProjectToViewModel(deco);
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, Deco>> Validate(TvContext dbContext, CreateDeco request)
|
|
{
|
|
Validation<BaseError, Unit> decoGroupValidation = await ValidateDecoGroupExists(dbContext, request);
|
|
Validation<BaseError, string> nameValidation = await ValidateDecoName(dbContext, request);
|
|
|
|
return (decoGroupValidation, nameValidation).Apply((_, name) => new Deco
|
|
{
|
|
DecoGroupId = request.DecoGroupId,
|
|
Name = name,
|
|
BreakContent = [],
|
|
DecoWatermarks = [],
|
|
DecoGraphicsElements = []
|
|
});
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, Unit>> ValidateDecoGroupExists(
|
|
TvContext dbContext,
|
|
CreateDeco request)
|
|
{
|
|
bool decoGroupExists = await dbContext.DecoGroups.AnyAsync(dg => dg.Id == request.DecoGroupId);
|
|
|
|
return decoGroupExists
|
|
? Success<BaseError, Unit>(Unit.Default)
|
|
: BaseError.New("Deco group does not exist");
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, string>> ValidateDecoName(
|
|
TvContext dbContext,
|
|
CreateDeco request)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(request.Name) || request.Name.Length > 50)
|
|
{
|
|
return BaseError.New($"Deco name \"{request.Name}\" is invalid");
|
|
}
|
|
|
|
bool duplicateName = await dbContext.Decos
|
|
.AnyAsync(r => r.DecoGroupId == request.DecoGroupId && r.Name == request.Name);
|
|
|
|
return duplicateName
|
|
? BaseError.New($"A deco named \"{request.Name}\" already exists in that deco group")
|
|
: Success<BaseError, string>(request.Name);
|
|
}
|
|
}
|