2011-01-28 22:10:22 -08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2011-06-13 18:23:04 -07:00
|
|
|
using Ninject;
|
2011-01-28 22:10:22 -08:00
|
|
|
using NLog;
|
2011-05-28 12:23:35 -07:00
|
|
|
using NzbDrone.Core.Model;
|
2011-01-28 22:10:22 -08:00
|
|
|
using NzbDrone.Core.Repository;
|
2011-06-16 23:59:13 -07:00
|
|
|
using PetaPoco;
|
2011-01-28 22:10:22 -08:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2011-04-08 16:58:46 -07:00
|
|
|
public class HistoryProvider
|
2011-01-28 22:10:22 -08:00
|
|
|
{
|
2011-06-16 23:59:13 -07:00
|
|
|
private readonly IDatabase _database;
|
2011-01-28 22:10:22 -08:00
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-06-16 23:59:13 -07:00
|
|
|
|
2011-01-28 22:10:22 -08:00
|
|
|
|
2011-06-13 18:23:04 -07:00
|
|
|
[Inject]
|
2011-06-16 23:59:13 -07:00
|
|
|
public HistoryProvider(IDatabase database)
|
2011-01-28 22:10:22 -08:00
|
|
|
{
|
2011-06-16 23:59:13 -07:00
|
|
|
_database = database;
|
2011-01-28 22:10:22 -08:00
|
|
|
}
|
|
|
|
|
2011-04-09 17:14:51 -07:00
|
|
|
public HistoryProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-06-19 16:44:45 -07:00
|
|
|
public virtual List<History> AllItems()
|
2011-01-28 22:10:22 -08:00
|
|
|
{
|
2011-06-19 16:44:45 -07:00
|
|
|
return _database.Fetch<History>("");
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual List<History> AllItemsWithRelationships()
|
|
|
|
{
|
|
|
|
return _database.Fetch<History, Episode>(@"
|
|
|
|
SELECT History.*, Series.Title as SeriesTitle, Episodes.* FROM History
|
|
|
|
INNER JOIN Series ON History.SeriesId = Series.SeriesId
|
|
|
|
INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId
|
|
|
|
");
|
2011-01-28 22:10:22 -08:00
|
|
|
}
|
2011-04-09 17:14:51 -07:00
|
|
|
|
2011-04-08 16:58:46 -07:00
|
|
|
public virtual void Purge()
|
2011-01-28 22:10:22 -08:00
|
|
|
{
|
2011-06-16 23:59:13 -07:00
|
|
|
_database.Delete<History>("");
|
2011-01-28 22:10:22 -08:00
|
|
|
Logger.Info("History has been Purged");
|
|
|
|
}
|
2011-04-09 17:14:51 -07:00
|
|
|
|
2011-04-08 16:58:46 -07:00
|
|
|
public virtual void Trim()
|
2011-01-28 22:10:22 -08:00
|
|
|
{
|
2011-06-16 23:59:13 -07:00
|
|
|
_database.Delete<History>("WHERE Date < @0", DateTime.Now.AddDays(-30).Date);
|
2011-01-28 22:10:22 -08:00
|
|
|
Logger.Info("History has been trimmed, items older than 30 days have been removed");
|
|
|
|
}
|
2011-04-09 17:14:51 -07:00
|
|
|
|
2011-04-22 13:14:02 -07:00
|
|
|
public virtual void Add(History item)
|
2011-01-28 22:10:22 -08:00
|
|
|
{
|
2011-06-16 23:59:13 -07:00
|
|
|
_database.Insert(item);
|
2011-04-22 13:14:02 -07:00
|
|
|
Logger.Debug("Item added to history: {0}", item.NzbTitle);
|
2011-01-28 22:10:22 -08:00
|
|
|
}
|
2011-04-09 17:14:51 -07:00
|
|
|
|
2011-05-28 12:23:35 -07:00
|
|
|
public virtual Quality GetBestQualityInHistory(long episodeId)
|
2011-01-28 22:10:22 -08:00
|
|
|
{
|
2011-06-05 12:55:46 -07:00
|
|
|
var history = AllItems().Where(c => c.EpisodeId == episodeId).ToList().Select(d => new Quality(d.Quality, d.IsProper)).OrderBy(c => c);
|
2011-06-05 12:42:53 -07:00
|
|
|
|
2011-05-28 12:23:35 -07:00
|
|
|
return history.FirstOrDefault();
|
2011-01-28 22:10:22 -08:00
|
|
|
}
|
2011-08-26 17:59:51 -07:00
|
|
|
|
|
|
|
public virtual void Delete(int historyId)
|
|
|
|
{
|
|
|
|
_database.Delete<History>(historyId);
|
|
|
|
}
|
2011-01-28 22:10:22 -08:00
|
|
|
}
|
2011-04-09 19:44:01 -07:00
|
|
|
}
|