HistoryProvider now uses PetaPoco

pull/2/head
kay.one 14 years ago
parent 46ec4fa3ba
commit 3ef17273fd

@ -23,38 +23,65 @@ namespace NzbDrone.Core.Test
public void AllItems() public void AllItems()
{ {
//Setup //Setup
var episode = new Episode var historyItem = Builder<History>.CreateListOfSize(10).Build();
{
AirDate = DateTime.Today.AddDays(-1),
EpisodeId = 1234,
EpisodeNumber = 5,
Overview = "This is an Overview",
SeasonNumber = 1,
SeriesId = 5656
};
var list = new List<History> var mocker = new AutoMoqer();
{ var db = MockLib.GetEmptyDatabase();
new History mocker.SetConstant(db);
db.InsertMany(historyItem);
//Act
var result = mocker.Resolve<HistoryProvider>().AllItems();
//Assert
result.Should().HaveSameCount(historyItem);
}
[Test]
public void PurgeItem()
{ {
HistoryId = new int(), //Setup
Date = DateTime.Now, var historyItem = Builder<History>.CreateListOfSize(10).Build();
IsProper = false,
Quality = QualityTypes.SDTV, var mocker = new AutoMoqer();
EpisodeId = episode.EpisodeId var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
db.InsertMany(historyItem);
//Act
db.Fetch<History>().Should().HaveCount(10);
mocker.Resolve<HistoryProvider>().Purge();
//Assert
db.Fetch<History>().Should().HaveCount(0);
} }
};
var repo = new Mock<IRepository>(); [Test]
repo.Setup(r => r.All<History>()).Returns(list.AsQueryable()); public void Trim_Items()
{
//Setup
var historyItem = Builder<History>.CreateListOfSize(20)
.WhereTheFirst(10).Have(c => c.Date = DateTime.Now)
.AndTheNext(10).Have(c => c.Date = DateTime.Now.AddDays(-31))
.Build();
var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
db.InsertMany(historyItem);
var target = new HistoryProvider(repo.Object);
//Act //Act
var result = target.AllItems(); db.Fetch<History>().Should().HaveCount(20);
mocker.Resolve<HistoryProvider>().Trim();
//Assert //Assert
Assert.AreEqual(result.Count(), 1); db.Fetch<History>().Should().HaveCount(10);
} }
@ -63,7 +90,7 @@ namespace NzbDrone.Core.Test
{ {
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.SetConstant(MockLib.GetEmptyRepository()); mocker.SetConstant(MockLib.GetEmptyDatabase());
//Act //Act
var result = mocker.Resolve<HistoryProvider>().GetBestQualityInHistory(12); var result = mocker.Resolve<HistoryProvider>().GetBestQualityInHistory(12);
@ -77,12 +104,12 @@ namespace NzbDrone.Core.Test
{ {
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
var db = MockLib.GetEmptyDatabase();
var repo = MockLib.GetEmptyRepository();
var history = Builder<History>.CreateNew() var history = Builder<History>.CreateNew()
.With(h => h.Quality = QualityTypes.Bluray720p).Build(); .With(h => h.Quality = QualityTypes.Bluray720p).Build();
repo.Add(history);
mocker.SetConstant(repo); db.Insert(history);
mocker.SetConstant(db);
//Act //Act
var result = mocker.Resolve<HistoryProvider>().GetBestQualityInHistory(history.EpisodeId); var result = mocker.Resolve<HistoryProvider>().GetBestQualityInHistory(history.EpisodeId);
@ -97,35 +124,40 @@ namespace NzbDrone.Core.Test
{ {
//Arange //Arange
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
var repo = MockLib.GetEmptyRepository(); var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(repo); mocker.SetConstant(db);
var episodes = MockLib.GetFakeEpisodes(1); var episode = Builder<Episode>.CreateNew().Build();
repo.AddMany(episodes);
var episode = episodes[5]; const QualityTypes quality = QualityTypes.HDTV;
const bool proper = true;
var history = new History var history = new History
{ {
Date = DateTime.Now, Date = DateTime.Now,
EpisodeId = episode.EpisodeId, EpisodeId = episode.EpisodeId,
SeriesId = episode.SeriesId,
NzbTitle = "my title", NzbTitle = "my title",
Indexer = "Fake Indexer" Indexer = "Fake Indexer",
Quality = quality,
IsProper = proper
}; };
//Act //Act
mocker.Resolve<HistoryProvider>().Add(history); mocker.Resolve<HistoryProvider>().Add(history);
//Assert //Assert
var storedHistory = repo.All<History>(); var storedHistory = db.Fetch<History>();
var newHistiory = repo.All<History>().First();
storedHistory.Should().HaveCount(1); storedHistory.Should().HaveCount(1);
Assert.AreEqual(history.Date, newHistiory.Date); Assert.AreEqual(history.Date, storedHistory.First().Date);
Assert.AreEqual(history.EpisodeId, newHistiory.EpisodeId); Assert.AreEqual(history.EpisodeId, storedHistory.First().EpisodeId);
Assert.AreEqual(history.NzbTitle, newHistiory.NzbTitle); Assert.AreEqual(history.SeriesId, storedHistory.First().SeriesId);
Assert.AreEqual(history.Indexer, newHistiory.Indexer); Assert.AreEqual(history.NzbTitle, storedHistory.First().NzbTitle);
Assert.AreEqual(history.Indexer, storedHistory.First().Indexer);
Assert.AreEqual(history.Quality, storedHistory.First().Quality);
Assert.AreEqual(history.IsProper, storedHistory.First().IsProper);
} }

@ -5,47 +5,46 @@ using Ninject;
using NLog; using NLog;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality; using PetaPoco;
using SubSonic.Repository;
namespace NzbDrone.Core.Providers namespace NzbDrone.Core.Providers
{ {
public class HistoryProvider public class HistoryProvider
{ {
private readonly IDatabase _database;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly IRepository _repository;
[Inject] [Inject]
public HistoryProvider(IRepository repository) public HistoryProvider(IDatabase database)
{ {
_repository = repository; _database = database;
} }
public HistoryProvider() public HistoryProvider()
{ {
} }
public virtual IQueryable<History> AllItems() public virtual IEnumerable<History> AllItems()
{ {
return _repository.All<History>(); return _database.Query<History>("");
} }
public virtual void Purge() public virtual void Purge()
{ {
_repository.DeleteMany(AllItems()); _database.Delete<History>("");
Logger.Info("History has been Purged"); Logger.Info("History has been Purged");
} }
public virtual void Trim() public virtual void Trim()
{ {
var old = AllItems().Where(h => h.Date < DateTime.Now.AddDays(-30)); _database.Delete<History>("WHERE Date < @0", DateTime.Now.AddDays(-30).Date);
_repository.DeleteMany(old);
Logger.Info("History has been trimmed, items older than 30 days have been removed"); Logger.Info("History has been trimmed, items older than 30 days have been removed");
} }
public virtual void Add(History item) public virtual void Add(History item)
{ {
_repository.Add(item); _database.Insert(item);
Logger.Debug("Item added to history: {0}", item.NzbTitle); Logger.Debug("Item added to history: {0}", item.NzbTitle);
} }
@ -55,15 +54,5 @@ namespace NzbDrone.Core.Providers
return history.FirstOrDefault(); return history.FirstOrDefault();
} }
public virtual void Delete(int historyId)
{
_repository.Delete<History>(historyId);
}
public virtual void DeleteForEpisode(int episodeId)
{
_repository.DeleteMany<History>(h => h.EpisodeId == episodeId);
}
} }
} }

@ -1,24 +1,22 @@
using System; using System;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
using NzbDrone.Core.Repository.Quality; using NzbDrone.Core.Repository.Quality;
using PetaPoco;
using SubSonic.SqlGeneration.Schema; using SubSonic.SqlGeneration.Schema;
namespace NzbDrone.Core.Repository namespace NzbDrone.Core.Repository
{ {
[PrimaryKey("HistoryId")]
public class History public class History
{ {
[SubSonicPrimaryKey] public int HistoryId { get; set; }
public virtual int HistoryId { get; set; }
public virtual int EpisodeId { get; set; } public int EpisodeId { get; set; }
public int SeriesId { get; set; }
public string NzbTitle { get; set; } public string NzbTitle { get; set; }
public QualityTypes Quality { get; set; } public QualityTypes Quality { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
public bool IsProper { get; set; } public bool IsProper { get; set; }
public string Indexer { get; set; } public string Indexer { get; set; }
[SubSonicToOneRelation(ThisClassContainsJoinKey = true)]
public virtual Episode Episode { get; protected set; }
} }
} }

@ -12,10 +12,12 @@ namespace NzbDrone.Web.Controllers
public class HistoryController : Controller public class HistoryController : Controller
{ {
private readonly HistoryProvider _historyProvider; private readonly HistoryProvider _historyProvider;
private readonly EpisodeProvider _episodeProvider;
public HistoryController(HistoryProvider historyProvider) public HistoryController(HistoryProvider historyProvider, EpisodeProvider episodeProvider)
{ {
_historyProvider = historyProvider; _historyProvider = historyProvider;
_episodeProvider = episodeProvider;
} }
// //
@ -51,8 +53,7 @@ namespace NzbDrone.Web.Controllers
foreach (var item in historyDb) foreach (var item in historyDb)
{ {
var episode = item.Episode; var episode = _episodeProvider.GetEpisode(item.EpisodeId);
var series = episode.Series;
history.Add(new HistoryModel history.Add(new HistoryModel
{ {
@ -61,7 +62,7 @@ namespace NzbDrone.Web.Controllers
EpisodeNumber = episode.EpisodeNumber, EpisodeNumber = episode.EpisodeNumber,
EpisodeTitle = episode.Title, EpisodeTitle = episode.Title,
EpisodeOverview = episode.Overview, EpisodeOverview = episode.Overview,
SeriesTitle = series.Title, SeriesTitle = episode.Series.Title,
NzbTitle = item.NzbTitle, NzbTitle = item.NzbTitle,
Quality = item.Quality.ToString(), Quality = item.Quality.ToString(),
IsProper = item.IsProper, IsProper = item.IsProper,

Loading…
Cancel
Save