fix(api): rework from-lineup to generated playlist design + review fixes (#63)
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 4m23s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m54s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped

Adversarial review found the previous multi-flood design non-viable:
PlayoutModeSchedulerFlood never yields to a following Dynamic-start
schedule item, so only the first item ever played, and grouping
media items into a Collection silently dropped the requested order.

Redesign:
- Single-item lineup: one ProgramScheduleItemFlood referencing the
  target directly (media item / collection / smart / multi / rerun /
  playlist); no generated collection or playlist. Response PlaylistId
  is null.
- Multi-item lineup (>= 2): one generated IsSystem Playlist in a
  get-or-created IsSystem PlaylistGroup ("Channel Lineups"), one
  PlaylistItem per entry in lineup order with PlayAll=true, referenced
  by a single Flood schedule item. Rerun collections and playlists are
  rejected (422) in multi lineups (PlaylistItem/CollectionKey lack the
  fields to enumerate them).

Review fixes:
- Normalize + strict-validate MediaType<->CollectionType pairs once up
  front (422 on mismatch / wrong id / not exactly one id).
- Reject MultiCollection with non-Shuffle order (mirrors
  PlayoutModeMustBeValid), Mirror playout source, all via 422.
- OnDemand parity: queue TimeShiftOnDemandPlayout post-commit.
- De-collide generated ProgramSchedule and Playlist names against their
  unique indexes instead of leaking a UNIQUE-constraint DbUpdateException.
- Generic 422 on save failure + ILogger; AnyAsync existence checks;
  Either/Validation unwrap via Match; XML doc on request DTO + endpoint.
- Response model: ChannelId, PlaylistId (nullable), ProgramScheduleId,
  PlayoutId (CollectionId removed). Regenerated OpenAPI v1.json + v1.d.ts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-06 21:09:00 +02:00
co-authored by Claude Fable 5
parent 6857a0d191
commit cf36c30997
7 changed files with 922 additions and 225 deletions
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading.Channels;
using ErsatzTV.Application.Playouts;
@@ -12,6 +12,7 @@ using ErsatzTV.Core.Interfaces.Search;
using ErsatzTV.Core.Scheduling;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Channel = ErsatzTV.Core.Domain.Channel;
namespace ErsatzTV.Application.Channels;
@@ -19,29 +20,40 @@ namespace ErsatzTV.Application.Channels;
public class CreateChannelFromLineupHandler(
ChannelWriter<IBackgroundServiceRequest> workerChannel,
IDbContextFactory<TvContext> dbContextFactory,
ISearchTargets searchTargets)
ISearchTargets searchTargets,
ILogger<CreateChannelFromLineupHandler> logger)
: IRequestHandler<CreateChannelFromLineup, Either<BaseError, CreateChannelFromLineupResponseModel>>
{
// The single system playlist group that holds every generated channel-lineup playlist.
// Matches the Trakt "Trakt Lists" precedent (DbInitializer + delete guards on IsSystem).
private const string SystemPlaylistGroupName = "Channel Lineups";
public async Task<Either<BaseError, CreateChannelFromLineupResponseModel>> Handle(
CreateChannelFromLineup request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
PreparedCreate prepared;
Either<BaseError, PreparedCreate> validation = await Validate(dbContext, request, cancellationToken);
foreach (BaseError error in validation.LeftToSeq())
{
return error;
}
prepared = validation.IfLeft(() => throw new InvalidOperationException("Validation failed without error"));
return await validation.Match(
Left: error => Task.FromResult<Either<BaseError, CreateChannelFromLineupResponseModel>>(error),
Right: prepared => PersistAndDispatch(dbContext, prepared, cancellationToken));
}
private async Task<Either<BaseError, CreateChannelFromLineupResponseModel>> PersistAndDispatch(
TvContext dbContext,
PreparedCreate prepared,
CancellationToken cancellationToken)
{
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
try
{
dbContext.Channels.Add(prepared.Channel);
dbContext.Collections.Add(prepared.Collection);
if (prepared.Playlist is not null)
{
dbContext.Playlists.Add(prepared.Playlist);
}
dbContext.ProgramSchedules.Add(prepared.ProgramSchedule);
dbContext.Playouts.Add(prepared.Playout);
@@ -51,16 +63,28 @@ public class CreateChannelFromLineupHandler(
catch (DbUpdateException ex)
{
await transaction.RollbackAsync(cancellationToken);
return BaseError.New($"Unable to create channel from lineup: {ex.GetBaseException().Message}");
logger.LogError(ex, "Failed to persist channel created from lineup");
return BaseError.New("Unable to create channel from lineup");
}
searchTargets.SearchTargetsChanged();
await workerChannel.WriteAsync(new BuildPlayout(prepared.Playout.Id, PlayoutBuildMode.Reset), cancellationToken);
await workerChannel.WriteAsync(
new BuildPlayout(prepared.Playout.Id, PlayoutBuildMode.Reset),
cancellationToken);
// Mirror CreateClassicPlayoutHandler: on-demand playouts must be time-shifted to "now" after build.
if (prepared.Channel.PlayoutMode is ChannelPlayoutMode.OnDemand)
{
await workerChannel.WriteAsync(
new TimeShiftOnDemandPlayout(prepared.Playout.Id, DateTimeOffset.Now, false),
cancellationToken);
}
await workerChannel.WriteAsync(new RefreshChannelList(), cancellationToken);
return new CreateChannelFromLineupResponseModel(
prepared.Channel.Id,
prepared.Collection.Id,
prepared.Playlist?.Id,
prepared.ProgramSchedule.Id,
prepared.Playout.Id);
}
@@ -126,23 +150,71 @@ public class CreateChannelFromLineupHandler(
return new NotFoundError($"Channel template {request.TemplateId} does not exist.");
}
Either<BaseError, Unit> referenceValidation = await ValidateReferences(
dbContext,
template,
advanced,
request.Lineup,
cancellationToken);
foreach (BaseError error in referenceValidation.LeftToSeq())
{
return error;
}
int ffmpegProfileId = advanced.FFmpegProfileId ?? template.FFmpegProfileId;
int? fallbackFillerId = advanced.FallbackFillerId ?? template.FallbackFillerId;
int? preRollFillerId = advanced.PreRollFillerId ?? template.PreRollFillerId;
int? midRollFillerId = advanced.MidRollFillerId ?? template.MidRollFillerId;
int? postRollFillerId = advanced.PostRollFillerId ?? template.PostRollFillerId;
PlaybackOrder playbackOrder = advanced.PlaybackOrder ?? PlaybackOrder.Chronological;
ChannelPlayoutSource playoutSource = advanced.PlayoutSource ?? template.PlayoutSource;
// Mirror channels need special MirrorSourceChannelId plumbing (see CreateChannelHandler);
// this endpoint only builds generated playouts.
if (playoutSource is ChannelPlayoutSource.Mirror)
{
return BaseError.New("Mirror playout source is not supported by this endpoint");
}
Either<BaseError, Unit> referenceValidation = await ValidateReferences(
dbContext,
advanced,
template,
cancellationToken);
foreach (BaseError error in referenceValidation.LeftToSeq())
{
return error;
}
// Normalize + validate every lineup entry once, so validation and build see the same data.
var normalized = new List<NormalizedLineupItem>();
for (int i = 0; i < request.Lineup.Count; i++)
{
Either<BaseError, NormalizedLineupItem> itemValidation =
await NormalizeLineupItem(dbContext, request.Lineup[i], i, cancellationToken);
foreach (BaseError error in itemValidation.LeftToSeq())
{
return error;
}
foreach (NormalizedLineupItem item in itemValidation.RightToSeq())
{
normalized.Add(item);
}
}
bool multiItem = normalized.Count >= 2;
// MultiCollection entries only support Shuffle / ShuffleInOrder (mirrors PlayoutModeMustBeValid).
if (normalized.Any(i => i.CollectionType is CollectionType.MultiCollection) &&
playbackOrder is not (PlaybackOrder.Shuffle or PlaybackOrder.ShuffleInOrder))
{
return BaseError.New($"Invalid playback order for multi collection: '{playbackOrder}'");
}
if (multiItem)
{
// The generated playlist cannot express rerun collections or nested playlists
// (PlaylistItem + CollectionKey.ForPlaylistItem lack those fields).
for (int i = 0; i < normalized.Count; i++)
{
if (normalized[i].CollectionType is CollectionType.RerunFirstRun or CollectionType.Playlist)
{
return BaseError.New(
$"lineup[{normalized[i].Index}]: rerun collections and playlists are only " +
"supported as a single-item lineup");
}
}
}
Channel channel = BuildChannel(
request,
@@ -155,19 +227,43 @@ public class CreateChannelFromLineupHandler(
ffmpegProfileId,
fallbackFillerId);
Collection collection = BuildCollection(number, name, request.Lineup);
ProgramSchedule schedule = BuildProgramSchedule(number, name, template, advanced);
schedule.Items = BuildScheduleItems(
collection,
request.Lineup,
string scheduleName = await DeCollideName(
GeneratedName(number, name, "Schedule"),
(candidate, ct) => dbContext.ProgramSchedules.AnyAsync(ps => ps.Name == candidate, ct),
cancellationToken);
ProgramSchedule schedule = BuildProgramSchedule(scheduleName, template, advanced);
Playlist playlist = null;
ProgramScheduleItemFlood floodItem = BuildFloodBase(
playbackOrder,
template,
advanced,
template,
fallbackFillerId,
preRollFillerId,
midRollFillerId,
postRollFillerId);
if (multiItem)
{
playlist = await BuildPlaylist(dbContext, number, name, normalized, playbackOrder, cancellationToken);
floodItem.CollectionType = CollectionType.Playlist;
floodItem.Playlist = playlist;
}
else
{
NormalizedLineupItem only = normalized[0];
floodItem.CollectionType = only.CollectionType;
floodItem.CollectionId = only.CollectionId;
floodItem.MultiCollectionId = only.MultiCollectionId;
floodItem.SmartCollectionId = only.SmartCollectionId;
floodItem.RerunCollectionId = only.RerunCollectionId;
floodItem.MediaItemId = only.MediaItemId;
floodItem.PlaylistId = only.PlaylistId;
}
schedule.Items = [floodItem];
var playout = new Playout
{
Channel = channel,
@@ -175,7 +271,60 @@ public class CreateChannelFromLineupHandler(
ScheduleKind = PlayoutScheduleKind.Classic
};
return new PreparedCreate(channel, collection, schedule, playout);
return new PreparedCreate(channel, playlist, schedule, playout);
}
private static async Task<Playlist> BuildPlaylist(
TvContext dbContext,
string channelNumber,
string channelName,
List<NormalizedLineupItem> lineup,
PlaybackOrder playbackOrder,
CancellationToken cancellationToken)
{
// Reuse the single system playlist group, creating it on first use.
PlaylistGroup playlistGroup = await dbContext.PlaylistGroups
.FirstOrDefaultAsync(pg => pg.IsSystem && pg.Name == SystemPlaylistGroupName, cancellationToken);
playlistGroup ??= new PlaylistGroup { Name = SystemPlaylistGroupName, IsSystem = true };
string playlistName = await DeCollideName(
GeneratedName(channelNumber, channelName, "Lineup"),
(candidate, ct) => dbContext.Playlists.AnyAsync(
p => p.PlaylistGroupId == playlistGroup.Id && p.Name == candidate,
ct),
cancellationToken);
var playlist = new Playlist
{
Name = playlistName,
IsSystem = true,
PlaylistGroup = playlistGroup,
PlaylistGroupId = playlistGroup.Id,
Items = []
};
int index = 1;
foreach (NormalizedLineupItem item in lineup)
{
playlist.Items.Add(new PlaylistItem
{
Playlist = playlist,
Index = index++,
CollectionType = item.CollectionType,
CollectionId = item.CollectionId,
MultiCollectionId = item.MultiCollectionId,
SmartCollectionId = item.SmartCollectionId,
MediaItemId = item.MediaItemId,
PlaybackOrder = playbackOrder,
// Play every item in each entry before advancing so lineup order is honored
// (PlaylistEnumerator round-robins one-per-entry unless PlayAll/Count).
PlayAll = true,
IncludeInProgramGuide = true
});
}
return playlist;
}
private static Channel BuildChannel(
@@ -242,37 +391,13 @@ public class CreateChannelFromLineupHandler(
};
}
private static Collection BuildCollection(string channelNumber, string channelName, List<CreateChannelFromLineupItem> lineup)
{
var collection = new Collection
{
Name = GeneratedName(channelNumber, channelName, "Lineup"),
UseCustomPlaybackOrder = true,
CollectionItems = []
};
int index = 1;
foreach (CreateChannelFromLineupItem item in lineup.Where(IsMediaLineupItem))
{
collection.CollectionItems.Add(new CollectionItem
{
Collection = collection,
MediaItemId = item.MediaItemId.GetValueOrDefault(),
CustomIndex = index++
});
}
return collection;
}
private static ProgramSchedule BuildProgramSchedule(
string channelNumber,
string channelName,
string scheduleName,
ChannelTemplate template,
CreateChannelFromLineupAdvancedOptions advanced) =>
new()
{
Name = GeneratedName(channelNumber, channelName, "Schedule"),
Name = scheduleName,
KeepMultiPartEpisodesTogether = true,
TreatCollectionsAsShows = true,
ShuffleScheduleItems = advanced.ShuffleScheduleItems ?? template.ShuffleScheduleItems,
@@ -281,6 +406,34 @@ public class CreateChannelFromLineupHandler(
Items = []
};
private static ProgramScheduleItemFlood BuildFloodBase(
PlaybackOrder playbackOrder,
CreateChannelFromLineupAdvancedOptions advanced,
ChannelTemplate template,
int? fallbackFillerId,
int? preRollFillerId,
int? midRollFillerId,
int? postRollFillerId) =>
new()
{
Index = 1,
PlaybackOrder = playbackOrder,
GuideMode = GuideMode.Normal,
CustomTitle = string.Empty,
SearchTitle = string.Empty,
SearchQuery = string.Empty,
PreRollFillerId = preRollFillerId,
MidRollFillerId = midRollFillerId,
PostRollFillerId = postRollFillerId,
FallbackFillerId = fallbackFillerId,
PreferredAudioLanguageCode =
advanced.PreferredAudioLanguageCode ?? template.PreferredAudioLanguageCode ?? string.Empty,
PreferredAudioTitle = advanced.PreferredAudioTitle ?? template.PreferredAudioTitle ?? string.Empty,
PreferredSubtitleLanguageCode =
advanced.PreferredSubtitleLanguageCode ?? template.PreferredSubtitleLanguageCode ?? string.Empty,
SubtitleMode = advanced.SubtitleMode ?? template.SubtitleMode
};
private static string GeneratedName(string channelNumber, string channelName, string suffix)
{
string prefix = $"{channelNumber} {channelName}".Trim();
@@ -293,80 +446,36 @@ public class CreateChannelFromLineupHandler(
return $"{prefix} {suffix}".Trim();
}
private static List<ProgramScheduleItem> BuildScheduleItems(
Collection collection,
List<CreateChannelFromLineupItem> lineup,
PlaybackOrder playbackOrder,
ChannelTemplate template,
CreateChannelFromLineupAdvancedOptions advanced,
int? fallbackFillerId,
int? preRollFillerId,
int? midRollFillerId,
int? postRollFillerId)
// De-collide a generated (already <= 50 char) name against a unique index by appending " 2", " 3", ...
// rather than leaking a raw UNIQUE-constraint failure.
private static async Task<string> DeCollideName(
string baseName,
Func<string, CancellationToken, Task<bool>> exists,
CancellationToken cancellationToken)
{
var result = new List<ProgramScheduleItem>();
if (collection.CollectionItems.Count != 0)
if (!await exists(baseName, cancellationToken))
{
result.Add(new ProgramScheduleItemFlood
{
Index = result.Count + 1,
Collection = collection,
CollectionType = CollectionType.Collection,
PlaybackOrder = playbackOrder,
GuideMode = GuideMode.Normal,
CustomTitle = string.Empty,
SearchTitle = string.Empty,
SearchQuery = string.Empty,
PreRollFillerId = preRollFillerId,
MidRollFillerId = midRollFillerId,
PostRollFillerId = postRollFillerId,
FallbackFillerId = fallbackFillerId,
PreferredAudioLanguageCode =
advanced.PreferredAudioLanguageCode ?? template.PreferredAudioLanguageCode ?? string.Empty,
PreferredAudioTitle = advanced.PreferredAudioTitle ?? template.PreferredAudioTitle ?? string.Empty,
PreferredSubtitleLanguageCode =
advanced.PreferredSubtitleLanguageCode ?? template.PreferredSubtitleLanguageCode ?? string.Empty,
SubtitleMode = advanced.SubtitleMode ?? template.SubtitleMode
});
return baseName;
}
foreach (CreateChannelFromLineupItem item in lineup.Where(i => !IsMediaLineupItem(i)))
for (int n = 2; ; n++)
{
result.Add(new ProgramScheduleItemFlood
{
Index = result.Count + 1,
CollectionType = item.CollectionType,
CollectionId = item.CollectionId,
MultiCollectionId = item.MultiCollectionId,
SmartCollectionId = item.SmartCollectionId,
RerunCollectionId = item.RerunCollectionId,
PlaylistId = item.PlaylistId,
PlaybackOrder = playbackOrder,
GuideMode = GuideMode.Normal,
CustomTitle = string.Empty,
SearchTitle = string.Empty,
SearchQuery = string.Empty,
PreRollFillerId = preRollFillerId,
MidRollFillerId = midRollFillerId,
PostRollFillerId = postRollFillerId,
FallbackFillerId = fallbackFillerId,
PreferredAudioLanguageCode =
advanced.PreferredAudioLanguageCode ?? template.PreferredAudioLanguageCode ?? string.Empty,
PreferredAudioTitle = advanced.PreferredAudioTitle ?? template.PreferredAudioTitle ?? string.Empty,
PreferredSubtitleLanguageCode =
advanced.PreferredSubtitleLanguageCode ?? template.PreferredSubtitleLanguageCode ?? string.Empty,
SubtitleMode = advanced.SubtitleMode ?? template.SubtitleMode
});
}
string suffix = $" {n}";
string candidate = baseName.Length + suffix.Length > 50
? baseName[..(50 - suffix.Length)].TrimEnd() + suffix
: baseName + suffix;
return result;
if (!await exists(candidate, cancellationToken))
{
return candidate;
}
}
}
private static async Task<Either<BaseError, Unit>> ValidateReferences(
TvContext dbContext,
ChannelTemplate template,
CreateChannelFromLineupAdvancedOptions advanced,
List<CreateChannelFromLineupItem> lineup,
ChannelTemplate template,
CancellationToken cancellationToken)
{
int ffmpegProfileId = advanced.FFmpegProfileId ?? template.FFmpegProfileId;
@@ -396,15 +505,6 @@ public class CreateChannelFromLineupHandler(
return error;
}
for (int i = 0; i < lineup.Count; i++)
{
Either<BaseError, Unit> itemValidation = await ValidateLineupItem(dbContext, lineup[i], i, cancellationToken);
foreach (BaseError error in itemValidation.LeftToSeq())
{
return error;
}
}
return Unit.Default;
}
@@ -462,17 +562,12 @@ public class CreateChannelFromLineupHandler(
CancellationToken cancellationToken) =>
dbContext.FillerPresets.AnyAsync(fp => fp.Id == id && fp.FillerKind == fillerKind, cancellationToken);
private static async Task<Either<BaseError, Unit>> ValidateLineupItem(
private static async Task<Either<BaseError, NormalizedLineupItem>> NormalizeLineupItem(
TvContext dbContext,
CreateChannelFromLineupItem item,
int index,
CancellationToken cancellationToken)
{
if (item.MediaType is LibraryBrowseMediaType.RerunCollection)
{
item = item with { CollectionType = CollectionType.RerunFirstRun };
}
int providedIds = new int?[]
{
item.CollectionId,
@@ -488,48 +583,172 @@ public class CreateChannelFromLineupHandler(
return BaseError.New($"lineup[{index}] must provide exactly one typed id.");
}
return item.CollectionType switch
switch (item.MediaType)
{
CollectionType.Movie when item.MediaType is LibraryBrowseMediaType.Movie && item.MediaItemId.HasValue =>
await Exists(dbContext.Movies, item.MediaItemId.Value, $"lineup[{index}] Movie", cancellationToken),
CollectionType.TelevisionShow when item.MediaType is LibraryBrowseMediaType.TelevisionShow && item.MediaItemId.HasValue =>
await Exists(dbContext.Shows, item.MediaItemId.Value, $"lineup[{index}] TelevisionShow", cancellationToken),
CollectionType.TelevisionSeason when item.MediaType is LibraryBrowseMediaType.TelevisionSeason && item.MediaItemId.HasValue =>
await Exists(dbContext.Seasons, item.MediaItemId.Value, $"lineup[{index}] TelevisionSeason", cancellationToken),
CollectionType.Artist when item.MediaType is LibraryBrowseMediaType.Artist && item.MediaItemId.HasValue =>
await Exists(dbContext.Artists, item.MediaItemId.Value, $"lineup[{index}] Artist", cancellationToken),
CollectionType.Collection when item.MediaType is LibraryBrowseMediaType.Collection && item.CollectionId.HasValue =>
await Exists(dbContext.Collections, item.CollectionId.Value, $"lineup[{index}] Collection", cancellationToken),
CollectionType.SmartCollection when item.MediaType is LibraryBrowseMediaType.SmartCollection && item.SmartCollectionId.HasValue =>
await Exists(dbContext.SmartCollections, item.SmartCollectionId.Value, $"lineup[{index}] SmartCollection", cancellationToken),
CollectionType.MultiCollection when item.MediaType is LibraryBrowseMediaType.MultiCollection && item.MultiCollectionId.HasValue =>
await Exists(dbContext.MultiCollections, item.MultiCollectionId.Value, $"lineup[{index}] MultiCollection", cancellationToken),
CollectionType.RerunFirstRun when item.MediaType is LibraryBrowseMediaType.RerunCollection && item.RerunCollectionId.HasValue =>
await Exists(dbContext.RerunCollections, item.RerunCollectionId.Value, $"lineup[{index}] RerunCollection", cancellationToken),
CollectionType.Playlist when item.MediaType is LibraryBrowseMediaType.Playlist && item.PlaylistId.HasValue =>
await Exists(dbContext.Playlists, item.PlaylistId.Value, $"lineup[{index}] Playlist", cancellationToken),
_ => BaseError.New($"lineup[{index}] has an unsupported or mismatched media type, collection type, and id.")
};
case LibraryBrowseMediaType.Movie:
return await NormalizeMediaItem(
dbContext.Movies, item, CollectionType.Movie, index, "Movie", cancellationToken);
case LibraryBrowseMediaType.TelevisionShow:
return await NormalizeMediaItem(
dbContext.Shows, item, CollectionType.TelevisionShow, index, "TelevisionShow", cancellationToken);
case LibraryBrowseMediaType.TelevisionSeason:
return await NormalizeMediaItem(
dbContext.Seasons, item, CollectionType.TelevisionSeason, index, "TelevisionSeason", cancellationToken);
case LibraryBrowseMediaType.Artist:
return await NormalizeMediaItem(
dbContext.Artists, item, CollectionType.Artist, index, "Artist", cancellationToken);
case LibraryBrowseMediaType.Collection:
if (item.CollectionType != CollectionType.Collection)
{
return Mismatch(index, item);
}
if (!item.CollectionId.HasValue)
{
return WrongId(index, item.MediaType, "collectionId");
}
return await ExistsThen(
dbContext.Collections,
item.CollectionId.Value,
$"lineup[{index}] Collection",
new NormalizedLineupItem(index, CollectionType.Collection, CollectionId: item.CollectionId),
cancellationToken);
case LibraryBrowseMediaType.SmartCollection:
if (item.CollectionType != CollectionType.SmartCollection)
{
return Mismatch(index, item);
}
if (!item.SmartCollectionId.HasValue)
{
return WrongId(index, item.MediaType, "smartCollectionId");
}
return await ExistsThen(
dbContext.SmartCollections,
item.SmartCollectionId.Value,
$"lineup[{index}] SmartCollection",
new NormalizedLineupItem(index, CollectionType.SmartCollection, SmartCollectionId: item.SmartCollectionId),
cancellationToken);
case LibraryBrowseMediaType.MultiCollection:
if (item.CollectionType != CollectionType.MultiCollection)
{
return Mismatch(index, item);
}
if (!item.MultiCollectionId.HasValue)
{
return WrongId(index, item.MediaType, "multiCollectionId");
}
return await ExistsThen(
dbContext.MultiCollections,
item.MultiCollectionId.Value,
$"lineup[{index}] MultiCollection",
new NormalizedLineupItem(index, CollectionType.MultiCollection, MultiCollectionId: item.MultiCollectionId),
cancellationToken);
case LibraryBrowseMediaType.RerunCollection:
if (item.CollectionType != CollectionType.RerunFirstRun)
{
return Mismatch(index, item);
}
if (!item.RerunCollectionId.HasValue)
{
return WrongId(index, item.MediaType, "rerunCollectionId");
}
return await ExistsThen(
dbContext.RerunCollections,
item.RerunCollectionId.Value,
$"lineup[{index}] RerunCollection",
new NormalizedLineupItem(index, CollectionType.RerunFirstRun, RerunCollectionId: item.RerunCollectionId),
cancellationToken);
case LibraryBrowseMediaType.Playlist:
if (item.CollectionType != CollectionType.Playlist)
{
return Mismatch(index, item);
}
if (!item.PlaylistId.HasValue)
{
return WrongId(index, item.MediaType, "playlistId");
}
return await ExistsThen(
dbContext.Playlists,
item.PlaylistId.Value,
$"lineup[{index}] Playlist",
new NormalizedLineupItem(index, CollectionType.Playlist, PlaylistId: item.PlaylistId),
cancellationToken);
default:
return BaseError.New($"lineup[{index}] has an unsupported media type '{item.MediaType}'.");
}
}
private static async Task<Either<BaseError, Unit>> Exists<TEntity>(
private static async Task<Either<BaseError, NormalizedLineupItem>> NormalizeMediaItem<TEntity>(
DbSet<TEntity> set,
int id,
CreateChannelFromLineupItem item,
CollectionType expectedType,
int index,
string label,
CancellationToken cancellationToken)
where TEntity : class
{
TEntity entity = await set.FindAsync([id], cancellationToken);
return entity is null ? new NotFoundError($"{label} {id} does not exist.") : Unit.Default;
if (item.CollectionType != expectedType)
{
return Mismatch(index, item);
}
if (!item.MediaItemId.HasValue)
{
return WrongId(index, item.MediaType, "mediaItemId");
}
return await ExistsThen(
set,
item.MediaItemId.Value,
$"lineup[{index}] {label}",
new NormalizedLineupItem(index, expectedType, MediaItemId: item.MediaItemId),
cancellationToken);
}
private static bool IsMediaLineupItem(CreateChannelFromLineupItem item) =>
item.CollectionType is CollectionType.Movie or CollectionType.TelevisionShow or CollectionType.TelevisionSeason
or CollectionType.Artist;
private static BaseError Mismatch(int index, CreateChannelFromLineupItem item) =>
BaseError.New(
$"lineup[{index}]: media type '{item.MediaType}' does not match collection type '{item.CollectionType}'.");
private static BaseError WrongId(int index, LibraryBrowseMediaType mediaType, string expectedField) =>
BaseError.New($"lineup[{index}]: media type '{mediaType}' requires a {expectedField}.");
private static async Task<Either<BaseError, NormalizedLineupItem>> ExistsThen<TEntity>(
DbSet<TEntity> set,
int id,
string label,
NormalizedLineupItem normalized,
CancellationToken cancellationToken)
where TEntity : class
{
bool exists = await set.AsNoTracking()
.AnyAsync(e => EF.Property<int>(e, "Id") == id, cancellationToken);
return exists
? normalized
: new NotFoundError($"{label} {id} does not exist.");
}
private sealed record NormalizedLineupItem(
int Index,
CollectionType CollectionType,
int? CollectionId = null,
int? MultiCollectionId = null,
int? SmartCollectionId = null,
int? RerunCollectionId = null,
int? MediaItemId = null,
int? PlaylistId = null);
private sealed record PreparedCreate(
Channel Channel,
Collection Collection,
Playlist Playlist,
ProgramSchedule ProgramSchedule,
Playout Playout);
}
@@ -4,6 +4,6 @@ namespace ErsatzTV.Core.Api.Channels;
public record CreateChannelFromLineupResponseModel(
int ChannelId,
int CollectionId,
int? PlaylistId,
int ProgramScheduleId,
int PlayoutId);
@@ -15,10 +15,12 @@ using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Tests.Support;
using LanguageExt;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
using DomainChannel = ErsatzTV.Core.Domain.Channel;
using DomainPlaylistItem = ErsatzTV.Core.Domain.PlaylistItem;
namespace ErsatzTV.Tests.Application.Channels;
@@ -41,7 +43,7 @@ public class CreateChannelFromLineupHandlerTests
public async Task TearDown() => await _db.DisposeAsync();
[Test]
public async Task Should_Create_Channel_Collection_Schedule_Items_And_Playout()
public async Task Should_Create_Single_Media_Item_Directly_Without_Playlist()
{
await SeedTemplateDependencies();
await SeedTemplate();
@@ -52,11 +54,16 @@ public class CreateChannelFromLineupHandlerTests
CreateChannelFromLineupResponseModel response = RightOf(result);
response.ChannelId.ShouldBeGreaterThan(0);
response.CollectionId.ShouldBeGreaterThan(0);
response.PlaylistId.ShouldBeNull();
response.ProgramScheduleId.ShouldBeGreaterThan(0);
response.PlayoutId.ShouldBeGreaterThan(0);
await using TvContext context = _db.CreateContext();
// No generated playlist or collection for a single-item lineup.
(await context.Playlists.CountAsync()).ShouldBe(0);
(await context.Collections.CountAsync()).ShouldBe(0);
DomainChannel channel = await context.Channels.SingleAsync();
channel.Name.ShouldBe("Movies");
channel.Number.ShouldBe("12");
@@ -66,25 +73,18 @@ public class CreateChannelFromLineupHandlerTests
channel.StreamingMode.ShouldBe(StreamingMode.HttpLiveStreamingSegmenter);
channel.ShowInEpg.ShouldBeTrue();
Collection collection = await context.Collections
.Include(c => c.CollectionItems)
.SingleAsync();
collection.Name.ShouldBe("12 Movies Lineup");
collection.UseCustomPlaybackOrder.ShouldBeTrue();
collection.CollectionItems.Single().MediaItemId.ShouldBe(42);
collection.CollectionItems.Single().CustomIndex.ShouldBe(1);
ProgramSchedule schedule = await context.ProgramSchedules
.Include(ps => ps.Items)
.SingleAsync();
ProgramSchedule schedule = await context.ProgramSchedules.Include(ps => ps.Items).SingleAsync();
schedule.Name.ShouldBe("12 Movies Schedule");
schedule.ShuffleScheduleItems.ShouldBeTrue();
schedule.RandomStartPoint.ShouldBeTrue();
schedule.FixedStartTimeBehavior.ShouldBe(FixedStartTimeBehavior.Strict);
ProgramScheduleItem item = schedule.Items.Single();
item.CollectionType.ShouldBe(CollectionType.Collection);
item.CollectionId.ShouldBe(collection.Id);
item.ShouldBeOfType<ProgramScheduleItemFlood>();
item.CollectionType.ShouldBe(CollectionType.Movie);
item.MediaItemId.ShouldBe(42);
item.CollectionId.ShouldBeNull();
item.PlaylistId.ShouldBeNull();
item.PlaybackOrder.ShouldBe(PlaybackOrder.Shuffle);
item.PreRollFillerId.ShouldBe(2);
item.MidRollFillerId.ShouldBe(3);
@@ -106,6 +106,393 @@ public class CreateChannelFromLineupHandlerTests
_searchTargets.Received(1).SearchTargetsChanged();
}
[Test]
public async Task Should_Create_Single_Collection_Item_Directly()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedCollection(7);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(lineup: [CollectionItem(7)]), CancellationToken.None);
RightOf(result).PlaylistId.ShouldBeNull();
await using TvContext context = _db.CreateContext();
(await context.Playlists.CountAsync()).ShouldBe(0);
ProgramScheduleItem item = await context.ProgramScheduleItems.SingleAsync();
item.CollectionType.ShouldBe(CollectionType.Collection);
item.CollectionId.ShouldBe(7);
}
[Test]
public async Task Should_Create_Single_Playlist_Item_Directly()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedExistingPlaylist(9);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(lineup: [PlaylistEntry(9)]), CancellationToken.None);
// The generated-playlist id is null for a single-item lineup, even when it references a playlist.
RightOf(result).PlaylistId.ShouldBeNull();
await using TvContext context = _db.CreateContext();
ProgramScheduleItem item = await context.ProgramScheduleItems.SingleAsync();
item.CollectionType.ShouldBe(CollectionType.Playlist);
item.PlaylistId.ShouldBe(9);
}
[Test]
public async Task Should_Create_Single_Rerun_Collection_As_First_Run()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedRerunCollection(11);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(lineup: [RerunItem(11)]), CancellationToken.None);
RightOf(result).PlaylistId.ShouldBeNull();
await using TvContext context = _db.CreateContext();
ProgramScheduleItem item = await context.ProgramScheduleItems.SingleAsync();
item.CollectionType.ShouldBe(CollectionType.RerunFirstRun);
item.RerunCollectionId.ShouldBe(11);
}
[Test]
public async Task Should_Create_Multi_Item_Lineup_As_Generated_System_Playlist()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await SeedShow(43);
await SeedCollection(7);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(
MakeRequest(lineup: [MovieItem(42), ShowItem(43), CollectionItem(7)]),
CancellationToken.None);
CreateChannelFromLineupResponseModel response = RightOf(result);
response.PlaylistId.ShouldNotBeNull();
await using TvContext context = _db.CreateContext();
PlaylistGroup group = await context.PlaylistGroups.SingleAsync();
group.Name.ShouldBe("Channel Lineups");
group.IsSystem.ShouldBeTrue();
Playlist playlist = await context.Playlists.Include(p => p.Items).SingleAsync();
playlist.Id.ShouldBe(response.PlaylistId!.Value);
playlist.Name.ShouldBe("12 Movies Lineup");
playlist.IsSystem.ShouldBeTrue();
playlist.PlaylistGroupId.ShouldBe(group.Id);
List<DomainPlaylistItem> items = playlist.Items.OrderBy(i => i.Index).ToList();
items.Count.ShouldBe(3);
items.ShouldAllBe(i => i.PlayAll);
items.ShouldAllBe(i => i.IncludeInProgramGuide);
items.ShouldAllBe(i => i.PlaybackOrder == PlaybackOrder.Shuffle);
items[0].Index.ShouldBe(1);
items[0].CollectionType.ShouldBe(CollectionType.Movie);
items[0].MediaItemId.ShouldBe(42);
items[1].Index.ShouldBe(2);
items[1].CollectionType.ShouldBe(CollectionType.TelevisionShow);
items[1].MediaItemId.ShouldBe(43);
items[2].Index.ShouldBe(3);
items[2].CollectionType.ShouldBe(CollectionType.Collection);
items[2].CollectionId.ShouldBe(7);
// Exactly one flood schedule item, referencing the generated playlist.
ProgramScheduleItem scheduleItem = await context.ProgramScheduleItems.SingleAsync();
scheduleItem.ShouldBeOfType<ProgramScheduleItemFlood>();
scheduleItem.CollectionType.ShouldBe(CollectionType.Playlist);
scheduleItem.PlaylistId.ShouldBe(playlist.Id);
}
[Test]
public async Task Should_Reject_Rerun_Collection_In_Multi_Item_Lineup()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await SeedRerunCollection(11);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(
MakeRequest(lineup: [MovieItem(42), RerunItem(11)]),
CancellationToken.None);
BaseError error = LeftOf(result);
error.ShouldNotBeOfType<NotFoundError>();
error.Value.ShouldContain("single-item");
}
[Test]
public async Task Should_Reject_Playlist_In_Multi_Item_Lineup()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await SeedExistingPlaylist(9);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(
MakeRequest(lineup: [MovieItem(42), PlaylistEntry(9)]),
CancellationToken.None);
BaseError error = LeftOf(result);
error.ShouldNotBeOfType<NotFoundError>();
error.Value.ShouldContain("single-item");
}
[Test]
public async Task Advanced_Overrides_Should_Beat_Template_Defaults()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await using (TvContext context = _db.CreateContext())
{
context.FFmpegProfiles.Add(new FFmpegProfile { Id = 2, Name = "advanced-profile" });
context.ChannelWatermarks.Add(new ChannelWatermark { Id = 21, Name = "wm", Image = "wm.png" });
context.FillerPresets.AddRange(
MakeFiller(6, FillerKind.Fallback),
MakeFiller(7, FillerKind.PreRoll));
await context.SaveChangesAsync();
}
var advanced = new CreateChannelFromLineupAdvancedOptions(
PlaybackOrder: PlaybackOrder.Shuffle,
FFmpegProfileId: 2,
WatermarkId: 21,
FallbackFillerId: 6,
PreRollFillerId: 7,
StreamingMode: StreamingMode.TransportStream);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(advanced: advanced), CancellationToken.None);
RightOf(result);
await using TvContext assert = _db.CreateContext();
DomainChannel channel = await assert.Channels.SingleAsync();
// Advanced wins over the template's values (template = profile 1, no watermark, HLS, fallback 5).
channel.FFmpegProfileId.ShouldBe(2);
channel.WatermarkId.ShouldBe(21);
channel.StreamingMode.ShouldBe(StreamingMode.TransportStream);
channel.FallbackFillerId.ShouldBe(6);
ProgramScheduleItem item = await assert.ProgramScheduleItems.SingleAsync();
item.PlaybackOrder.ShouldBe(PlaybackOrder.Shuffle);
item.PreRollFillerId.ShouldBe(7);
item.FallbackFillerId.ShouldBe(6);
// Not overridden in Advanced -> falls through to the template value.
item.MidRollFillerId.ShouldBe(3);
}
[Test]
public async Task Should_Return_Validation_Error_When_Not_Exactly_One_Id_Provided()
{
await SeedTemplateDependencies();
await SeedTemplate();
var item = new CreateChannelFromLineupItem(
LibraryBrowseMediaType.Movie, CollectionType.Movie, null, null, null, null, null, null);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(lineup: [item]), CancellationToken.None);
LeftOf(result).Value.ShouldContain("exactly one typed id");
}
[Test]
public async Task Should_Return_Validation_Error_When_Media_Type_Does_Not_Match_Collection_Type()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
var item = new CreateChannelFromLineupItem(
LibraryBrowseMediaType.Movie, CollectionType.Playlist, null, null, null, null, 42, null);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(lineup: [item]), CancellationToken.None);
BaseError error = LeftOf(result);
error.ShouldNotBeOfType<NotFoundError>();
error.Value.ShouldContain("does not match");
}
[Test]
public async Task Should_Reject_MultiCollection_With_Non_Shuffle_Playback_Order()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMultiCollection(15);
var advanced = new CreateChannelFromLineupAdvancedOptions(PlaybackOrder: PlaybackOrder.Chronological);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(
MakeRequest(advanced: advanced, lineup: [MultiCollectionItem(15)]),
CancellationToken.None);
LeftOf(result).Value.ShouldContain("Invalid playback order for multi collection");
}
[Test]
public async Task Should_Reject_Mirror_Playout_Source()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
var advanced = new CreateChannelFromLineupAdvancedOptions(
PlaybackOrder: PlaybackOrder.Shuffle,
PlayoutSource: ChannelPlayoutSource.Mirror);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(advanced: advanced), CancellationToken.None);
LeftOf(result).Value.ShouldContain("Mirror playout source");
}
[Test]
public async Task OnDemand_Playout_Mode_Should_Queue_TimeShift_After_Build()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
var advanced = new CreateChannelFromLineupAdvancedOptions(
PlaybackOrder: PlaybackOrder.Shuffle,
PlayoutMode: ChannelPlayoutMode.OnDemand);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(advanced: advanced), CancellationToken.None);
CreateChannelFromLineupResponseModel response = RightOf(result);
_background.Reader.TryRead(out IBackgroundServiceRequest? first).ShouldBeTrue();
first.ShouldBeOfType<BuildPlayout>();
_background.Reader.TryRead(out IBackgroundServiceRequest? second).ShouldBeTrue();
TimeShiftOnDemandPlayout timeShift = second.ShouldBeOfType<TimeShiftOnDemandPlayout>();
timeShift.PlayoutId.ShouldBe(response.PlayoutId);
timeShift.Force.ShouldBeFalse();
_background.Reader.TryRead(out IBackgroundServiceRequest? third).ShouldBeTrue();
third.ShouldBeOfType<RefreshChannelList>();
}
[Test]
public async Task Should_Recreate_With_De_Collided_Names_After_Channel_Delete()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await SeedShow(43);
// First create builds "12 Movies Schedule" + "12 Movies Lineup".
RightOf(await MakeHandler().Handle(
MakeRequest(lineup: [MovieItem(42), ShowItem(43)]),
CancellationToken.None));
// Delete the channel + playout but leave the generated schedule/playlist rows behind.
await using (TvContext context = _db.CreateContext())
{
context.Playouts.RemoveRange(await context.Playouts.ToListAsync());
context.Channels.RemoveRange(await context.Channels.ToListAsync());
await context.SaveChangesAsync();
}
// Second create with the same name must succeed via de-collided names.
CreateChannelFromLineupResponseModel response = RightOf(await MakeHandler().Handle(
MakeRequest(lineup: [MovieItem(42), ShowItem(43)]),
CancellationToken.None));
await using TvContext assert = _db.CreateContext();
bool scheduleExists = await assert.ProgramSchedules.AnyAsync(ps => ps.Name == "12 Movies Schedule 2");
scheduleExists.ShouldBeTrue();
Playlist newPlaylist = await assert.Playlists.SingleAsync(p => p.Id == response.PlaylistId!.Value);
newPlaylist.Name.ShouldBe("12 Movies Lineup 2");
// Only one system playlist group is ever created.
(await assert.PlaylistGroups.CountAsync(pg => pg.Name == "Channel Lineups")).ShouldBe(1);
}
[Test]
public async Task Should_Return_NotFound_For_Missing_Advanced_References()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await AssertNotFound(
new CreateChannelFromLineupAdvancedOptions(FFmpegProfileId: 999),
"FFmpegProfile 999");
await AssertNotFound(
new CreateChannelFromLineupAdvancedOptions(WatermarkId: 999),
"Watermark 999");
await AssertNotFound(
new CreateChannelFromLineupAdvancedOptions(FallbackFillerId: 999),
"Fallback filler 999");
await AssertNotFound(
new CreateChannelFromLineupAdvancedOptions(PreRollFillerId: 999),
"Pre-roll filler 999");
await AssertNotFound(
new CreateChannelFromLineupAdvancedOptions(MidRollFillerId: 999),
"Mid-roll filler 999");
await AssertNotFound(
new CreateChannelFromLineupAdvancedOptions(PostRollFillerId: 999),
"Post-roll filler 999");
}
[Test]
public async Task Should_Roll_Back_When_Save_Fails()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await SeedShow(43);
// A non-system group with the reserved name forces the handler's new system group insert
// to violate the unique Name index at save time (multi-item lineup path).
await using (TvContext context = _db.CreateContext())
{
context.PlaylistGroups.Add(new PlaylistGroup { Id = 99, Name = "Channel Lineups", IsSystem = false });
await context.SaveChangesAsync();
}
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(
MakeRequest(lineup: [MovieItem(42), ShowItem(43)]),
CancellationToken.None);
BaseError error = LeftOf(result);
error.ShouldNotBeOfType<NotFoundError>();
error.Value.ShouldBe("Unable to create channel from lineup");
await using TvContext assertContext = _db.CreateContext();
(await assertContext.Channels.CountAsync()).ShouldBe(0);
(await assertContext.Playlists.CountAsync()).ShouldBe(0);
(await assertContext.ProgramSchedules.CountAsync()).ShouldBe(0);
(await assertContext.Playouts.CountAsync()).ShouldBe(0);
_background.Reader.TryRead(out _).ShouldBeFalse();
}
[Test]
public async Task Should_Return_NotFound_When_Template_Is_Missing()
{
@@ -150,6 +537,38 @@ public class CreateChannelFromLineupHandlerTests
error.Value.ShouldContain("Channel number must be unique");
}
[Test]
public async Task Should_Return_Validation_Error_When_Disabled_But_Shown_In_Epg()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(
MakeRequest(isEnabled: false, showInEpg: true),
CancellationToken.None);
BaseError error = LeftOf(result);
error.ShouldNotBeOfType<NotFoundError>();
error.Value.ShouldContain("Disabled channels cannot be shown in EPG");
}
[Test]
public async Task Should_Return_Validation_Error_For_Invalid_External_Logo()
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
var logo = new ArtworkContentTypeModel("ftp://example.com/logo.png", string.Empty);
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(logo: logo), CancellationToken.None);
LeftOf(result).Value.ShouldContain("External logo url is invalid");
}
[Test]
public async Task Should_Return_NotFound_When_Lineup_Target_Is_Missing()
{
@@ -164,35 +583,21 @@ public class CreateChannelFromLineupHandlerTests
error.Value.ShouldContain("Movie 42");
}
[Test]
public async Task Should_Roll_Back_When_Save_Fails()
private async Task AssertNotFound(CreateChannelFromLineupAdvancedOptions advanced, string expectedFragment)
{
await SeedTemplateDependencies();
await SeedTemplate();
await SeedMovie(42);
await using (TvContext context = _db.CreateContext())
{
context.ProgramSchedules.Add(new ProgramSchedule
{
Name = "12 Movies Schedule",
FixedStartTimeBehavior = FixedStartTimeBehavior.Flexible
});
await context.SaveChangesAsync();
}
Either<BaseError, CreateChannelFromLineupResponseModel> result =
await MakeHandler().Handle(MakeRequest(), CancellationToken.None);
await MakeHandler().Handle(MakeRequest(advanced: advanced), CancellationToken.None);
LeftOf(result).ShouldNotBeOfType<NotFoundError>();
await using TvContext assertContext = _db.CreateContext();
(await assertContext.Channels.CountAsync()).ShouldBe(0);
(await assertContext.Collections.CountAsync()).ShouldBe(0);
(await assertContext.Playouts.CountAsync()).ShouldBe(0);
_background.Reader.TryRead(out _).ShouldBeFalse();
NotFoundError error = LeftOf(result).ShouldBeOfType<NotFoundError>();
error.Value.ShouldContain(expectedFragment);
}
private CreateChannelFromLineupHandler MakeHandler() =>
new(_background.Writer, _db.Factory, _searchTargets);
new(
_background.Writer,
_db.Factory,
_searchTargets,
NullLogger<CreateChannelFromLineupHandler>.Instance);
private async Task SeedTemplateDependencies()
{
@@ -247,6 +652,47 @@ public class CreateChannelFromLineupHandlerTests
await context.SaveChangesAsync();
}
private async Task SeedShow(int id)
{
await using TvContext context = _db.CreateContext();
context.Shows.Add(new Show { Id = id });
await context.SaveChangesAsync();
}
private async Task SeedCollection(int id)
{
await using TvContext context = _db.CreateContext();
context.Collections.Add(new Collection { Id = id, Name = $"Collection {id}" });
await context.SaveChangesAsync();
}
private async Task SeedMultiCollection(int id)
{
await using TvContext context = _db.CreateContext();
context.MultiCollections.Add(new MultiCollection { Id = id, Name = $"Multi {id}" });
await context.SaveChangesAsync();
}
private async Task SeedRerunCollection(int id)
{
await using TvContext context = _db.CreateContext();
context.RerunCollections.Add(new RerunCollection
{
Id = id,
Name = $"Rerun {id}",
CollectionType = CollectionType.Collection
});
await context.SaveChangesAsync();
}
private async Task SeedExistingPlaylist(int id)
{
await using TvContext context = _db.CreateContext();
context.PlaylistGroups.Add(new PlaylistGroup { Id = 500, Name = "User Group", IsSystem = false });
context.Playlists.Add(new Playlist { Id = id, Name = $"Playlist {id}", PlaylistGroupId = 500, IsSystem = false });
await context.SaveChangesAsync();
}
private static FillerPreset MakeFiller(int id, FillerKind kind) =>
new()
{
@@ -258,28 +704,44 @@ public class CreateChannelFromLineupHandlerTests
CollectionType = CollectionType.Collection
};
private static CreateChannelFromLineupItem MovieItem(int id) =>
new(LibraryBrowseMediaType.Movie, CollectionType.Movie, null, null, null, null, id, null);
private static CreateChannelFromLineupItem ShowItem(int id) =>
new(LibraryBrowseMediaType.TelevisionShow, CollectionType.TelevisionShow, null, null, null, null, id, null);
private static CreateChannelFromLineupItem CollectionItem(int id) =>
new(LibraryBrowseMediaType.Collection, CollectionType.Collection, id, null, null, null, null, null);
private static CreateChannelFromLineupItem MultiCollectionItem(int id) =>
new(LibraryBrowseMediaType.MultiCollection, CollectionType.MultiCollection, null, id, null, null, null, null);
private static CreateChannelFromLineupItem RerunItem(int id) =>
new(LibraryBrowseMediaType.RerunCollection, CollectionType.RerunFirstRun, null, null, null, id, null, null);
private static CreateChannelFromLineupItem PlaylistEntry(int id) =>
new(LibraryBrowseMediaType.Playlist, CollectionType.Playlist, null, null, null, null, null, id);
private static CreateChannelFromLineup MakeRequest(
string number = "12",
int templateId = 10) =>
string name = "Movies",
int templateId = 10,
bool isEnabled = true,
bool showInEpg = true,
ArtworkContentTypeModel logo = null,
CreateChannelFromLineupAdvancedOptions advanced = null,
List<CreateChannelFromLineupItem> lineup = null) =>
new(
"Movies",
name,
number,
"Kids",
string.Empty,
ArtworkContentTypeModel.None,
true,
true,
logo ?? ArtworkContentTypeModel.None,
isEnabled,
showInEpg,
templateId,
new CreateChannelFromLineupAdvancedOptions(PlaybackOrder.Shuffle),
[new CreateChannelFromLineupItem(
LibraryBrowseMediaType.Movie,
CollectionType.Movie,
null,
null,
null,
null,
42,
null)]);
advanced ?? new CreateChannelFromLineupAdvancedOptions(PlaybackOrder.Shuffle),
lineup ?? [MovieItem(42)]);
private static BaseError LeftOf<TR>(Either<BaseError, TR> either) =>
either.Match(Left: e => e, Right: _ => throw new AssertionException("Expected a Left result"));
@@ -83,8 +83,15 @@ public class ChannelController(ChannelWriter<IBackgroundServiceRequest> workerCh
[Tags("Channels")]
[EndpointSummary("Create a channel from a library lineup")]
[EndpointDescription(
"Atomically creates the channel, generated lineup collection, program schedule, schedule items, " +
"and classic playout. Template defaults are stamped at create time; advanced overrides win.")]
"Atomically creates the channel, program schedule, a classic playout, and (for multi-item lineups) a " +
"generated system playlist. A single-item lineup produces one flood schedule item that references the " +
"target directly (movie, show, season, artist, collection, smart/multi collection, rerun collection, or " +
"playlist) and no generated playlist. A lineup with two or more items produces one generated system " +
"playlist whose entries play in the given order (each entry played in full before the next) referenced by " +
"one flood schedule item; only movies, shows, seasons, artists, collections, smart collections and multi " +
"collections are allowed there (rerun collections and playlists are single-item only). playbackOrder sets " +
"how items within each lineup entry are ordered. Template defaults are stamped at create time; advanced " +
"overrides win.")]
[EndpointGroupName("general")]
[ProducesResponseType(typeof(CreateChannelFromLineupResponseModel), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
@@ -8,6 +8,12 @@ using ErsatzTV.Core.Scheduling;
namespace ErsatzTV.Controllers.Api.Requests;
/// <summary>
/// Composite request to create a channel, its generated schedule/playlist and a classic playout in one call.
/// Template defaults are stamped at create time; any value set in <see cref="Advanced" /> overrides the template.
/// A single-item lineup references its target directly; a multi-item lineup is played in order via a generated
/// system playlist.
/// </summary>
public record CreateChannelFromLineupRequest(
string Name,
string Number,
+7 -4
View File
@@ -537,7 +537,7 @@
"Channels"
],
"summary": "Create a channel from a library lineup",
"description": "Atomically creates the channel, generated lineup collection, program schedule, schedule items, and classic playout. Template defaults are stamped at create time; advanced overrides win.",
"description": "Atomically creates the channel, program schedule, a classic playout, and (for multi-item lineups) a generated system playlist. A single-item lineup produces one flood schedule item that references the target directly (movie, show, season, artist, collection, smart/multi collection, rerun collection, or playlist) and no generated playlist. A lineup with two or more items produces one generated system playlist whose entries play in the given order (each entry played in full before the next) referenced by one flood schedule item; only movies, shows, seasons, artists, collections, smart collections and multi collections are allowed there (rerun collections and playlists are single-item only). playbackOrder sets how items within each lineup entry are ordered. Template defaults are stamped at create time; advanced overrides win.",
"operationId": "CreateChannelFromLineup",
"requestBody": {
"content": {
@@ -5607,7 +5607,7 @@
"CreateChannelFromLineupResponseModel": {
"required": [
"channelId",
"collectionId",
"playlistId",
"programScheduleId",
"playoutId"
],
@@ -5617,8 +5617,11 @@
"type": "integer",
"format": "int32"
},
"collectionId": {
"type": "integer",
"playlistId": {
"type": [
"null",
"integer"
],
"format": "int32"
},
"programScheduleId": {
+1 -1
View File
@@ -209,7 +209,7 @@ export interface components {
};
"CreateChannelFromLineupResponseModel": {
"channelId": number;
"collectionId": number;
"playlistId": null | number;
"programScheduleId": number;
"playoutId": number;
};