#nullable enable using System.Collections.Generic; using System.Linq; using ErsatzTV.Application.MediaCollections; using ErsatzTV.Core.Domain; namespace ErsatzTV.Controllers.Api.Requests; public record PlaylistItemRequest( int Index, CollectionType CollectionType, int? CollectionId, int? MultiCollectionId, int? SmartCollectionId, int? MediaItemId, PlaybackOrder PlaybackOrder, int? Count, bool PlayAll, bool IncludeInProgramGuide) { public ReplacePlaylistItem ToReplaceCommand(int index) => new( index, CollectionType, CollectionId, MultiCollectionId, SmartCollectionId, MediaItemId, PlaybackOrder, Count, PlayAll, IncludeInProgramGuide); // Mirrors ReplacePlaylistItemsHandler.CollectionTypeMustBeValid: the id required for the // item's collection type must be present. Kept in sync with that handler so the PUT and // preview endpoints enforce the same rule (the handler operates on the command type in the // Application layer and cannot reference this request DTO, hence the parallel logic). public bool HasRequiredId() => CollectionType switch { CollectionType.Collection => CollectionId is not null, CollectionType.MultiCollection => MultiCollectionId is not null, CollectionType.SmartCollection => SmartCollectionId is not null, CollectionType.TelevisionShow or CollectionType.TelevisionSeason or CollectionType.Artist or CollectionType.Movie or CollectionType.Episode or CollectionType.MusicVideo or CollectionType.OtherVideo or CollectionType.Song or CollectionType.Image => MediaItemId is not null, _ => false }; } public record ReplacePlaylistRequest(string? Name, List? Items) { public ReplacePlaylistItems ToCommand(int id) => new(id, Name ?? string.Empty, BuildItems()); // Preview operates on the posted draft, so there is no persisted playlist id (0). public ReplacePlaylistItems ToReplaceCommand() => new(0, Name ?? string.Empty, BuildItems()); // Array indexes of draft items missing the id required for their collection type. public List ItemsMissingRequiredId() { List items = Items ?? new List(); var invalid = new List(); for (int i = 0; i < items.Count; i++) { if (!items[i].HasRequiredId()) { invalid.Add(i); } } return invalid; } private List BuildItems() => (Items ?? new List()) .Select((item, index) => item.ToReplaceCommand(index)) .ToList(); }