2013-09-22 08:20:26 +03:00
|
|
|
using System.Collections.Generic;
|
2013-05-01 09:43:14 +03:00
|
|
|
using NzbDrone.Api.ClientSchema;
|
2013-04-28 03:25:28 +03:00
|
|
|
using NzbDrone.Core.Indexers;
|
2013-09-22 08:20:26 +03:00
|
|
|
using NzbDrone.Core.ThingiProvider;
|
2013-05-01 09:43:14 +03:00
|
|
|
using Omu.ValueInjecter;
|
2013-08-21 04:07:48 +03:00
|
|
|
using FluentValidation;
|
2013-08-26 10:14:46 +03:00
|
|
|
using NzbDrone.Api.Mapping;
|
2013-04-28 03:25:28 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Api.Indexers
|
|
|
|
{
|
|
|
|
public class IndexerModule : NzbDroneRestModule<IndexerResource>
|
|
|
|
{
|
|
|
|
private readonly IIndexerService _indexerService;
|
|
|
|
|
|
|
|
public IndexerModule(IIndexerService indexerService)
|
|
|
|
{
|
|
|
|
_indexerService = indexerService;
|
|
|
|
GetResourceAll = GetAll;
|
2013-08-26 10:14:46 +03:00
|
|
|
GetResourceById = GetIndexer;
|
2013-05-31 04:03:37 +03:00
|
|
|
CreateResource = CreateIndexer;
|
|
|
|
UpdateResource = UpdateIndexer;
|
2013-06-27 10:37:39 +03:00
|
|
|
DeleteResource = DeleteIndexer;
|
2013-08-21 04:07:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
|
|
|
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
|
|
|
|
|
|
|
PostValidator.RuleFor(c => c.Fields).NotEmpty();
|
2013-04-28 03:25:28 +03:00
|
|
|
}
|
|
|
|
|
2013-08-26 10:14:46 +03:00
|
|
|
private IndexerResource GetIndexer(int id)
|
|
|
|
{
|
|
|
|
return _indexerService.Get(id).InjectTo<IndexerResource>();
|
|
|
|
}
|
|
|
|
|
2013-04-28 03:25:28 +03:00
|
|
|
private List<IndexerResource> GetAll()
|
|
|
|
{
|
2013-05-01 09:43:14 +03:00
|
|
|
var indexers = _indexerService.All();
|
|
|
|
|
|
|
|
var result = new List<IndexerResource>(indexers.Count);
|
|
|
|
|
2013-05-03 08:24:52 +03:00
|
|
|
foreach (var indexer in indexers)
|
2013-05-01 09:43:14 +03:00
|
|
|
{
|
2013-05-03 08:24:52 +03:00
|
|
|
var indexerResource = new IndexerResource();
|
|
|
|
indexerResource.InjectFrom(indexer);
|
|
|
|
indexerResource.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
|
|
|
|
|
|
|
|
result.Add(indexerResource);
|
2013-05-01 09:43:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2013-04-28 03:25:28 +03:00
|
|
|
}
|
2013-05-29 03:44:29 +03:00
|
|
|
|
2013-08-26 08:14:55 +03:00
|
|
|
private int CreateIndexer(IndexerResource indexerResource)
|
2013-05-31 04:03:37 +03:00
|
|
|
{
|
|
|
|
var indexer = GetIndexer(indexerResource);
|
|
|
|
indexer = _indexerService.Create(indexer);
|
2013-08-26 08:14:55 +03:00
|
|
|
return indexer.Id;
|
2013-05-31 04:03:37 +03:00
|
|
|
}
|
|
|
|
|
2013-08-26 08:14:55 +03:00
|
|
|
private void UpdateIndexer(IndexerResource indexerResource)
|
2013-05-31 04:03:37 +03:00
|
|
|
{
|
2013-09-22 08:20:26 +03:00
|
|
|
var indexer = GetIndexer(indexerResource);
|
2013-08-22 11:08:43 +03:00
|
|
|
|
2013-09-22 08:20:26 +03:00
|
|
|
ValidateIndexer(indexer.Settings);
|
2013-08-22 11:08:43 +03:00
|
|
|
|
2013-08-26 08:14:55 +03:00
|
|
|
_indexerService.Update(indexer);
|
2013-05-31 04:03:37 +03:00
|
|
|
}
|
|
|
|
|
2013-08-22 11:08:43 +03:00
|
|
|
|
2013-09-22 08:20:26 +03:00
|
|
|
private static void ValidateIndexer(IProviderConfig config)
|
2013-08-22 11:08:43 +03:00
|
|
|
{
|
2013-09-22 08:20:26 +03:00
|
|
|
var validationResult = config.Validate();
|
2013-08-26 07:01:03 +03:00
|
|
|
|
2013-09-22 08:20:26 +03:00
|
|
|
if (!validationResult.IsValid)
|
|
|
|
{
|
|
|
|
throw new ValidationException(validationResult.Errors);
|
2013-08-22 11:08:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-22 08:20:26 +03:00
|
|
|
private IndexerDefinition GetIndexer(IndexerResource indexerResource)
|
2013-05-29 03:44:29 +03:00
|
|
|
{
|
|
|
|
|
2013-09-22 08:20:26 +03:00
|
|
|
var definition = new IndexerDefinition();
|
|
|
|
|
|
|
|
definition.InjectFrom(indexerResource);
|
|
|
|
if (indexerResource.Enable)
|
2013-05-29 03:44:29 +03:00
|
|
|
{
|
2013-09-22 08:20:26 +03:00
|
|
|
ValidateIndexer(definition.Settings);
|
2013-05-29 03:44:29 +03:00
|
|
|
}
|
|
|
|
|
2013-09-22 08:20:26 +03:00
|
|
|
return definition;
|
2013-05-29 03:44:29 +03:00
|
|
|
}
|
2013-06-27 10:37:39 +03:00
|
|
|
|
|
|
|
private void DeleteIndexer(int id)
|
|
|
|
{
|
|
|
|
_indexerService.Delete(id);
|
|
|
|
}
|
2013-04-28 03:25:28 +03:00
|
|
|
}
|
|
|
|
}
|