2011-01-29 07:10:22 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-06-14 03:23:04 +02:00
|
|
|
|
using Ninject;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
using NLog;
|
2011-05-28 21:23:35 +02:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
using NzbDrone.Core.Repository;
|
2012-02-07 06:08:07 +01:00
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
2011-06-17 08:59:13 +02:00
|
|
|
|
using PetaPoco;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
2011-04-09 01:58:46 +02:00
|
|
|
|
public class HistoryProvider
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-06-17 08:59:13 +02:00
|
|
|
|
private readonly IDatabase _database;
|
2012-02-07 06:08:07 +01:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-06-17 08:59:13 +02:00
|
|
|
|
|
2011-01-29 07:10:22 +01:00
|
|
|
|
|
2011-06-14 03:23:04 +02:00
|
|
|
|
[Inject]
|
2011-06-17 08:59:13 +02:00
|
|
|
|
public HistoryProvider(IDatabase database)
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-06-17 08:59:13 +02:00
|
|
|
|
_database = database;
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-10 02:14:51 +02:00
|
|
|
|
public HistoryProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-20 01:44:45 +02:00
|
|
|
|
public virtual List<History> AllItems()
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-06-20 01:44:45 +02: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-29 07:10:22 +01:00
|
|
|
|
}
|
2011-04-10 02:14:51 +02:00
|
|
|
|
|
2011-04-09 01:58:46 +02:00
|
|
|
|
public virtual void Purge()
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-06-17 08:59:13 +02:00
|
|
|
|
_database.Delete<History>("");
|
2012-02-07 06:08:07 +01:00
|
|
|
|
logger.Info("History has been Purged");
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
2011-04-10 02:14:51 +02:00
|
|
|
|
|
2011-04-09 01:58:46 +02:00
|
|
|
|
public virtual void Trim()
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-06-17 08:59:13 +02:00
|
|
|
|
_database.Delete<History>("WHERE Date < @0", DateTime.Now.AddDays(-30).Date);
|
2012-02-07 06:08:07 +01:00
|
|
|
|
logger.Info("History has been trimmed, items older than 30 days have been removed");
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
2011-04-10 02:14:51 +02:00
|
|
|
|
|
2011-04-22 22:14:02 +02:00
|
|
|
|
public virtual void Add(History item)
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2011-06-17 08:59:13 +02:00
|
|
|
|
_database.Insert(item);
|
2012-02-07 06:08:07 +01:00
|
|
|
|
logger.Debug("Item added to history: {0}", item.NzbTitle);
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
2011-04-10 02:14:51 +02:00
|
|
|
|
|
2012-02-07 06:08:07 +01:00
|
|
|
|
public virtual Quality GetBestQualityInHistory(int seriesId, int seasonNumber, int episodeNumber)
|
2011-01-29 07:10:22 +01:00
|
|
|
|
{
|
2012-02-07 06:08:07 +01:00
|
|
|
|
var quality = _database.SingleOrDefault<dynamic>(@"SELECT TOP 1 History.Quality , History.IsProper FROM History
|
|
|
|
|
INNER JOIN Episodes ON History.EpisodeId = Episodes.EpisodeId
|
|
|
|
|
WHERE Episodes.seriesId = @0 AND
|
|
|
|
|
Episodes.SeasonNumber = @1 AND
|
|
|
|
|
Episodes.EpisodeNumber = @2
|
|
|
|
|
ORDER BY History.Quality DESC, History.IsProper DESC"
|
|
|
|
|
, seriesId, seasonNumber, episodeNumber);
|
2011-06-05 21:42:53 +02:00
|
|
|
|
|
2012-02-07 06:08:07 +01:00
|
|
|
|
if (quality == null) return null;
|
|
|
|
|
|
|
|
|
|
return new Quality((QualityTypes)quality.Quality, quality.IsProper);
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
2011-08-27 02:59:51 +02:00
|
|
|
|
|
|
|
|
|
public virtual void Delete(int historyId)
|
|
|
|
|
{
|
|
|
|
|
_database.Delete<History>(historyId);
|
|
|
|
|
}
|
2011-01-29 07:10:22 +01:00
|
|
|
|
}
|
2011-04-10 04:44:01 +02:00
|
|
|
|
}
|