sonarr-repo-only/NzbDrone.Core/Indexers/IndexerService.cs

78 lines
2.3 KiB
C#
Raw Normal View History

2013-04-07 07:30:37 +00:00
using System.Collections.Generic;
2013-02-21 07:07:34 +00:00
using System.Linq;
using NLog;
using NzbDrone.Common.Eventing;
2013-02-21 16:38:31 +00:00
using NzbDrone.Core.Lifecycle;
2013-03-02 18:25:39 +00:00
2013-02-21 07:07:34 +00:00
namespace NzbDrone.Core.Indexers
{
public interface IIndexerService
{
2013-04-07 07:30:37 +00:00
List<IndexerDefinition> All();
List<IIndexerBase> GetAvailableIndexers();
void Save(IndexerDefinition indexer);
IndexerDefinition Get(string name);
2013-02-21 07:07:34 +00:00
}
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
2013-02-21 07:07:34 +00:00
{
private readonly IIndexerRepository _indexerRepository;
2013-02-21 16:38:31 +00:00
private readonly Logger _logger;
2013-02-21 07:07:34 +00:00
2013-04-07 07:30:37 +00:00
private readonly IList<IIndexerBase> _indexers;
2013-02-21 07:07:34 +00:00
2013-04-07 07:30:37 +00:00
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IIndexerBase> indexers, Logger logger)
2013-02-21 07:07:34 +00:00
{
_indexerRepository = indexerRepository;
2013-02-21 16:38:31 +00:00
_logger = logger;
2013-02-21 07:07:34 +00:00
_indexers = indexers.ToList();
2013-02-21 16:38:31 +00:00
}
2013-02-21 07:07:34 +00:00
2013-04-07 07:30:37 +00:00
public List<IndexerDefinition> All()
2013-02-21 07:07:34 +00:00
{
return _indexerRepository.All().ToList();
}
2013-04-07 07:30:37 +00:00
public List<IIndexerBase> GetAvailableIndexers()
2013-02-21 07:07:34 +00:00
{
2013-04-07 07:30:37 +00:00
var enabled = All().Where(c => c.Enable).Select(c => c.Name);
2013-04-10 23:44:48 +00:00
var configureIndexers = _indexers.Where(c => c.IsConfigured);
2013-04-07 07:30:37 +00:00
return configureIndexers.Where(c => enabled.Contains(c.Name)).ToList();
2013-02-21 07:07:34 +00:00
}
2013-04-07 07:30:37 +00:00
public void Save(IndexerDefinition indexer)
2013-02-21 07:07:34 +00:00
{
2013-04-07 07:30:37 +00:00
_indexerRepository.Update(indexer);
2013-02-21 07:07:34 +00:00
}
2013-04-07 07:30:37 +00:00
public IndexerDefinition Get(string name)
2013-02-21 07:07:34 +00:00
{
2013-04-07 07:30:37 +00:00
return _indexerRepository.Get(name);
2013-02-21 07:07:34 +00:00
}
public void Handle(ApplicationStartedEvent message)
{
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
var currentIndexers = All();
foreach (var feedProvider in _indexers)
{
IIndexerBase indexerLocal = feedProvider;
if (!currentIndexers.Exists(c => c.Name == indexerLocal.Name))
{
var settings = new IndexerDefinition
{
Enable = indexerLocal.EnabledByDefault,
Name = indexerLocal.Name.ToLower()
};
_indexerRepository.Insert(settings);
}
}
}
2013-02-21 07:07:34 +00:00
}
}