2011-01-29 08:10:22 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
|
|
|
using SubSonic.Repository;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
2011-04-09 02:58:46 +03:00
|
|
|
public class HistoryProvider
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2011-04-10 05:44:01 +03:00
|
|
|
private readonly IRepository _sonicRepo;
|
2011-01-29 08:10:22 +02:00
|
|
|
|
|
|
|
public HistoryProvider(IRepository sonicRepo)
|
|
|
|
{
|
|
|
|
_sonicRepo = sonicRepo;
|
|
|
|
}
|
|
|
|
|
2011-04-10 03:14:51 +03:00
|
|
|
public HistoryProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-22 05:23:31 +03:00
|
|
|
public virtual IQueryable<History> AllItems()
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
2011-04-22 05:23:31 +03:00
|
|
|
return _sonicRepo.All<History>();
|
2011-01-29 08:10:22 +02:00
|
|
|
}
|
2011-04-10 03:14:51 +03:00
|
|
|
|
2011-04-09 02:58:46 +03:00
|
|
|
public virtual void Purge()
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
2011-04-22 05:23:31 +03:00
|
|
|
_sonicRepo.DeleteMany(AllItems());
|
2011-01-29 08:10:22 +02:00
|
|
|
Logger.Info("History has been Purged");
|
|
|
|
}
|
2011-04-10 03:14:51 +03:00
|
|
|
|
2011-04-09 02:58:46 +03:00
|
|
|
public virtual void Trim()
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
2011-04-22 05:23:31 +03:00
|
|
|
var old = AllItems().Where(h => h.Date < DateTime.Now.AddDays(-30));
|
2011-01-29 08:10:22 +02:00
|
|
|
_sonicRepo.DeleteMany(old);
|
|
|
|
Logger.Info("History has been trimmed, items older than 30 days have been removed");
|
|
|
|
}
|
2011-04-10 03:14:51 +03:00
|
|
|
|
2011-04-22 23:14:02 +03:00
|
|
|
public virtual void Add(History item)
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
|
|
|
_sonicRepo.Add(item);
|
2011-04-22 23:14:02 +03:00
|
|
|
Logger.Debug("Item added to history: {0}", item.NzbTitle);
|
2011-01-29 08:10:22 +02:00
|
|
|
}
|
2011-04-10 03:14:51 +03:00
|
|
|
|
2011-04-09 02:58:46 +03:00
|
|
|
public virtual bool Exists(int episodeId, QualityTypes quality, bool proper)
|
2011-01-29 08:10:22 +02:00
|
|
|
{
|
2011-04-22 05:23:31 +03:00
|
|
|
//Looks for the existence of this episode in History
|
2011-04-10 05:44:01 +03:00
|
|
|
if (_sonicRepo.Exists<History>(h => h.EpisodeId == episodeId && h.Quality == quality && h.IsProper == proper))
|
2011-01-29 08:10:22 +02:00
|
|
|
return true;
|
|
|
|
|
2011-04-22 23:14:02 +03:00
|
|
|
Logger.Debug("Episode not in History. ID:{0} Q:{1} Proper:{2}", episodeId, quality, proper);
|
2011-01-29 08:10:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-04-10 05:44:01 +03:00
|
|
|
}
|