Files
ersatztv/ErsatzTV.Infrastructure/Data/Repositories/PlayoutRepository.cs
T
Jason DoveandGitHub 871a031467 rework television media (#26)
* rework television media

* refactor poster saving

* television and movie views are working again

* remove dead code

* use paper styling for all cards

* add show poster, plot to seasons page

* remove missing shows; cleanup interfaces

* fix split show display (same show in different folders/sources)

* add placeholder "add to schedule" button

* no more duplicate television shows, even with the same show split across sources

* stop releasing CLI for now

* use season number as season placeholder

* add television shows to collections

* add television seasons to collections

* add television episodes to collections

* add movies to collections

* remove movies, shows, seasons, episodes from collections

* fix page width and menus

* fix buffer size defaults

* fix chronological episode ordering

* allow deleting media collections

* don't get stuck building a playout with an empty collection

* schedule editing and playouts work again

* minor cleanup

* remove dead code

* fix bugs with viewing movies as they are loading

* add scanner tests; support nested movie folders

* update collections docs

* rearrange order of schedule items

* add show and season to schedule

* delete schedules that use legacy collections, reset all posters

* move cleanup to new migration

* load fallback metadata when nfo fails; don't require metadata in ui

* update readme and screenshots
2021-02-22 00:54:41 +00:00

105 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Repositories;
using LanguageExt;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using static LanguageExt.Prelude;
namespace ErsatzTV.Infrastructure.Data.Repositories
{
public class PlayoutRepository : IPlayoutRepository
{
private readonly TvContext _dbContext;
public PlayoutRepository(TvContext dbContext) => _dbContext = dbContext;
public async Task<Playout> Add(Playout playout)
{
await _dbContext.Playouts.AddAsync(playout);
await _dbContext.SaveChangesAsync();
return playout;
}
public Task<Option<Playout>> Get(int id) =>
_dbContext.Playouts.SingleOrDefaultAsync(p => p.Id == id).Map(Optional);
public async Task<Option<Playout>> GetFull(int id) =>
await _dbContext.Playouts
.Include(p => p.Channel)
.Include(p => p.Items)
.Include(p => p.ProgramScheduleAnchors)
.Include(p => p.ProgramSchedule)
.ThenInclude(ps => ps.Items)
.ThenInclude(psi => psi.MediaCollection)
.Include(p => p.ProgramSchedule)
.ThenInclude(ps => ps.Items)
.ThenInclude(psi => psi.TelevisionShow)
.OrderBy(p => p.Id) // https://github.com/dotnet/efcore/issues/22579#issuecomment-694772289
.SingleOrDefaultAsync(p => p.Id == id);
public async Task<Option<PlayoutItem>> GetPlayoutItem(int channelId, DateTimeOffset now)
{
var p1 = new SqliteParameter("channelId", channelId);
var p2 = new SqliteParameter("now", now);
return await _dbContext.PlayoutItems
.FromSqlRaw(
"select i.* from playoutitems i inner join playouts p on i.playoutid = p.id where p.channelid = @channelId and i.start <= @now and i.finish > @now",
p1,
p2)
.Include(i => i.MediaItem)
.AsNoTracking()
.SingleOrDefaultAsync();
}
public Task<List<PlayoutItem>> GetPlayoutItems(int playoutId) =>
_dbContext.PlayoutItems
.Include(i => i.MediaItem)
.ThenInclude(m => (m as MovieMediaItem).Metadata)
.Include(i => i.MediaItem)
.ThenInclude(m => (m as TelevisionEpisodeMediaItem).Metadata)
.Filter(i => i.PlayoutId == playoutId)
.ToListAsync();
public Task<List<int>> GetPlayoutIdsForMediaItems(Seq<MediaItem> mediaItems)
{
var ids = string.Join(", ", mediaItems.Map(mi => mi.Id));
return _dbContext.Playouts.FromSqlRaw(
@"SELECT DISTINCT p.* FROM Playouts p
INNER JOIN ProgramScheduleItems psi on psi.ProgramScheduleId = p.ProgramScheduleId
INNER JOIN SimpleMediaCollections smc on smc.Id = psi.MediaCollectionId
INNER JOIN MediaItemSimpleMediaCollection mismc on mismc.SimpleMediaCollectionsId = smc.Id
WHERE mismc.ItemsId in ({0})
UNION
SELECT DISTINCT p.* FROM Playouts p
INNER JOIN ProgramScheduleItems psi on psi.ProgramScheduleId = p.ProgramScheduleId
INNER JOIN TelevisionMediaCollections tmc on tmc.Id = psi.MediaCollectionId
INNER JOIN MediaItems mi on mi.Metadata_Title = tmc.ShowTitle and (tmc.SeasonNumber is null or tmc.SeasonNumber = mi.Metadata_SeasonNumber)
WHERE mi.Id in ({0})",
ids).Select(p => p.Id).ToListAsync();
}
public Task<List<Playout>> GetAll() =>
_dbContext.Playouts
.Include(p => p.Channel)
.Include(p => p.ProgramSchedule)
.ToListAsync();
public async Task Update(Playout playout)
{
_dbContext.Playouts.Update(playout);
await _dbContext.SaveChangesAsync();
}
public async Task Delete(int playoutId)
{
Playout playout = await _dbContext.Playouts.FindAsync(playoutId);
_dbContext.Playouts.Remove(playout);
await _dbContext.SaveChangesAsync();
}
}
}