1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-27 02:09:59 +00:00
Radarr/NzbDrone.Api/Indexers/IndexerModule.cs

113 lines
3.4 KiB
C#
Raw Normal View History

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