Browse Source

use dapper in a few places

pull/29/head
Jason Dove 6 years ago
parent
commit
fc132a0ac9
  1. 4
      ErsatzTV.Core/AggregateModels/GenericIntegerId.cs
  2. 4
      ErsatzTV.Core/AggregateModels/MediaCollectionSummary.cs
  3. 4
      ErsatzTV.Core/AggregateModels/MediaItemSummary.cs
  4. 12
      ErsatzTV.Infrastructure/Data/Configurations/GenericIntegerIdConfiguration.cs
  5. 12
      ErsatzTV.Infrastructure/Data/Configurations/MediaCollectionSummaryConfiguration.cs
  6. 12
      ErsatzTV.Infrastructure/Data/Configurations/MediaItemSummaryConfiguration.cs
  7. 43
      ErsatzTV.Infrastructure/Data/Repositories/MediaCollectionRepository.cs
  8. 33
      ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs
  9. 12
      ErsatzTV.Infrastructure/Data/TvContext.cs
  10. 1
      ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj
  11. 11
      ErsatzTV/Startup.cs

4
ErsatzTV.Core/AggregateModels/GenericIntegerId.cs

@ -1,4 +0,0 @@ @@ -1,4 +0,0 @@
namespace ErsatzTV.Core.AggregateModels
{
public record GenericIntegerId(int Id);
}

4
ErsatzTV.Core/AggregateModels/MediaCollectionSummary.cs

@ -1,4 +0,0 @@ @@ -1,4 +0,0 @@
namespace ErsatzTV.Core.AggregateModels
{
public record MediaCollectionSummary(int Id, string Name, int ItemCount, bool IsSimple);
}

4
ErsatzTV.Core/AggregateModels/MediaItemSummary.cs

@ -1,4 +0,0 @@ @@ -1,4 +0,0 @@
namespace ErsatzTV.Core.AggregateModels
{
public record MediaItemSummary(int MediaItemId, string Title, string SortTitle, string Subtitle, string Poster);
}

12
ErsatzTV.Infrastructure/Data/Configurations/GenericIntegerIdConfiguration.cs

@ -1,12 +0,0 @@ @@ -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<GenericIntegerId>
{
public void Configure(EntityTypeBuilder<GenericIntegerId> builder) =>
builder.HasNoKey().ToView("No table or view exists for GenericIntegerId");
}
}

12
ErsatzTV.Infrastructure/Data/Configurations/MediaCollectionSummaryConfiguration.cs

@ -1,12 +0,0 @@ @@ -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<MediaCollectionSummary>
{
public void Configure(EntityTypeBuilder<MediaCollectionSummary> builder) =>
builder.HasNoKey().ToView("No table or view exists for MediaCollectionSummary");
}
}

12
ErsatzTV.Infrastructure/Data/Configurations/MediaItemSummaryConfiguration.cs

@ -1,12 +0,0 @@ @@ -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<MediaItemSummary>
{
public void Configure(EntityTypeBuilder<MediaItemSummary> builder) =>
builder.HasNoKey().ToView("No table or view exists for MediaItemSummary");
}
}

43
ErsatzTV.Infrastructure/Data/Repositories/MediaCollectionRepository.cs

