mirror of
https://github.com/Radarr/Radarr.git
synced 2024-11-05 02:22:31 +01:00
101 lines
3.3 KiB
C#
101 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using FluentValidation.Internal;
|
|
using NzbDrone.Api.ClientSchema;
|
|
using NzbDrone.Api.Mapping;
|
|
using NzbDrone.Api.REST;
|
|
using NzbDrone.Api.Validation;
|
|
using NzbDrone.Core.Indexers;
|
|
using Omu.ValueInjecter;
|
|
using FluentValidation;
|
|
|
|
namespace NzbDrone.Api.Indexers
|
|
{
|
|
public class IndexerModule : NzbDroneRestModule<IndexerResource>
|
|
{
|
|
private readonly IIndexerService _indexerService;
|
|
|
|
public IndexerModule(IIndexerService indexerService)
|
|
{
|
|
_indexerService = indexerService;
|
|
GetResourceAll = GetAll;
|
|
CreateResource = CreateIndexer;
|
|
UpdateResource = UpdateIndexer;
|
|
DeleteResource = DeleteIndexer;
|
|
|
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
|
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
|
|
|
SharedValidator.RuleForField<string>(c=>c.Fields,"Url").NotEmpty();
|
|
|
|
PostValidator.RuleFor(c => c.Fields).NotEmpty();
|
|
}
|
|
|
|
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 CreateIndexer(IndexerResource indexerResource)
|
|
{
|
|
var indexer = GetIndexer(indexerResource);
|
|
indexer = _indexerService.Create(indexer);
|
|
|
|
var response = indexer.InjectTo<IndexerResource>();
|
|
response.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
|
|
|
|
return response;
|
|
}
|
|
|
|
private IndexerResource UpdateIndexer(IndexerResource indexerResource)
|
|
{
|
|
var indexer = _indexerService.Get(indexerResource.Id);
|
|
indexer.InjectFrom(indexerResource);
|
|
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
|
|
indexer = _indexerService.Update(indexer);
|
|
|
|
var response = indexer.InjectTo<IndexerResource>();
|
|
response.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
|
|
|
|
return response;
|
|
}
|
|
|
|
private Indexer GetIndexer(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.InjectFrom(indexerResource);
|
|
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
|
|
|
|
return indexer;
|
|
}
|
|
|
|
private void DeleteIndexer(int id)
|
|
{
|
|
_indexerService.Delete(id);
|
|
}
|
|
}
|
|
} |