mirror of
https://github.com/lidarr/Lidarr
synced 2024-12-28 10:37:41 +00:00
65 lines
No EOL
2.1 KiB
C#
65 lines
No EOL
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NzbDrone.Api.ClientSchema;
|
|
using NzbDrone.Api.Mapping;
|
|
using NzbDrone.Api.REST;
|
|
using NzbDrone.Core.Indexers;
|
|
using Omu.ValueInjecter;
|
|
|
|
namespace NzbDrone.Api.Indexers
|
|
{
|
|
public class IndexerModule : NzbDroneRestModule<IndexerResource>
|
|
{
|
|
private readonly IIndexerService _indexerService;
|
|
|
|
public IndexerModule(IIndexerService indexerService)
|
|
{
|
|
_indexerService = indexerService;
|
|
GetResourceAll = GetAll;
|
|
CreateResource = Create;
|
|
}
|
|
|
|
private List<IndexerResource> GetAll()
|
|
{
|
|
var indexers = _indexerService.All();
|
|
|
|
var result = new List<IndexerResource>(indexers.Count);
|
|
|
|
foreach (var indexer in indexers)
|
|
{
|
|
var indexerResource = new IndexerResource();
|
|
indexerResource.InjectFrom(indexer);
|
|
indexerResource.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
|
|
|
|
result.Add(indexerResource);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private IndexerResource Create(IndexerResource indexerResource)
|
|
{
|
|
var indexer = _indexerService.Schema()
|
|
.SingleOrDefault(i =>
|
|
i.Implementation.Equals(indexerResource.Implementation,
|
|
StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
if (indexer == null)
|
|
{
|
|
throw new BadRequestException("Invalid Indexer Implementation");
|
|
}
|
|
|
|
indexer.Name = indexerResource.Name;
|
|
indexer.Enable = indexerResource.Enable;
|
|
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
|
|
|
|
indexer = _indexerService.Create(indexer);
|
|
|
|
var response = indexer.InjectTo<IndexerResource>();
|
|
response.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
|
|
|
|
return response;
|
|
}
|
|
}
|
|
} |