@ -1,7 +1,9 @@ @@ -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 @@ -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<SimpleMediaCollection> Add(SimpleMediaCollection collection)
{
@ -110,17 +117,15 @@ namespace ErsatzTV.Infrastructure.Data.Repositories @@ -110,17 +117,15 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
private async Task<List<TelevisionEpisodeMediaItem>> GetTelevisionShowItems(SimpleMediaCollection collection)
{
// TODO: would be nice to get the media items in one go, but ef...
List<int> showItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw(
@"select tmi.Id
var parameters = new { CollectionId = collection.Id };
IEnumerable<int> showItemIds = await _dbConnection.QueryAsync<int>(
@"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}", @@ -131,16 +136,14 @@ where s.SimpleMediaCollectionsId = {0}",
private async Task<List<TelevisionEpisodeMediaItem>> GetTelevisionSeasonItems(SimpleMediaCollection collection)
{
// TODO: would be nice to get the media items in one go, but ef...
List<int> seasonItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw(
@"select tmi.Id
var parameters = new { CollectionId = collection.Id };
IEnumerable<int> seasonItemIds = await _dbConnection.QueryAsync<int>(
@"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}", @@ -151,14 +154,12 @@ where s.SimpleMediaCollectionsId = {0}",
private async Task<List<TelevisionEpisodeMediaItem>> GetTelevisionEpisodeItems(SimpleMediaCollection collection)
{
// TODO: would be nice to get the media items in one go, but ef...
List<int> episodeItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw(
var parameters = new { CollectionId = collection.Id };
IEnumerable<int> episodeItemIds = await _dbConnection.QueryAsync<int>(
@"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()

33
ErsatzTV.Infrastructure/Data/Repositories/TelevisionRepository.cs

@ -1,7 +1,9 @@ @@ -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 @@ -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<bool> Update(TelevisionShow show)
{
@ -245,16 +252,14 @@ namespace ErsatzTV.Infrastructure.Data.Repositories @@ -245,16 +252,14 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
public async Task<List<TelevisionEpisodeMediaItem>> GetShowItems(int televisionShowId)
{
// TODO: would be nice to get the media items in one go, but ef...
List<int> showItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw(
@"select tmi.Id
var parameters = new { ShowId = televisionShowId };
IEnumerable<int> showItemIds = await _dbConnection.QueryAsync<int>(
@"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}", @@ -265,15 +270,13 @@ where ts.Id = {0}",
public async Task<List<TelevisionEpisodeMediaItem>> GetSeasonItems(int televisionSeasonId)
{
// TODO: would be nice to get the media items in one go, but ef...
List<int> seasonItemIds = await _dbContext.GenericIntegerIds.FromSqlRaw(
@"select tmi.Id
var parameters = new { SeasonId = televisionSeasonId };
IEnumerable<int> seasonItemIds = await _dbConnection.QueryAsync<int>(
@"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()

12
ErsatzTV.Infrastructure/Data/TvContext.cs

@ -1,5 +1,4 @@ @@ -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 @@ -34,11 +33,6 @@ namespace ErsatzTV.Infrastructure.Data
public DbSet<TelevisionShowMetadata> TelevisionShowMetadata { get; set; }
public DbSet<TelevisionSeason> TelevisionSeasons { get; set; }
// support raw sql queries
public DbSet<MediaCollectionSummary> MediaCollectionSummaries { get; set; }
public DbSet<GenericIntegerId> GenericIntegerIds { get; set; }
public DbSet<MediaItemSummary> MediaItemSummaries { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseLoggerFactory(_loggerFactory);
@ -46,10 +40,6 @@ namespace ErsatzTV.Infrastructure.Data @@ -46,10 +40,6 @@ namespace ErsatzTV.Infrastructure.Data
{
base.OnModelCreating(builder);
builder.Ignore<MediaCollectionSummary>();
builder.Ignore<GenericIntegerId>();
builder.Ignore<MediaItemSummary>();
builder.ApplyConfigurationsFromAssembly(typeof(TvContext).Assembly);
}
}

1
ErsatzTV.Infrastructure/ErsatzTV.Infrastructure.csproj

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
<PrivateAssets>all</PrivateAssets>

11
ErsatzTV/Startup.cs

@ -1,4 +1,5 @@ @@ -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; @@ -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 @@ -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<TvContext>(
options => options.UseSqlite(
$"Data Source={FileSystemLayout.DatabasePath}",
connectionString,
o =>
{
o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
o.MigrationsAssembly("ErsatzTV.Infrastructure");
}));
services.AddDbContext<LogContext>(
options => options.UseSqlite($"Data Source={FileSystemLayout.LogDatabasePath}"));
services.AddDbContext<LogContext>(options => options.UseSqlite(connectionString));
services.AddTransient<IDbConnection>(_ => new SqliteConnection(connectionString));
services.AddMediatR(typeof(GetAllChannels).Assembly);
services.AddRefitClient<IPlexTvApi>()

Loading…
Cancel
Save