Wire the frozen #253 ETag/If-Match/412 recipe onto ProgramSchedule / schedule-items, keeping the PR#258 positional in-place reconcile intact. Backend: - ReplaceProgramScheduleItems command gains Option<int> ExpectedVersion; ReplaceScheduleItemsRequest.ToCommand threads it. - Handler: standalone CheckVersion Either AFTER validation (so 412 isn't flattened to 422), unconditional Version++ before save, guarded save via SaveChangesWithConcurrencyGuard, and 412 propagated without running the post-save reload/enqueue. - ProgramScheduleViewModel + Mapper carry Version. - ScheduleController: GET /items emits ETag; PUT /items parses If-Match (malformed -> 400), threads ExpectedVersion, re-queries for the new ETag, and advertises 400/412. - Sibling config-writers (Add/Delete item, Update schedule) bump Version. Frontend: - schedules.ts: getScheduleItemsWithMeta + replaceScheduleItems(ifMatch) returning ResponseWithMeta. - SchedulesScreen: etagRef threaded through the #242 dirty-guard (set from load + every successful save); 412 opens a conflict ConfirmDialog whose Reload discards the draft and re-runs loadItems. Tests: handler concurrency suite (stale->412 no mutation + fill-group state untouched, match/absent success+bump, no-op still bumps, racing save->412); controller ETag/If-Match/412 cases; SchedulesScreen 412-conflict-dialog test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
111 lines
4.9 KiB
C#
111 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.Scheduling;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.ProgramSchedules;
|
|
|
|
public class UpdateProgramScheduleHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> channel)
|
|
:
|
|
IRequestHandler<UpdateProgramSchedule, Either<BaseError, UpdateProgramScheduleResult>>
|
|
{
|
|
public async Task<Either<BaseError, UpdateProgramScheduleResult>> Handle(
|
|
UpdateProgramSchedule request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Option<ProgramSchedule> maybeProgramSchedule =
|
|
await ProgramScheduleMustExist(dbContext, request, cancellationToken);
|
|
return await maybeProgramSchedule.Match(
|
|
Some: async programSchedule =>
|
|
{
|
|
Validation<BaseError, ProgramSchedule> validation = await Validate(dbContext, request, programSchedule, cancellationToken);
|
|
return await validation.Apply(ps => ApplyUpdateRequest(dbContext, ps, request));
|
|
},
|
|
None: () => Task.FromResult<Either<BaseError, UpdateProgramScheduleResult>>(
|
|
new NotFoundError("Schedule does not exist")));
|
|
}
|
|
|
|
private async Task<UpdateProgramScheduleResult> ApplyUpdateRequest(
|
|
TvContext dbContext,
|
|
ProgramSchedule programSchedule,
|
|
UpdateProgramSchedule request)
|
|
{
|
|
// we need to refresh playouts if the playback order or keep multi-episodes has been modified
|
|
bool needToRefreshPlayout =
|
|
programSchedule.KeepMultiPartEpisodesTogether != request.KeepMultiPartEpisodesTogether ||
|
|
programSchedule.TreatCollectionsAsShows != request.TreatCollectionsAsShows ||
|
|
programSchedule.ShuffleScheduleItems != request.ShuffleScheduleItems ||
|
|
programSchedule.RandomStartPoint != request.RandomStartPoint ||
|
|
programSchedule.FixedStartTimeBehavior != request.FixedStartTimeBehavior;
|
|
|
|
programSchedule.Name = request.Name;
|
|
programSchedule.KeepMultiPartEpisodesTogether = request.KeepMultiPartEpisodesTogether;
|
|
programSchedule.TreatCollectionsAsShows = programSchedule.KeepMultiPartEpisodesTogether &&
|
|
request.TreatCollectionsAsShows;
|
|
programSchedule.ShuffleScheduleItems = request.ShuffleScheduleItems;
|
|
programSchedule.RandomStartPoint = request.RandomStartPoint;
|
|
programSchedule.FixedStartTimeBehavior = request.FixedStartTimeBehavior;
|
|
|
|
// bump the optimistic-concurrency token so this config edit rotates other clients' ETags (#253)
|
|
programSchedule.Version++;
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
if (needToRefreshPlayout)
|
|
{
|
|
List<int> playoutIds = await dbContext.Playouts
|
|
.Filter(p => p.ProgramScheduleId == programSchedule.Id)
|
|
.Map(p => p.Id)
|
|
.ToListAsync();
|
|
|
|
foreach (int playoutId in playoutIds)
|
|
{
|
|
await channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh));
|
|
}
|
|
}
|
|
|
|
return new UpdateProgramScheduleResult(programSchedule.Id);
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, ProgramSchedule>> Validate(
|
|
TvContext dbContext,
|
|
UpdateProgramSchedule request,
|
|
ProgramSchedule programSchedule,
|
|
CancellationToken cancellationToken) =>
|
|
(await ValidateName(dbContext, request, cancellationToken)).Map(_ => programSchedule);
|
|
|
|
private static Task<Option<ProgramSchedule>> ProgramScheduleMustExist(
|
|
TvContext dbContext,
|
|
UpdateProgramSchedule request,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.ProgramSchedules
|
|
.SelectOneAsync(ps => ps.Id, ps => ps.Id == request.ProgramScheduleId, cancellationToken)
|
|
.Map(identity);
|
|
|
|
private static async Task<Validation<BaseError, string>> ValidateName(
|
|
TvContext dbContext,
|
|
UpdateProgramSchedule request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
Validation<BaseError, string> result1 = request.NotEmpty(c => c.Name)
|
|
.Bind(_ => request.NotLongerThan(50)(c => c.Name));
|
|
|
|
bool duplicateName = await dbContext.ProgramSchedules
|
|
.AnyAsync(c => c.Id != request.ProgramScheduleId && c.Name == request.Name, cancellationToken);
|
|
|
|
Validation<BaseError, Unit> result2 = duplicateName
|
|
? Fail<BaseError, Unit>("Schedule name must be unique")
|
|
: Success<BaseError, Unit>(Unit.Default);
|
|
|
|
return (result1, result2).Apply((_, _) => request.Name);
|
|
}
|
|
}
|