1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2025-01-19 10:54:05 +02:00
Sonarr/NzbDrone.Api/Indexers/IndexerModule.cs

90 lines
2.9 KiB
C#
Raw Normal View History

2013-05-28 17:44:29 -07:00
using System;
using System.Collections.Generic;
using System.Linq;
2013-04-30 23:43:14 -07:00
using NzbDrone.Api.ClientSchema;
using NzbDrone.Api.Mapping;
2013-05-28 23:24:45 -07:00
using NzbDrone.Api.REST;
using NzbDrone.Core.Indexers;
2013-04-30 23:43:14 -07:00
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 = CreateIndexer;
UpdateResource = UpdateIndexer;
2013-06-27 00:37:39 -07:00
DeleteResource = DeleteIndexer;
}
private List<IndexerResource> GetAll()
{
2013-04-30 23:43:14 -07:00
var indexers = _indexerService.All();
var result = new List<IndexerResource>(indexers.Count);
2013-05-02 22:24:52 -07:00
foreach (var indexer in indexers)
2013-04-30 23:43:14 -07:00
{
2013-05-02 22:24:52 -07:00
var indexerResource = new IndexerResource();
indexerResource.InjectFrom(indexer);
indexerResource.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
result.Add(indexerResource);
2013-04-30 23:43:14 -07:00
}
return result;
}
2013-05-28 17:44:29 -07:00
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)
2013-05-28 17:44:29 -07:00
{
var indexer = _indexerService.Schema()
.SingleOrDefault(i =>
i.Implementation.Equals(indexerResource.Implementation,
2013-05-28 17:44:29 -07:00
StringComparison.InvariantCultureIgnoreCase));
if (indexer == null)
{
throw new BadRequestException("Invalid Indexer Implementation");
2013-05-28 17:44:29 -07:00
}
indexer.InjectFrom(indexerResource);
2013-05-28 23:24:45 -07:00
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
2013-05-28 17:44:29 -07:00
return indexer;
2013-05-28 17:44:29 -07:00
}
2013-06-27 00:37:39 -07:00
private void DeleteIndexer(int id)
{
_indexerService.Delete(id);
}
}
}