You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readarr/NzbDrone.Core/Indexers/IndexerService.cs

78 lines
2.3 KiB

using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle;
12 years ago
namespace NzbDrone.Core.Indexers
{
public interface IIndexerService
{
List<IndexerDefinition> All();
List<IIndexerBase> GetAvailableIndexers();
void Save(IndexerDefinition indexer);
IndexerDefinition Get(string name);
}
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
{
private readonly IIndexerRepository _indexerRepository;
private readonly Logger _logger;
private readonly IList<IIndexerBase> _indexers;
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IIndexerBase> indexers, Logger logger)
{
_indexerRepository = indexerRepository;
_logger = logger;
_indexers = indexers.ToList();
}
public List<IndexerDefinition> All()
{
return _indexerRepository.All().ToList();
}
public List<IIndexerBase> GetAvailableIndexers()
{
var enabled = All().Where(c => c.Enable).Select(c => c.Name);
var configureIndexers = _indexers.Where(c => c.Settings.IsValid);
return configureIndexers.Where(c => enabled.Contains(c.Name)).ToList();
}
public void Save(IndexerDefinition indexer)
{
_indexerRepository.Update(indexer);
}
public IndexerDefinition Get(string name)
{
return _indexerRepository.Get(name);
}
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);
}
}
}
}
}