1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-28 02:37:40 +00:00
Sonarr/NzbDrone.Core/Indexers/IndexerService.cs
2013-04-10 16:44:59 -07:00

78 lines
No EOL
2.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Eventing;
using NzbDrone.Core.Lifecycle;
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.IsConfigured);
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);
}
}
}
}
}