Rebuild playouts with modified collections (#39)

* rebuild playouts when items are removed from collections

* rebuild playouts when items are added to collections

* simplify logic
This commit is contained in:
Jason Dove
2021-03-05 00:50:26 +00:00
committed by GitHub
parent c6ea2c88df
commit 363eb2c276
24 changed files with 176 additions and 88 deletions
@@ -30,8 +30,10 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
}
public async Task<Unit> AddMediaItem(int collectionId, int mediaItemId)
public async Task<bool> AddMediaItem(int collectionId, int mediaItemId)
{
var modified = false;
Option<Collection> maybeCollection = await _dbContext.Collections
.Include(c => c.MediaItems)
.OrderBy(c => c.Id)
@@ -52,12 +54,12 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
async mediaItem =>
{
collection.MediaItems.Add(mediaItem);
await _dbContext.SaveChangesAsync();
modified = await _dbContext.SaveChangesAsync() > 0;
});
}
});
return Unit.Default;
return modified;
}
public Task<Option<Collection>> Get(int id) =>
@@ -121,10 +123,10 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
public Task<Option<List<MediaItem>>> GetItems(int id) =>
Get(id).MapT(GetItemsForCollection).Bind(x => x.Sequence());
public Task Update(Collection collection)
public Task<bool> Update(Collection collection)
{
_dbContext.Collections.Update(collection);
return _dbContext.SaveChangesAsync();
return _dbContext.SaveChangesAsync().Map(result => result > 0);
}
public async Task Delete(int collectionId)
@@ -134,6 +136,16 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
await _dbContext.SaveChangesAsync();
}
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",
new { CollectionId = collectionId })
.Map(result => result.ToList());
private async Task<List<MediaItem>> GetItemsForCollection(Collection collection)
{
var result = new List<MediaItem>();
@@ -173,7 +173,7 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
await _dbConnection.ExecuteAsync(
"UPDATE PlexLibrary SET ShouldSyncItems = 0 WHERE Id IN @ids",
new { ids = libraryIds });
await _dbConnection.ExecuteAsync(
"UPDATE Library SET LastScan = null WHERE Id IN @ids",
new { ids = libraryIds });
@@ -122,7 +122,10 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
}
}
private async Task<Either<BaseError, PlexMovie>> AddPlexMovie(TvContext context, PlexLibrary library, PlexMovie item)
private async Task<Either<BaseError, PlexMovie>> AddPlexMovie(
TvContext context,
PlexLibrary library,
PlexMovie item)
{
try
{