2013-05-29 02:44:29 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-05-01 08:43:14 +02:00
|
|
|
|
using NzbDrone.Api.ClientSchema;
|
2013-05-29 08:24:45 +02:00
|
|
|
|
using NzbDrone.Api.REST;
|
2013-04-28 02:25:28 +02:00
|
|
|
|
using NzbDrone.Core.Indexers;
|
2013-05-01 08:43:14 +02:00
|
|
|
|
using Omu.ValueInjecter;
|
2013-08-21 03:07:48 +02:00
|
|
|
|
using FluentValidation;
|
2013-08-26 09:14:46 +02:00
|
|
|
|
using NzbDrone.Api.Extensions;
|
|
|
|
|
using NzbDrone.Api.Mapping;
|
2013-04-28 02:25:28 +02: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 09:14:46 +02:00
|
|
|
|
GetResourceById = GetIndexer;
|
2013-05-31 03:03:37 +02:00
|
|
|
|
CreateResource = CreateIndexer;
|
|
|
|
|
UpdateResource = UpdateIndexer;
|
2013-06-27 09:37:39 +02:00
|
|
|
|
DeleteResource = DeleteIndexer;
|
2013-08-21 03:07:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
|
|
|
|
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
|
|
|
|
|
|
|
|
|
PostValidator.RuleFor(c => c.Fields).NotEmpty();
|
2013-04-28 02:25:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 09:14:46 +02:00
|
|
|
|
private IndexerResource GetIndexer(int id)
|
|
|
|
|
{
|
|
|
|
|
return _indexerService.Get(id).InjectTo<IndexerResource>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 02:25:28 +02:00
|
|
|
|
private List<IndexerResource> GetAll()
|
|
|
|
|
{
|
2013-05-01 08:43:14 +02:00
|
|
|
|
var indexers = _indexerService.All();
|
|
|
|
|
|
|
|
|
|
var result = new List<IndexerResource>(indexers.Count);
|
|
|
|
|
|
2013-05-03 07:24:52 +02:00
|
|
|
|
foreach (var indexer in indexers)
|
2013-05-01 08:43:14 +02:00
|
|
|
|
{
|
2013-05-03 07:24:52 +02:00
|
|
|
|
var indexerResource = new IndexerResource();
|
|
|
|
|
indexerResource.InjectFrom(indexer);
|
|
|
|
|
indexerResource.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
|
|
|
|
|
|
|
|
|
|
result.Add(indexerResource);
|
2013-05-01 08:43:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2013-04-28 02:25:28 +02:00
|
|
|
|
}
|
2013-05-29 02:44:29 +02:00
|
|
|
|
|
2013-08-26 07:14:55 +02:00
|
|
|
|
private int CreateIndexer(IndexerResource indexerResource)
|
2013-05-31 03:03:37 +02:00
|
|
|
|
{
|
|
|
|
|
var indexer = GetIndexer(indexerResource);
|
|
|
|
|
indexer = _indexerService.Create(indexer);
|
2013-08-26 07:14:55 +02:00
|
|
|
|
return indexer.Id;
|
2013-05-31 03:03:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 07:14:55 +02:00
|
|
|
|
private void UpdateIndexer(IndexerResource indexerResource)
|
2013-05-31 03:03:37 +02:00
|
|
|
|
{
|
|
|
|
|
var indexer = _indexerService.Get(indexerResource.Id);
|
|
|
|
|
indexer.InjectFrom(indexerResource);
|
|
|
|
|
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
|
2013-08-22 10:08:43 +02:00
|
|
|
|
|
2013-08-26 06:01:03 +02:00
|
|
|
|
ValidateIndexer(indexer);
|
2013-08-22 10:08:43 +02:00
|
|
|
|
|
2013-08-26 07:14:55 +02:00
|
|
|
|
_indexerService.Update(indexer);
|
2013-05-31 03:03:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-22 10:08:43 +02:00
|
|
|
|
|
2013-08-26 06:01:03 +02:00
|
|
|
|
private static void ValidateIndexer(Indexer indexer)
|
2013-08-22 10:08:43 +02:00
|
|
|
|
{
|
2013-08-26 06:01:03 +02:00
|
|
|
|
if (indexer.Enable)
|
2013-08-22 10:08:43 +02:00
|
|
|
|
{
|
2013-08-26 06:01:03 +02:00
|
|
|
|
var validationResult = indexer.Settings.Validate();
|
|
|
|
|
|
|
|
|
|
if (!validationResult.IsValid)
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException(validationResult.Errors);
|
|
|
|
|
}
|
2013-08-22 10:08:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 03:03:37 +02:00
|
|
|
|
private Indexer GetIndexer(IndexerResource indexerResource)
|
2013-05-29 02:44:29 +02:00
|
|
|
|
{
|
|
|
|
|
var indexer = _indexerService.Schema()
|
|
|
|
|
.SingleOrDefault(i =>
|
2013-05-31 03:03:37 +02:00
|
|
|
|
i.Implementation.Equals(indexerResource.Implementation,
|
2013-05-29 02:44:29 +02:00
|
|
|
|
StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (indexer == null)
|
|
|
|
|
{
|
2013-05-30 05:26:47 +02:00
|
|
|
|
throw new BadRequestException("Invalid Indexer Implementation");
|
2013-05-29 02:44:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 03:03:37 +02:00
|
|
|
|
indexer.InjectFrom(indexerResource);
|
2013-05-29 08:24:45 +02:00
|
|
|
|
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
|
2013-05-29 02:44:29 +02:00
|
|
|
|
|
2013-08-26 06:01:03 +02:00
|
|
|
|
ValidateIndexer(indexer);
|
2013-08-22 10:08:43 +02:00
|
|
|
|
|
2013-05-31 03:03:37 +02:00
|
|
|
|
return indexer;
|
2013-05-29 02:44:29 +02:00
|
|
|
|
}
|
2013-06-27 09:37:39 +02:00
|
|
|
|
|
|
|
|
|
private void DeleteIndexer(int id)
|
|
|
|
|
{
|
|
|
|
|
_indexerService.Delete(id);
|
|
|
|
|
}
|
2013-04-28 02:25:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|