1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 07:52:37 +01:00
Sonarr/NzbDrone.Core/Indexers/IndexerService.cs

78 lines
2.3 KiB
C#
Raw Normal View History

2013-02-21 08:07:34 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
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
{
List<Indexer> All();
List<IndexerBase> GetEnabledIndexers();
void SaveSettings(Indexer indexer);
Indexer GetSettings(Type type);
}
2013-02-21 17:38:31 +01:00
public class IndexerService : IIndexerService, IInitializable
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
private IList<IndexerBase> _indexers;
2013-02-21 17:38:31 +01:00
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IndexerBase> 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
}
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 08:07:34 +01: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 17:38:31 +01:00
//Todo: This will be used in the API
_logger.Debug("Upserting Indexer definitions for {0}", indexer.Name);
2013-03-24 05:16:00 +01:00
_indexerRepository.Upsert(indexer);
2013-02-21 08:07:34 +01:00
}
public Indexer GetSettings(Type type)
{
return _indexerRepository.Find(type);
}
}
}