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

78 lines
2.3 KiB
C#
Raw Normal View History

2013-02-21 07:07:34 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
2013-02-21 16:38:31 +00:00
using NzbDrone.Core.Lifecycle;
2013-02-21 07:07:34 +00:00
using PetaPoco;
namespace NzbDrone.Core.Indexers
{
public interface IIndexerService
{
List<Indexer> All();
List<IndexerBase> GetEnabledIndexers();
void SaveSettings(Indexer indexer);
Indexer GetSettings(Type type);
}
2013-02-21 16:38:31 +00:00
public class IndexerService : IIndexerService, IInitializable
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
private IList<IndexerBase> _indexers;
2013-02-21 16:38:31 +00:00
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IndexerBase> 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
}
public void Init()
{
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
var currentIndexers = All();
foreach (var feedProvider in _indexers)
{
IndexerBase indexerLocal = feedProvider;
if (!currentIndexers.Exists(c => c.Type == indexerLocal.GetType().ToString()))
{
var settings = new Indexer
{
Enable = indexerLocal.EnabledByDefault,
Type = indexerLocal.GetType().ToString(),
Name = indexerLocal.Name
};
_indexerRepository.Insert(settings);
}
}
2013-02-21 07:07:34 +00:00
}
public List<Indexer> All()
{
return _indexerRepository.All().ToList();
}
public List<IndexerBase> GetEnabledIndexers()
{
var all = All();
return _indexers.Where(i => all.Exists(c => c.Type == i.GetType().ToString() && c.Enable)).ToList();
}
public void SaveSettings(Indexer indexer)
{
2013-02-21 16:38:31 +00:00
//Todo: This will be used in the API
_logger.Debug("Upserting Indexer definitions for {0}", indexer.Name);
_indexerRepository.Upsert(indexer);
2013-02-21 07:07:34 +00:00
}
public Indexer GetSettings(Type type)
{
return _indexerRepository.Find(type);
}
}
}