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-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-05-29 02:44:29 +02:00
|
|
|
|
CreateResource = Create;
|
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
|
|
|
|
|
|
|
|
|
private IndexerResource Create(IndexerResource indexerResource)
|
|
|
|
|
{
|
|
|
|
|
var indexer = _indexerService.Schema()
|
|
|
|
|
.SingleOrDefault(i =>
|
|
|
|
|
i.Implementation.Equals(indexerResource.Implementation,
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (indexer == null)
|
|
|
|
|
{
|
2013-05-29 08:24:45 +02:00
|
|
|
|
throw new BadRequestException("Invalid Notification Implementation");
|
2013-05-29 02:44:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
indexer.Name = indexerResource.Name;
|
|
|
|
|
indexer.Enable = indexerResource.Enable;
|
2013-05-29 08:24:45 +02:00
|
|
|
|
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
|
2013-05-29 02:44:29 +02:00
|
|
|
|
|
|
|
|
|
indexer = _indexerService.Create(indexer);
|
|
|
|
|
|
2013-05-29 08:24:45 +02:00
|
|
|
|
var responseResource = new IndexerResource();
|
|
|
|
|
responseResource.InjectFrom(indexer);
|
|
|
|
|
responseResource.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
|
|
|
|
|
|
|
|
|
|
return responseResource;
|
2013-05-29 02:44:29 +02:00
|
|
|
|
}
|
2013-04-28 02:25:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|