From b9fac94eca3978a40fa14e9713470935efe1207f Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 24 Mar 2013 18:38:11 -0700 Subject: [PATCH] cleaned up history lookup. --- .../UpgradeHistorySpecificationFixture.cs | 8 ++++---- NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs | 10 +++++----- NzbDrone.Core/Datastore/BasicRepository.cs | 6 ++++++ .../Specifications/UpgradeHistorySpecification.cs | 2 +- NzbDrone.Core/History/History.cs | 2 +- NzbDrone.Core/History/HistoryRepository.cs | 6 +++--- NzbDrone.Core/History/HistoryService.cs | 8 ++++---- 7 files changed, 24 insertions(+), 18 deletions(-) diff --git a/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs b/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs index dd0c416f4..202fcbe44 100644 --- a/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs +++ b/NzbDrone.Core.Test/DecisionEngineTests/UpgradeHistorySpecificationFixture.cs @@ -67,9 +67,9 @@ namespace NzbDrone.Core.Test.DecisionEngineTests firstQuality = new QualityModel(Quality.Bluray1080p, true); secondQuality = new QualityModel(Quality.Bluray1080p, true); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 3)).Returns(firstQuality); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 4)).Returns(secondQuality); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 5)).Returns(null); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(1)).Returns(firstQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(2)).Returns(secondQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(3)).Returns(null); } private void WithFirstReportUpgradable() @@ -125,7 +125,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests parseResultSingle.Quality = new QualityModel(Quality.WEBDL1080p, false); firstQuality = new QualityModel(Quality.WEBDL1080p, false); - Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 3)).Returns(firstQuality); + Mocker.GetMock().Setup(c => c.GetBestQualityInHistory(1)).Returns(firstQuality); _upgradeHistory.IsSatisfiedBy(parseResultSingle).Should().BeFalse(); } diff --git a/NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs b/NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs index dfa4cce1d..559daaf00 100644 --- a/NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs +++ b/NzbDrone.Core.Test/HistoryTests/HistoryServiceTest.cs @@ -37,7 +37,7 @@ namespace NzbDrone.Core.Test.HistoryTests [Test] public void GetBestQualityInHistory_no_result() { - Subject.GetBestQualityInHistory(12, 12, 12).Should().Be(null); + Subject.GetBestQualityInHistory(12).Should().Be(null); } [Test] @@ -54,12 +54,12 @@ namespace NzbDrone.Core.Test.HistoryTests var history = Builder.CreateNew() .With(c => c.Id = 0) .With(h => h.Quality = new QualityModel(Quality.Bluray720p, true)) - .With(h => h.Episode = episode) + .With(h => h.EpisodeId = episode.Id) .Build(); Db.Insert(history); - var result = Subject.GetBestQualityInHistory(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); + var result = Subject.GetBestQualityInHistory(episode.Id); result.Should().NotBeNull(); result.Quality.Should().Be(Quality.Bluray720p); @@ -81,7 +81,7 @@ namespace NzbDrone.Core.Test.HistoryTests .CreateListOfSize(5) .All() .With(c => c.Id = 0) - .With(h => h.Episode = episode) + .With(h => h.EpisodeId = episode.Id) .TheFirst(1) .With(h => h.Quality = new QualityModel(Quality.DVD, true)) .TheNext(1) @@ -96,7 +96,7 @@ namespace NzbDrone.Core.Test.HistoryTests Db.InsertMany(history); - var result = Subject.GetBestQualityInHistory(episode.SeriesId, episode.SeasonNumber, episode.EpisodeNumber); + var result = Subject.GetBestQualityInHistory(episode.Id); result.Should().NotBeNull(); result.Quality.Should().Be(Quality.Bluray720p); diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index 968b59920..5b175978f 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -29,6 +29,7 @@ namespace NzbDrone.Core.Datastore bool HasItems(); void DeleteMany(IEnumerable ids); void UpdateFields(TModel model, Expression> onlyFields); + List Where(SqlExpressionVisitor expression); } public class BasicRepository : IBasicRepository where TModel : ModelBase, new() @@ -93,6 +94,11 @@ namespace NzbDrone.Core.Datastore return _database.Select(predicate); } + public List Where(SqlExpressionVisitor expression) + { + return _database.Select(expression); + } + public TModel Insert(TModel model) { if (model.Id != 0) diff --git a/NzbDrone.Core/DecisionEngine/Specifications/UpgradeHistorySpecification.cs b/NzbDrone.Core/DecisionEngine/Specifications/UpgradeHistorySpecification.cs index 8f87fac16..da65f6bc1 100644 --- a/NzbDrone.Core/DecisionEngine/Specifications/UpgradeHistorySpecification.cs +++ b/NzbDrone.Core/DecisionEngine/Specifications/UpgradeHistorySpecification.cs @@ -29,7 +29,7 @@ namespace NzbDrone.Core.DecisionEngine.Specifications { foreach (var episode in subject.Episodes) { - var bestQualityInHistory = _historyService.GetBestQualityInHistory(subject.Series.Id, episode.SeasonNumber, episode.EpisodeNumber); + var bestQualityInHistory = _historyService.GetBestQualityInHistory(episode.Id); if (bestQualityInHistory != null) { _logger.Trace("Comparing history quality with report. History is {0}", bestQualityInHistory); diff --git a/NzbDrone.Core/History/History.cs b/NzbDrone.Core/History/History.cs index a9622a3dc..6ea64f968 100644 --- a/NzbDrone.Core/History/History.cs +++ b/NzbDrone.Core/History/History.cs @@ -7,6 +7,7 @@ namespace NzbDrone.Core.History { public class History : ModelBase { + public int EpisodeId { get; set; } public string NzbTitle { get; set; } public QualityModel Quality { get; set; } public DateTime Date { get; set; } @@ -14,6 +15,5 @@ namespace NzbDrone.Core.History public string NzbInfoUrl { get; set; } public string ReleaseGroup { get; set; } - public Episode Episode { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Core/History/HistoryRepository.cs b/NzbDrone.Core/History/HistoryRepository.cs index e29da8cd4..6774d0bd4 100644 --- a/NzbDrone.Core/History/HistoryRepository.cs +++ b/NzbDrone.Core/History/HistoryRepository.cs @@ -9,7 +9,7 @@ namespace NzbDrone.Core.History public interface IHistoryRepository : IBasicRepository { void Trim(); - QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber); + QualityModel GetBestQualityInHistory(int episodeId); } public class HistoryRepository : BasicRepository, IHistoryRepository @@ -26,9 +26,9 @@ namespace NzbDrone.Core.History } - public QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) + public QualityModel GetBestQualityInHistory(int episodeId) { - var history = Where(c => c.Episode.Series.Id == seriesId && c.Episode.SeasonNumber == seasonNumber && c.Episode.EpisodeNumber == episodeNumber) + var history = Where(c => c.EpisodeId == episodeId) .OrderByDescending(c => c.Quality).FirstOrDefault(); if (history != null) diff --git a/NzbDrone.Core/History/HistoryService.cs b/NzbDrone.Core/History/HistoryService.cs index 59efb3389..21b042204 100644 --- a/NzbDrone.Core/History/HistoryService.cs +++ b/NzbDrone.Core/History/HistoryService.cs @@ -13,7 +13,7 @@ namespace NzbDrone.Core.History List All(); void Purge(); void Trim(); - QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber); + QualityModel GetBestQualityInHistory(int episodeId); } public class HistoryService : IHistoryService, IHandle @@ -43,9 +43,9 @@ namespace NzbDrone.Core.History _historyRepository.Trim(); } - public virtual QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber) + public virtual QualityModel GetBestQualityInHistory(int episodeId) { - return _historyRepository.GetBestQualityInHistory(seriesId, seasonNumber, episodeNumber); + return _historyRepository.GetBestQualityInHistory(episodeId); } public void Handle(EpisodeGrabbedEvent message) @@ -58,7 +58,7 @@ namespace NzbDrone.Core.History Indexer = message.ParseResult.Indexer, Quality = message.ParseResult.Quality, NzbTitle = message.ParseResult.OriginalString, - Episode = episode, + EpisodeId = episode.Id, NzbInfoUrl = message.ParseResult.NzbInfoUrl, ReleaseGroup = message.ParseResult.ReleaseGroup, };