add multi collections (#315)
* start to add multi-collections * create multi collection with no items * edit multi collections * fix plex credentials threading issue * add playback order to multi collection items * group episodes outside of shuffled enumerator * move playback order onto each schedule item * fix multi collection grouping * update changelog
This commit is contained in:
@@ -5,6 +5,8 @@ using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Core.Interfaces.Repositories;
|
||||
using ErsatzTV.Core.Scheduling;
|
||||
using ErsatzTV.Infrastructure.Extensions;
|
||||
using LanguageExt;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using static LanguageExt.Prelude;
|
||||
@@ -50,16 +52,113 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
|
||||
return result.Distinct().ToList();
|
||||
}
|
||||
|
||||
public async Task<List<MediaItem>> GetMultiCollectionItems(int id)
|
||||
{
|
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
|
||||
|
||||
var result = new List<MediaItem>();
|
||||
|
||||
Option<MultiCollection> maybeMultiCollection = await dbContext.MultiCollections
|
||||
.Include(mc => mc.Collections)
|
||||
.SelectOneAsync(mc => mc.Id, mc => mc.Id == id);
|
||||
|
||||
foreach (MultiCollection multiCollection in maybeMultiCollection)
|
||||
{
|
||||
foreach (int collectionId in multiCollection.Collections.Map(c => c.Id))
|
||||
{
|
||||
result.AddRange(await GetMovieItems(dbContext, collectionId));
|
||||
result.AddRange(await GetShowItems(dbContext, collectionId));
|
||||
result.AddRange(await GetSeasonItems(dbContext, collectionId));
|
||||
result.AddRange(await GetEpisodeItems(dbContext, collectionId));
|
||||
result.AddRange(await GetArtistItems(dbContext, collectionId));
|
||||
result.AddRange(await GetMusicVideoItems(dbContext, collectionId));
|
||||
}
|
||||
}
|
||||
|
||||
return result.Distinct().ToList();
|
||||
}
|
||||
|
||||
public async Task<List<CollectionWithItems>> GetMultiCollectionCollections(int id)
|
||||
{
|
||||
await using TvContext dbContext = _dbContextFactory.CreateDbContext();
|
||||
|
||||
var result = new List<CollectionWithItems>();
|
||||
|
||||
Option<MultiCollection> maybeMultiCollection = await dbContext.MultiCollections
|
||||
.Include(mc => mc.Collections)
|
||||
.Include(mc => mc.MultiCollectionItems)
|
||||
.ThenInclude(mci => mci.Collection)
|
||||
.SelectOneAsync(mc => mc.Id, mc => mc.Id == id);
|
||||
|
||||
foreach (MultiCollection multiCollection in maybeMultiCollection)
|
||||
{
|
||||
foreach (MultiCollectionItem multiCollectionItem in multiCollection.MultiCollectionItems)
|
||||
{
|
||||
List<MediaItem> items = await GetItems(multiCollectionItem.CollectionId);
|
||||
|
||||
if (multiCollectionItem.Collection.UseCustomPlaybackOrder)
|
||||
{
|
||||
foreach (Collection collection in await GetCollectionWithCollectionItemsUntracked(
|
||||
multiCollectionItem.CollectionId))
|
||||
{
|
||||
var sortedItems = collection.CollectionItems
|
||||
.OrderBy(ci => ci.CustomIndex)
|
||||
.Map(ci => items.First(i => i.Id == ci.MediaItemId))
|
||||
.ToList();
|
||||
|
||||
result.Add(
|
||||
new CollectionWithItems(
|
||||
multiCollectionItem.CollectionId,
|
||||
sortedItems,
|
||||
multiCollectionItem.ScheduleAsGroup,
|
||||
multiCollectionItem.PlaybackOrder,
|
||||
multiCollectionItem.Collection.UseCustomPlaybackOrder));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(
|
||||
new CollectionWithItems(
|
||||
multiCollectionItem.CollectionId,
|
||||
items,
|
||||
multiCollectionItem.ScheduleAsGroup,
|
||||
multiCollectionItem.PlaybackOrder,
|
||||
multiCollectionItem.Collection.UseCustomPlaybackOrder));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove duplicate items from ungrouped collections
|
||||
var toRemoveFrom = result.Filter(c => !c.ScheduleAsGroup).ToList();
|
||||
var toRemove = result.Filter(c => c.ScheduleAsGroup)
|
||||
.SelectMany(c => c.MediaItems.Map(i => i.Id))
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
foreach (CollectionWithItems collection in toRemoveFrom)
|
||||
{
|
||||
collection.MediaItems.RemoveAll(mi => toRemove.Contains(mi.Id));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<List<int>> PlayoutIdsUsingCollection(int collectionId) =>
|
||||
_dbConnection.QueryAsync<int>(
|
||||
@"SELECT DISTINCT p.Id
|
||||
FROM Playout p
|
||||
INNER JOIN ProgramSchedule PS on p.ProgramScheduleId = PS.Id
|
||||
INNER JOIN ProgramScheduleItem PSI on p.Anchor_NextScheduleItemId = PSI.Id
|
||||
WHERE PSI.CollectionId = @CollectionId",
|
||||
@"SELECT DISTINCT p.PlayoutId
|
||||
FROM PlayoutProgramScheduleAnchor p
|
||||
WHERE p.CollectionId = @CollectionId",
|
||||
new { CollectionId = collectionId })
|
||||
.Map(result => result.ToList());
|
||||
|
||||
public Task<List<int>> PlayoutIdsUsingMultiCollection(int multiCollectionId) =>
|
||||
_dbConnection.QueryAsync<int>(
|
||||
@"SELECT DISTINCT p.PlayoutId
|
||||
FROM PlayoutProgramScheduleAnchor p
|
||||
WHERE p.MultiCollectionId = @MultiCollectionId",
|
||||
new { MultiCollectionId = multiCollectionId })
|
||||
.Map(result => result.ToList());
|
||||
|
||||
public Task<bool> IsCustomPlaybackOrder(int collectionId) =>
|
||||
_dbConnection.QuerySingleAsync<bool>(
|
||||
@"SELECT IFNULL(MIN(UseCustomPlaybackOrder), 0) FROM Collection WHERE Id = @CollectionId",
|
||||
|
||||
Reference in New Issue
Block a user