diff --git a/ErsatzTV.Core/AggregateModels/GenericIntegerId.cs b/ErsatzTV.Core/AggregateModels/GenericIntegerId.cs deleted file mode 100644 index fec0b0164..000000000 --- a/ErsatzTV.Core/AggregateModels/GenericIntegerId.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace ErsatzTV.Core.AggregateModels -{ - public record GenericIntegerId(int Id); -} diff --git a/ErsatzTV.Core/AggregateModels/MediaCollectionSummary.cs b/ErsatzTV.Core/AggregateModels/MediaCollectionSummary.cs deleted file mode 100644 index 0df552482..000000000 --- a/ErsatzTV.Core/AggregateModels/MediaCollectionSummary.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace ErsatzTV.Core.AggregateModels -{ - public record MediaCollectionSummary(int Id, string Name, int ItemCount, bool IsSimple); -} diff --git a/ErsatzTV.Core/AggregateModels/MediaItemSummary.cs b/ErsatzTV.Core/AggregateModels/MediaItemSummary.cs deleted file mode 100644 index be009ab73..000000000 --- a/ErsatzTV.Core/AggregateModels/MediaItemSummary.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace ErsatzTV.Core.AggregateModels -{ - public record MediaItemSummary(int MediaItemId, string Title, string SortTitle, string Subtitle, string Poster); -} diff --git a/ErsatzTV.Infrastructure/Data/Configurations/GenericIntegerIdConfiguration.cs b/ErsatzTV.Infrastructure/Data/Configurations/GenericIntegerIdConfiguration.cs deleted file mode 100644 index 406162533..000000000 --- a/ErsatzTV.Infrastructure/Data/Configurations/GenericIntegerIdConfiguration.cs +++ /dev/null @@ -1,12 +0,0 @@ -using ErsatzTV.Core.AggregateModels; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace ErsatzTV.Infrastructure.Data.Configurations -{ - public class GenericIntegerIdConfiguration : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) => - builder.HasNoKey().ToView("No table or view exists for GenericIntegerId"); - } -} diff --git a/ErsatzTV.Infrastructure/Data/Configurations/MediaCollectionSummaryConfiguration.cs b/ErsatzTV.Infrastructure/Data/Configurations/MediaCollectionSummaryConfiguration.cs deleted file mode 100644 index 97b1a6730..000000000 --- a/ErsatzTV.Infrastructure/Data/Configurations/MediaCollectionSummaryConfiguration.cs +++ /dev/null @@ -1,12 +0,0 @@ -using ErsatzTV.Core.AggregateModels; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace ErsatzTV.Infrastructure.Data.Configurations -{ - public class MediaCollectionSummaryConfiguration : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) => - builder.HasNoKey().ToView("No table or view exists for MediaCollectionSummary"); - } -} diff --git a/ErsatzTV.Infrastructure/Data/Configurations/MediaItemSummaryConfiguration.cs b/ErsatzTV.Infrastructure/Data/Configurations/MediaItemSummaryConfiguration.cs deleted file mode 100644 index 5bed7f586..000000000 --- a/ErsatzTV.Infrastructure/Data/Configurations/MediaItemSummaryConfiguration.cs +++ /dev/null @@ -1,12 +0,0 @@ -using ErsatzTV.Core.AggregateModels; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace ErsatzTV.Infrastructure.Data.Configurations -{ - public class MediaItemSummaryConfiguration : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) => - builder.HasNoKey().ToView("No table or view exists for MediaItemSummary"); - } -} diff --git a/ErsatzTV.Infrastructure/Data/Repositories/MediaCollectionRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/MediaCollectionRepository.cs index 812a63a70..e8bf06f68 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/MediaCollectionRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/MediaCollectionRepository.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Threading.Tasks; +using Dapper; using ErsatzTV.Core.Domain; using ErsatzTV.Core.Interfaces.Repositories; using LanguageExt; @@ -13,8 +15,13 @@ namespace ErsatzTV.Infrastructure.Data.Repositories public class MediaCollectionRepository : IMediaCollectionRepository { private readonly TvContext _dbContext; + private readonly IDbConnection _dbConnection; - public MediaCollectionRepository(TvContext dbContext) => _dbContext = dbContext; + public MediaCollectionRepository(TvContext dbContext, IDbConnection dbConnection) + { + _dbContext = dbContext; + _dbConnection = dbConnection; + } public async Task Add(SimpleMediaCollection collection) { @@ -110,17 +117,15 @@ namespace ErsatzTV.Infrastructure.Data.Repositories private async Task> GetTelevisionShowItems(SimpleMediaCollection collection) { - // TODO: would be nice to get the media items in one go, but ef... - List showItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw( - @"select tmi.Id + var parameters = new { CollectionId = collection.Id }; + IEnumerable showItemIds = await _dbConnection.QueryAsync( + @"select tmi.Id from TelevisionEpisodes tmi inner join TelevisionSeasons tsn on tsn.Id = tmi.SeasonId inner join TelevisionShows ts on ts.Id = tsn.TelevisionShowId inner join SimpleMediaCollectionShows s on s.TelevisionShowsId = ts.Id -where s.SimpleMediaCollectionsId = {0}", - collection.Id) - .Select(i => i.Id) - .ToListAsync(); +where s.SimpleMediaCollectionsId = @CollectionId", + parameters); return await _dbContext.TelevisionEpisodeMediaItems .AsNoTracking() @@ -131,16 +136,14 @@ where s.SimpleMediaCollectionsId = {0}", private async Task> GetTelevisionSeasonItems(SimpleMediaCollection collection) { - // TODO: would be nice to get the media items in one go, but ef... - List seasonItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw( - @"select tmi.Id + var parameters = new { CollectionId = collection.Id }; + IEnumerable seasonItemIds = await _dbConnection.QueryAsync( + @"select tmi.Id from TelevisionEpisodes tmi inner join TelevisionSeasons tsn on tsn.Id = tmi.SeasonId inner join SimpleMediaCollectionSeasons s on s.TelevisionSeasonsId = tsn.Id -where s.SimpleMediaCollectionsId = {0}", - collection.Id) - .Select(i => i.Id) - .ToListAsync(); +where s.SimpleMediaCollectionsId = @CollectionId", + parameters); return await _dbContext.TelevisionEpisodeMediaItems .AsNoTracking() @@ -151,14 +154,12 @@ where s.SimpleMediaCollectionsId = {0}", private async Task> GetTelevisionEpisodeItems(SimpleMediaCollection collection) { - // TODO: would be nice to get the media items in one go, but ef... - List episodeItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw( + var parameters = new { CollectionId = collection.Id }; + IEnumerable episodeItemIds = await _dbConnection.QueryAsync( @"select s.TelevisionEpisodesId as Id from SimpleMediaCollectionEpisodes s -where s.SimpleMediaCollectionsId = {0}", - collection.Id) - .Select(i => i.Id) - .ToListAsync(); +where s.SimpleMediaCollectionsId = @CollectionId", + parameters); return await _dbContext.TelevisionEpisodeMediaItems .AsNoTracking() diff --git a/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs index 8b7f3d42f..580ac99f5 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Threading.Tasks; +using Dapper; using ErsatzTV.Core; using ErsatzTV.Core.Domain; using ErsatzTV.Core.Interfaces.Repositories; @@ -14,8 +16,13 @@ namespace ErsatzTV.Infrastructure.Data.Repositories public class TelevisionRepository : ITelevisionRepository { private readonly TvContext _dbContext; + private readonly IDbConnection _dbConnection; - public TelevisionRepository(TvContext dbContext) => _dbContext = dbContext; + public TelevisionRepository(TvContext dbContext, IDbConnection dbConnection) + { + _dbContext = dbContext; + _dbConnection = dbConnection; + } public async Task Update(TelevisionShow show) { @@ -245,16 +252,14 @@ namespace ErsatzTV.Infrastructure.Data.Repositories public async Task> GetShowItems(int televisionShowId) { - // TODO: would be nice to get the media items in one go, but ef... - List showItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw( - @"select tmi.Id + var parameters = new { ShowId = televisionShowId }; + IEnumerable showItemIds = await _dbConnection.QueryAsync( + @"select tmi.Id from TelevisionEpisodes tmi inner join TelevisionSeasons tsn on tsn.Id = tmi.SeasonId inner join TelevisionShows ts on ts.Id = tsn.TelevisionShowId -where ts.Id = {0}", - televisionShowId) - .Select(i => i.Id) - .ToListAsync(); +where ts.Id = @ShowId", + parameters); return await _dbContext.TelevisionEpisodeMediaItems .AsNoTracking() @@ -265,15 +270,13 @@ where ts.Id = {0}", public async Task> GetSeasonItems(int televisionSeasonId) { - // TODO: would be nice to get the media items in one go, but ef... - List seasonItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw( - @"select tmi.Id + var parameters = new { SeasonId = televisionSeasonId }; + IEnumerable seasonItemIds = await _dbConnection.QueryAsync( + @"select tmi.Id from TelevisionEpisodes tmi inner join TelevisionSeasons tsn on tsn.Id = tmi.SeasonId -where tsn.Id = {0}", - televisionSeasonId) - .Select(i => i.Id) - .ToListAsync(); +where tsn.Id = @SeasonId", + parameters); return await _dbContext.TelevisionEpisodeMediaItems .AsNoTracking() diff --git a/ErsatzTV.Infrastructure/Data/TvContext.cs b/ErsatzTV.Infrastructure/Data/TvContext.cs index 688a8e885..d6a0854f6 100644 --- a/ErsatzTV.Infrastructure/Data/TvContext.cs +++ b/ErsatzTV.Infrastructure/Data/TvContext.cs @@ -1,5 +1,4 @@ -using ErsatzTV.Core.AggregateModels; -using ErsatzTV.Core.Domain; +using ErsatzTV.Core.Domain; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -34,11 +33,6 @@ namespace ErsatzTV.Infrastructure.Data public DbSet TelevisionShowMetadata { get; set; } public DbSet TelevisionSeasons { get; set; } - // support raw sql queries - public DbSet MediaCollectionSummaries { get; set; } - public DbSet GenericIntegerIds { get; set; } - public DbSet MediaItemSummaries { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseLoggerFactory(_loggerFactory); @@ -46,10 +40,6 @@ namespace ErsatzTV.Infrastructure.Data { base.OnModelCreating(builder); - builder.Ignore(); - builder.Ignore(); - builder.Ignore(); - builder.ApplyConfigurationsFromAssembly(typeof(TvContext).Assembly); } } diff --git a/ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj b/ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj index db9613c4a..c43fc02bc 100644 --- a/ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj +++ b/ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj @@ -6,6 +6,7 @@ + all diff --git a/ErsatzTV/Startup.cs b/ErsatzTV/Startup.cs index 85210d13b..ccff660fb 100644 --- a/ErsatzTV/Startup.cs +++ b/ErsatzTV/Startup.cs @@ -1,4 +1,5 @@ using System; +using System.Data; using System.IO; using System.Reflection; using System.Threading.Channels; @@ -27,6 +28,7 @@ using FluentValidation.AspNetCore; using MediatR; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -109,18 +111,21 @@ namespace ErsatzTV // string xmltvPath = Path.Combine(appDataFolder, "xmltv.xml"); // Log.Logger.Information("XMLTV is at {XmltvPath}", xmltvPath); + var connectionString = $"Data Source={FileSystemLayout.DatabasePath}"; + services.AddDbContext( options => options.UseSqlite( - $"Data Source={FileSystemLayout.DatabasePath}", + connectionString, o => { o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery); o.MigrationsAssembly("ErsatzTV.Infrastructure"); })); - services.AddDbContext( - options => options.UseSqlite($"Data Source={FileSystemLayout.LogDatabasePath}")); + services.AddDbContext(options => options.UseSqlite(connectionString)); + services.AddTransient(_ => new SqliteConnection(connectionString)); + services.AddMediatR(typeof(GetAllChannels).Assembly); services.AddRefitClient()