From 894945f65276bdec572f2b71d20e238e00e879be Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Tue, 17 Oct 2017 15:03:00 +0100 Subject: [PATCH] Made a generic repository to use !minor --- .../Rule/Search/RadarrCacheRuleTests.cs | 115 +----------------- .../Rule/Rules/Search/RadarrCacheRule.cs | 18 ++- src/Ombi.DependencyInjection/IocExtensions.cs | 1 + src/Ombi.Store/Context/OmbiContext.cs | 3 +- .../Repository/IPlexContentRepository.cs | 5 +- src/Ombi.Store/Repository/IRepository.cs | 22 ++++ .../Repository/PlexContentRepository.cs | 26 +--- src/Ombi.Store/Repository/Repository.cs | 58 +++++++++ 8 files changed, 99 insertions(+), 149 deletions(-) create mode 100644 src/Ombi.Store/Repository/IRepository.cs create mode 100644 src/Ombi.Store/Repository/Repository.cs diff --git a/src/Ombi.Core.Tests/Rule/Search/RadarrCacheRuleTests.cs b/src/Ombi.Core.Tests/Rule/Search/RadarrCacheRuleTests.cs index 58a8f127c..914112d5b 100644 --- a/src/Ombi.Core.Tests/Rule/Search/RadarrCacheRuleTests.cs +++ b/src/Ombi.Core.Tests/Rule/Search/RadarrCacheRuleTests.cs @@ -1,16 +1,12 @@ using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; -using System.Threading; using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Query.Internal; using Moq; using NUnit.Framework; using Ombi.Core.Models.Search; using Ombi.Core.Rule.Rules.Search; -using Ombi.Store.Context; using Ombi.Store.Entities; +using Ombi.Store.Repository; namespace Ombi.Core.Tests.Rule.Search { @@ -19,38 +15,23 @@ namespace Ombi.Core.Tests.Rule.Search [SetUp] public void Setup() { - ContextMock = new Mock(); + ContextMock = new Mock>(); Rule = new RadarrCacheRule(ContextMock.Object); } private RadarrCacheRule Rule { get; set; } - private Mock ContextMock { get; set; } + private Mock> ContextMock { get; set; } [Test] - [Ignore("EF IAsyncQueryProvider")] public async Task Should_ReturnApproved_WhenMovieIsInRadarr() { var list = new List(){new RadarrCache { TheMovieDbId = 123 }}.AsQueryable(); - var radarrMock = new Mock>(); - radarrMock.As>() - .Setup(m => m.GetEnumerator()) - .Returns(new TestAsyncEnumerator(list.GetEnumerator())); - - - radarrMock.As>() - .Setup(m => m.Provider) - .Returns(new TestAsyncQueryProvider(list.Provider)); - - radarrMock.As>().Setup(m => m.Expression).Returns(list.Expression); - radarrMock.As>().Setup(m => m.ElementType).Returns(list.ElementType); - radarrMock.As>().Setup(m => m.GetEnumerator()).Returns(() => list.GetEnumerator()); - - - ContextMock.Setup(c => c.Set()).Returns(radarrMock.Object); + + ContextMock.Setup(x => x.GetAll()).Returns(list); var request = new SearchMovieViewModel { Id = 123 }; var result =await Rule.Execute(request); @@ -61,7 +42,6 @@ namespace Ombi.Core.Tests.Rule.Search [Test] - [Ignore("EF IAsyncQueryProvider")] public async Task Should_ReturnNotApproved_WhenMovieIsNotInRadarr() { var list = DbHelper.GetQueryableMockDbSet(new RadarrCache @@ -69,7 +49,7 @@ namespace Ombi.Core.Tests.Rule.Search TheMovieDbId = 000012 }); - ContextMock.Setup(x => x.RadarrCache).Returns(list); + ContextMock.Setup(x => x.GetAll()).Returns(list); var request = new SearchMovieViewModel { Id = 123 }; var result = await Rule.Execute(request); @@ -78,87 +58,4 @@ namespace Ombi.Core.Tests.Rule.Search Assert.False(request.Approved); } } - - internal class TestAsyncQueryProvider : IAsyncQueryProvider - { - private readonly IQueryProvider _inner; - - internal TestAsyncQueryProvider(IQueryProvider inner) - { - _inner = inner; - } - - public IQueryable CreateQuery(Expression expression) - { - return new TestAsyncEnumerable(expression); - } - - public IQueryable CreateQuery(Expression expression) - { - return new TestAsyncEnumerable(expression); - } - - public object Execute(Expression expression) - { - return _inner.Execute(expression); - } - - public TResult Execute(Expression expression) - { - return _inner.Execute(expression); - } - - public IAsyncEnumerable ExecuteAsync(Expression expression) - { - return new TestAsyncEnumerable(expression); - } - - public Task ExecuteAsync(Expression expression, CancellationToken cancellationToken) - { - return Task.FromResult(Execute(expression)); - } - } - - internal class TestAsyncEnumerable : EnumerableQuery, IAsyncEnumerable, IQueryable - { - public TestAsyncEnumerable(IEnumerable enumerable) - : base(enumerable) - { } - - public TestAsyncEnumerable(Expression expression) - : base(expression) - { } - - public IAsyncEnumerator GetEnumerator() - { - return new TestAsyncEnumerator(this.AsEnumerable().GetEnumerator()); - } - - IQueryProvider IQueryable.Provider - { - get { return new TestAsyncQueryProvider(this); } - } - } - - internal class TestAsyncEnumerator : IAsyncEnumerator - { - private readonly IEnumerator _inner; - - public TestAsyncEnumerator(IEnumerator inner) - { - _inner = inner; - } - - public void Dispose() - { - _inner.Dispose(); - } - - public T Current => _inner.Current; - - public Task MoveNext(CancellationToken cancellationToken) - { - return Task.FromResult(_inner.MoveNext()); - } - } } diff --git a/src/Ombi.Core/Rule/Rules/Search/RadarrCacheRule.cs b/src/Ombi.Core/Rule/Rules/Search/RadarrCacheRule.cs index 1c936012a..14564b7d9 100644 --- a/src/Ombi.Core/Rule/Rules/Search/RadarrCacheRule.cs +++ b/src/Ombi.Core/Rule/Rules/Search/RadarrCacheRule.cs @@ -1,35 +1,33 @@ using System.Linq; using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; using Ombi.Core.Models.Search; using Ombi.Core.Rule.Interfaces; -using Ombi.Store.Context; using Ombi.Store.Entities; +using Ombi.Store.Repository; namespace Ombi.Core.Rule.Rules.Search { public class RadarrCacheRule : BaseSearchRule, IRules { - public RadarrCacheRule(IOmbiContext ctx) + public RadarrCacheRule(IRepository db) { - _ctx = ctx; + _db = db; } - private readonly IOmbiContext _ctx; + private readonly IRepository _db; - public async Task Execute(SearchViewModel obj) + public Task Execute(SearchViewModel obj) { if (obj.Type == RequestType.Movie) { // Check if it's in Radarr - var result = await _ctx.RadarrCache.FirstOrDefaultAsync(x => x.TheMovieDbId == obj.Id); + var result = _db.GetAll().FirstOrDefault(x => x.TheMovieDbId == obj.Id); if (result != null) { - obj.Approved = - true; // It's in radarr so it's approved... Maybe have a new property called "Processing" or something? + obj.Approved = true; // It's in radarr so it's approved... Maybe have a new property called "Processing" or something? } } - return Success(); + return Task.FromResult(Success()); } } } \ No newline at end of file diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index 2d4aaa814..0f2e49ad4 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -115,6 +115,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(typeof(ISettingsService<>), typeof(SettingsService<>)); + services.AddTransient(typeof(IRepository<>), typeof(Repository<>)); } public static void RegisterServices(this IServiceCollection services) { diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index 7dd5e2711..c34470e51 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -66,8 +66,7 @@ namespace Ombi.Store.Context .WithMany(b => b.Episodes) .HasPrincipalKey(x => x.EmbyId) .HasForeignKey(p => p.ParentId); - - builder.Ignore(); + base.OnModelCreating(builder); } diff --git a/src/Ombi.Store/Repository/IPlexContentRepository.cs b/src/Ombi.Store/Repository/IPlexContentRepository.cs index 85e61d38e..7b9ae7c9a 100644 --- a/src/Ombi.Store/Repository/IPlexContentRepository.cs +++ b/src/Ombi.Store/Repository/IPlexContentRepository.cs @@ -5,12 +5,10 @@ using Ombi.Store.Entities; namespace Ombi.Store.Repository { - public interface IPlexContentRepository + public interface IPlexContentRepository : IRepository { Task Add(PlexContent content); - Task AddRange(IEnumerable content); Task ContentExists(string providerId); - Task> GetAll(); Task Get(string providerId); Task GetByKey(int key); Task Update(PlexContent existingContent); @@ -18,6 +16,5 @@ namespace Ombi.Store.Repository Task Add(PlexEpisode content); Task GetEpisodeByKey(int key); Task AddRange(IEnumerable content); - IQueryable Get(); } } \ No newline at end of file diff --git a/src/Ombi.Store/Repository/IRepository.cs b/src/Ombi.Store/Repository/IRepository.cs new file mode 100644 index 000000000..15702572d --- /dev/null +++ b/src/Ombi.Store/Repository/IRepository.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Query; +using Ombi.Store.Entities; + +namespace Ombi.Store.Repository +{ + public interface IRepository where T : Entity + { + Task Find(object key); + IQueryable GetAll(); + Task FirstOrDefaultAsync(Expression> predicate); + Task AddRange(IEnumerable content); + + IIncludableQueryable Include( + IQueryable source, Expression> navigationPropertyPath) + where TEntity : class; + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Repository/PlexContentRepository.cs b/src/Ombi.Store/Repository/PlexContentRepository.cs index 196d47021..10d464d16 100644 --- a/src/Ombi.Store/Repository/PlexContentRepository.cs +++ b/src/Ombi.Store/Repository/PlexContentRepository.cs @@ -34,49 +34,27 @@ using Ombi.Store.Entities; namespace Ombi.Store.Repository { - public class PlexContentRepository : IPlexContentRepository + public class PlexContentRepository : Repository, IPlexContentRepository { - public PlexContentRepository(IOmbiContext db) + public PlexContentRepository(IOmbiContext db) : base(db) { Db = db; } private IOmbiContext Db { get; } - public async Task> GetAll() - { - return await Db.PlexContent.ToListAsync(); - } - - public async Task AddRange(IEnumerable content) - { - Db.PlexContent.AddRange(content); - await Db.SaveChangesAsync(); - } public async Task ContentExists(string providerId) { return await Db.PlexContent.AnyAsync(x => x.ProviderId == providerId); } - public async Task Add(PlexContent content) - { - await Db.PlexContent.AddAsync(content); - await Db.SaveChangesAsync(); - return content; - } - public async Task Get(string providerId) { return await Db.PlexContent.FirstOrDefaultAsync(x => x.ProviderId == providerId); } - public IQueryable Get() - { - return Db.PlexContent.AsQueryable(); - } - public async Task GetByKey(int key) { return await Db.PlexContent.Include(x => x.Seasons).FirstOrDefaultAsync(x => x.Key == key); diff --git a/src/Ombi.Store/Repository/Repository.cs b/src/Ombi.Store/Repository/Repository.cs new file mode 100644 index 000000000..dbd0cfe7f --- /dev/null +++ b/src/Ombi.Store/Repository/Repository.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query; +using Ombi.Store.Context; +using Ombi.Store.Entities; + +namespace Ombi.Store.Repository +{ + public class Repository : IRepository where T : Entity + { + public Repository(IOmbiContext ctx) + { + _ctx = ctx; + _db = _ctx.Set(); + } + private readonly DbSet _db; + private readonly IOmbiContext _ctx; + + public async Task Find(object key) + { + return await _db.FindAsync(key); + } + + public IQueryable GetAll() + { + return _db.AsQueryable(); + } + + public async Task FirstOrDefaultAsync(Expression> predicate) + { + return await _db.FirstOrDefaultAsync(predicate); + } + + public async Task AddRange(IEnumerable content) + { + _db.AddRange(content); + await _ctx.SaveChangesAsync(); + } + + public async Task Add(T content) + { + await _db.AddAsync(content); + await _ctx.SaveChangesAsync(); + return content; + } + + public IIncludableQueryable Include( + IQueryable source, Expression> navigationPropertyPath) + where TEntity : class + { + return source.Include(navigationPropertyPath); + } + } +} \ No newline at end of file