2011-01-29 06:10:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-06-14 01:23:04 +00:00
|
|
|
|
using Ninject;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
using NLog;
|
2011-05-28 19:23:35 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
|
|
|
|
using SubSonic.Repository;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
2011-04-08 23:58:46 +00:00
|
|
|
|
public class HistoryProvider
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-26 00:28:33 +00:00
|
|
|
|
private readonly IRepository _repository;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
|
2011-06-14 01:23:04 +00:00
|
|
|
|
[Inject]
|
2011-04-26 00:28:33 +00:00
|
|
|
|
public HistoryProvider(IRepository repository)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository = repository;
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-10 00:14:51 +00:00
|
|
|
|
public HistoryProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-22 02:23:31 +00:00
|
|
|
|
public virtual IQueryable<History> AllItems()
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
return _repository.All<History>();
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-08 23:58:46 +00:00
|
|
|
|
public virtual void Purge()
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.DeleteMany(AllItems());
|
2011-01-29 06:10:22 +00:00
|
|
|
|
Logger.Info("History has been Purged");
|
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-08 23:58:46 +00:00
|
|
|
|
public virtual void Trim()
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-04-22 02:23:31 +00:00
|
|
|
|
var old = AllItems().Where(h => h.Date < DateTime.Now.AddDays(-30));
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.DeleteMany(old);
|
2011-01-29 06:10:22 +00:00
|
|
|
|
Logger.Info("History has been trimmed, items older than 30 days have been removed");
|
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-04-22 20:14:02 +00:00
|
|
|
|
public virtual void Add(History item)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-04-26 00:28:33 +00:00
|
|
|
|
_repository.Add(item);
|
2011-04-22 20:14:02 +00:00
|
|
|
|
Logger.Debug("Item added to history: {0}", item.NzbTitle);
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 00:14:51 +00:00
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
public virtual Quality GetBestQualityInHistory(long episodeId)
|
2011-01-29 06:10:22 +00:00
|
|
|
|
{
|
2011-06-05 19:55:46 +00:00
|
|
|
|
var history = AllItems().Where(c => c.EpisodeId == episodeId).ToList().Select(d => new Quality(d.Quality, d.IsProper)).OrderBy(c => c);
|
2011-06-05 19:42:53 +00:00
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
return history.FirstOrDefault();
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-05-13 00:55:26 +00:00
|
|
|
|
|
|
|
|
|
public virtual void Delete(int historyId)
|
|
|
|
|
{
|
|
|
|
|
_repository.Delete<History>(historyId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void DeleteForEpisode(int episodeId)
|
|
|
|
|
{
|
|
|
|
|
_repository.DeleteMany<History>(h => h.EpisodeId == episodeId);
|
|
|
|
|
}
|
2011-01-29 06:10:22 +00:00
|
|
|
|
}
|
2011-04-10 02:44:01 +00:00
|
|
|
|
}
|