1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

cleaned up history lookup.

This commit is contained in:
kay.one 2013-03-24 18:38:11 -07:00
parent cd6f0fc55c
commit b9fac94eca
7 changed files with 24 additions and 18 deletions

View File

@ -67,9 +67,9 @@ public void Setup()
firstQuality = new QualityModel(Quality.Bluray1080p, true);
secondQuality = new QualityModel(Quality.Bluray1080p, true);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 3)).Returns(firstQuality);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 4)).Returns(secondQuality);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 5)).Returns<QualityModel>(null);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(1)).Returns(firstQuality);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(2)).Returns(secondQuality);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(3)).Returns<QualityModel>(null);
}
private void WithFirstReportUpgradable()
@ -125,7 +125,7 @@ public void should_not_be_upgradable_if_episode_is_of_same_quality_as_existing()
parseResultSingle.Quality = new QualityModel(Quality.WEBDL1080p, false);
firstQuality = new QualityModel(Quality.WEBDL1080p, false);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(fakeSeries.Id, 12, 3)).Returns(firstQuality);
Mocker.GetMock<IHistoryService>().Setup(c => c.GetBestQualityInHistory(1)).Returns(firstQuality);
_upgradeHistory.IsSatisfiedBy(parseResultSingle).Should().BeFalse();
}

View File

@ -37,7 +37,7 @@ public void Trim_Items()
[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 @@ public void GetBestQualityInHistory_single_result()
var history = Builder<History.History>.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 @@ public void GetBestQualityInHistory_should_return_highest_result()
.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 @@ public void GetBestQualityInHistory_should_return_highest_result()
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);

View File

@ -29,6 +29,7 @@ namespace NzbDrone.Core.Datastore
bool HasItems();
void DeleteMany(IEnumerable<int> ids);
void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields);
List<TModel> Where(SqlExpressionVisitor<TModel> expression);
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
@ -93,6 +94,11 @@ public List<TModel> Where(Expression<Func<TModel, bool>> predicate)
return _database.Select(predicate);
}
public List<TModel> Where(SqlExpressionVisitor<TModel> expression)
{
return _database.Select(expression);
}
public TModel Insert(TModel model)
{
if (model.Id != 0)

View File

@ -29,7 +29,7 @@ public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
{
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);

View File

@ -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 @@ public class History : ModelBase
public string NzbInfoUrl { get; set; }
public string ReleaseGroup { get; set; }
public Episode Episode { get; set; }
}
}

View File

@ -9,7 +9,7 @@ namespace NzbDrone.Core.History
public interface IHistoryRepository : IBasicRepository<History>
{
void Trim();
QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber);
QualityModel GetBestQualityInHistory(int episodeId);
}
public class HistoryRepository : BasicRepository<History>, IHistoryRepository
@ -26,9 +26,9 @@ public void Trim()
}
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)

View File

@ -13,7 +13,7 @@ public interface IHistoryService
List<History> All();
void Purge();
void Trim();
QualityModel GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber);
QualityModel GetBestQualityInHistory(int episodeId);
}
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>
@ -43,9 +43,9 @@ public virtual void Trim()
_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 @@ public void Handle(EpisodeGrabbedEvent message)
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,
};