Fans the frozen ETag/If-Match/412 recipe (Block reference, #253) onto the Playlist aggregate: - ReplacePlaylistItems command carries ExpectedVersion; the handler runs CheckVersion as a standalone Either after validation (so a stale write survives as 412, not flattened to 422 by Apply/Join), bumps Version unconditionally before saving, and persists via SaveChangesWithConcurrencyGuard (EF concurrency-token backstop). - PlaylistViewModel carries Version; the items GET sets a strong ETag and the PUT parses If-Match, threads it into the command, and returns the refreshed ETag on success (400 on a malformed If-Match). - Sibling item-adding handlers (AddItemsToPlaylist, AddMovie/Episode/ Season/ShowToPlaylist) bump Version too, since they mutate the same editor-visible item list. - SPA: playlists.ts exposes getPlaylistItemsWithMeta and an If-Match-aware updatePlaylist; PlaylistEditor holds the ETag in a ref, round-trips it on save, and opens a "changed elsewhere" ConfirmDialog on 412 (mirrors BlockEditor). Tests: new ReplacePlaylistItemsHandlerConcurrencyTests (stale/match/ force-write/no-op-bump/racing-save), new PlaylistController tests (ETag on GET items, 400/412/thread-version/force-write on PUT), and a vitest 412-conflict-dialog test for PlaylistsScreen. dotnet test: 1304/1304 green. web: npm run typecheck clean, npm run build clean, vitest 664/664 green. Ref #253 PR2.
70 lines
2.8 KiB
C#
70 lines
2.8 KiB
C#
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.MediaCollections;
|
|
|
|
public class AddShowToPlaylistHandler(IDbContextFactory<TvContext> dbContextFactory)
|
|
: IRequestHandler<AddShowToPlaylist, Either<BaseError, Unit>>
|
|
{
|
|
public async Task<Either<BaseError, Unit>> Handle(
|
|
AddShowToPlaylist request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, Parameters> validation = await Validate(dbContext, request, cancellationToken);
|
|
return await validation.Apply(parameters => ApplyAddShowRequest(dbContext, parameters));
|
|
}
|
|
|
|
private static async Task<Unit> ApplyAddShowRequest(TvContext dbContext, Parameters parameters)
|
|
{
|
|
int index = parameters.Playlist.Items.Count > 0 ? parameters.Playlist.Items.Max(i => i.Index) + 1 : 0;
|
|
|
|
var playlistItem = new PlaylistItem
|
|
{
|
|
Index = index,
|
|
CollectionType = CollectionType.TelevisionShow,
|
|
MediaItemId = parameters.Show.Id,
|
|
PlaybackOrder = PlaybackOrder.Shuffle,
|
|
IncludeInProgramGuide = true
|
|
};
|
|
|
|
parameters.Playlist.Items.Add(playlistItem);
|
|
|
|
// Mutates the playlist's editor-visible item list, so bump the concurrency token (issue #253).
|
|
parameters.Playlist.Version++;
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
return Unit.Default;
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, Parameters>> Validate(
|
|
TvContext dbContext,
|
|
AddShowToPlaylist request,
|
|
CancellationToken cancellationToken) =>
|
|
(await PlaylistMustExist(dbContext, request, cancellationToken),
|
|
await ValidateShow(dbContext, request, cancellationToken))
|
|
.Apply((collection, episode) => new Parameters(collection, episode));
|
|
|
|
private static Task<Validation<BaseError, Playlist>> PlaylistMustExist(
|
|
TvContext dbContext,
|
|
AddShowToPlaylist request,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Playlists
|
|
.Include(c => c.Items)
|
|
.SelectOneAsync(c => c.Id, c => c.Id == request.PlaylistId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Playlist does not exist."));
|
|
|
|
private static Task<Validation<BaseError, Show>> ValidateShow(
|
|
TvContext dbContext,
|
|
AddShowToPlaylist request,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Shows
|
|
.SelectOneAsync(m => m.Id, e => e.Id == request.ShowId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Show does not exist"));
|
|
|
|
private sealed record Parameters(Playlist Playlist, Show Show);
|
|
}
|