1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 20:42:37 +01:00
Radarr/NzbDrone.Core/Indexers/IndexerService.cs

79 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-02-21 08:07:34 +01:00
using System.Linq;
using NLog;
using NzbDrone.Common.Messaging;
2013-02-21 17:38:31 +01:00
using NzbDrone.Core.Lifecycle;
2013-03-02 19:25:39 +01:00
2013-02-21 08:07:34 +01:00
namespace NzbDrone.Core.Indexers
{
public interface IIndexerService
{
2013-04-07 09:30:37 +02:00
List<IndexerDefinition> All();
List<IIndexerBase> GetAvailableIndexers();
void Save(IndexerDefinition indexer);
IndexerDefinition Get(string name);
2013-02-21 08:07:34 +01:00
}
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
2013-02-21 08:07:34 +01:00
{
private readonly IIndexerRepository _indexerRepository;
2013-02-21 17:38:31 +01:00
private readonly Logger _logger;
2013-02-21 08:07:34 +01:00
2013-04-07 09:30:37 +02:00
private readonly IList<IIndexerBase> _indexers;
2013-02-21 08:07:34 +01:00
2013-04-07 09:30:37 +02:00
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IIndexerBase> indexers, Logger logger)
2013-02-21 08:07:34 +01:00
{
_indexerRepository = indexerRepository;
2013-02-21 17:38:31 +01:00
_logger = logger;
2013-02-21 08:07:34 +01:00
_indexers = indexers.ToList();
2013-02-21 17:38:31 +01:00
}
2013-02-21 08:07:34 +01:00
2013-04-07 09:30:37 +02:00
public List<IndexerDefinition> All()
2013-02-21 08:07:34 +01:00
{
return _indexerRepository.All().ToList();
}
2013-04-07 09:30:37 +02:00
public List<IIndexerBase> GetAvailableIndexers()
2013-02-21 08:07:34 +01:00
{
2013-04-07 09:30:37 +02:00
var enabled = All().Where(c => c.Enable).Select(c => c.Name);
2013-04-11 01:44:48 +02:00
var configureIndexers = _indexers.Where(c => c.IsConfigured);
2013-04-07 09:30:37 +02:00
return configureIndexers.Where(c => enabled.Contains(c.Name)).ToList();
2013-02-21 08:07:34 +01:00
}
2013-04-07 09:30:37 +02:00
public void Save(IndexerDefinition indexer)
2013-02-21 08:07:34 +01:00
{
2013-04-07 09:30:37 +02:00
_indexerRepository.Update(indexer);
2013-02-21 08:07:34 +01:00
}
2013-04-07 09:30:37 +02:00
public IndexerDefinition Get(string name)
2013-02-21 08:07:34 +01:00
{
2013-04-07 09:30:37 +02:00
return _indexerRepository.Get(name);
2013-02-21 08:07:34 +01: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.Equals(indexerLocal.Name, StringComparison.InvariantCultureIgnoreCase)))
{
var settings = new IndexerDefinition
{
Enable = indexerLocal.EnabledByDefault,
Name = indexerLocal.Name.ToLower()
};
_indexerRepository.Insert(settings);
}
}
}
2013-02-21 08:07:34 +01:00
}
}