From e4410d8cb75cb55086a69a64fc97d4659d9614e5 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 28 May 2013 17:44:29 -0700 Subject: [PATCH] Able to create new Newznab indexers --- .../ClientSchema/SchemaDeserializer.cs | 27 ++++++++++++++++++ NzbDrone.Api/Indexers/IndexerModule.cs | 28 ++++++++++++++++++- NzbDrone.Api/Indexers/IndexerResource.cs | 1 + NzbDrone.Api/NzbDrone.Api.csproj | 1 + NzbDrone.Core/Indexers/IndexerService.cs | 21 ++++++++++++++ UI/Settings/General/GeneralView.js | 7 ++--- UI/Settings/Indexers/CollectionView.js | 26 ++++++++++++++++- UI/Settings/Indexers/EditView.js | 19 ++++++++----- 8 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 NzbDrone.Api/ClientSchema/SchemaDeserializer.cs diff --git a/NzbDrone.Api/ClientSchema/SchemaDeserializer.cs b/NzbDrone.Api/ClientSchema/SchemaDeserializer.cs new file mode 100644 index 000000000..d638bb0bc --- /dev/null +++ b/NzbDrone.Api/ClientSchema/SchemaDeserializer.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using NzbDrone.Common.Reflection; +using NzbDrone.Core.Annotations; + +namespace NzbDrone.Api.ClientSchema +{ + public static class SchemaDeserializer + { + public static object DeserializeSchema(object model, List fields) + { + var properties = model.GetType().GetSimpleProperties(); + + foreach (var propertyInfo in properties) + { + var fieldAttribute = propertyInfo.GetAttribute(false); + + if (fieldAttribute != null) + { + var field = fields.Find(f => f.Name == propertyInfo.Name); + propertyInfo.SetValue(model, field.Value, null); + } + } + + return model; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/Indexers/IndexerModule.cs b/NzbDrone.Api/Indexers/IndexerModule.cs index 829dd1572..f24d845f0 100644 --- a/NzbDrone.Api/Indexers/IndexerModule.cs +++ b/NzbDrone.Api/Indexers/IndexerModule.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; using NzbDrone.Api.ClientSchema; using NzbDrone.Core.Indexers; using Omu.ValueInjecter; @@ -13,6 +15,7 @@ namespace NzbDrone.Api.Indexers { _indexerService = indexerService; GetResourceAll = GetAll; + CreateResource = Create; } private List GetAll() @@ -32,5 +35,28 @@ namespace NzbDrone.Api.Indexers return result; } + + private IndexerResource Create(IndexerResource indexerResource) + { + var indexer = _indexerService.Schema() + .SingleOrDefault(i => + i.Implementation.Equals(indexerResource.Implementation, + StringComparison.InvariantCultureIgnoreCase)); + + //TODO: How should be handle this error? + if (indexer == null) + { + throw new InvalidOperationException(); + } + + indexer.Name = indexerResource.Name; + indexer.Enable = indexerResource.Enable; + indexer.Settings = (IIndexerSetting)SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields); + + indexer = _indexerService.Create(indexer); + indexerResource.Id = indexer.Id; + + return indexerResource; + } } } \ No newline at end of file diff --git a/NzbDrone.Api/Indexers/IndexerResource.cs b/NzbDrone.Api/Indexers/IndexerResource.cs index 4c793942f..34a31551a 100644 --- a/NzbDrone.Api/Indexers/IndexerResource.cs +++ b/NzbDrone.Api/Indexers/IndexerResource.cs @@ -10,5 +10,6 @@ namespace NzbDrone.Api.Indexers public Boolean Enable { get; set; } public String Name { get; set; } public List Fields { get; set; } + public String Implementation { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index 516c737d6..cf590269a 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -95,6 +95,7 @@ + diff --git a/NzbDrone.Core/Indexers/IndexerService.cs b/NzbDrone.Core/Indexers/IndexerService.cs index 8b1097ac0..86b1c5808 100644 --- a/NzbDrone.Core/Indexers/IndexerService.cs +++ b/NzbDrone.Core/Indexers/IndexerService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Messaging; +using NzbDrone.Common.Serializer; using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.Lifecycle; @@ -15,6 +16,7 @@ namespace NzbDrone.Core.Indexers public bool Enable { get; set; } public IIndexerSetting Settings { get; set; } public IIndexer Instance { get; set; } + public string Implementation { get; set; } } public interface IIndexerService @@ -23,6 +25,7 @@ namespace NzbDrone.Core.Indexers List GetAvailableIndexers(); Indexer Get(string name); List Schema(); + Indexer Create(Indexer indexer); } public class IndexerService : IIndexerService, IHandle @@ -63,12 +66,29 @@ namespace NzbDrone.Core.Indexers newznab.Id = 1; newznab.Name = "Newznab"; newznab.Settings = new NewznabSettings(); + newznab.Implementation = "Newznab"; indexers.Add(newznab); return indexers; } + public Indexer Create(Indexer indexer) + { + var definition = new IndexerDefinition + { + Name = indexer.Name, + Enable = indexer.Enable, + Implementation = indexer.Implementation, + Settings = Json.Serialize(indexer.Settings) + }; + + definition = _indexerRepository.Insert(definition); + indexer.Id = definition.Id; + + return indexer; + } + private Indexer ToIndexer(IndexerDefinition definition) { var indexer = new Indexer(); @@ -76,6 +96,7 @@ namespace NzbDrone.Core.Indexers indexer.Enable = definition.Enable; indexer.Instance = GetInstance(definition); indexer.Name = definition.Name; + indexer.Implementation = definition.Implementation; if (indexer.Instance.GetType().GetMethod("ImportSettingsFromJson") != null) { diff --git a/UI/Settings/General/GeneralView.js b/UI/Settings/General/GeneralView.js index 82a50b5c4..e8b72c384 100644 --- a/UI/Settings/General/GeneralView.js +++ b/UI/Settings/General/GeneralView.js @@ -5,23 +5,22 @@ define(['app', 'Settings/SettingsModel', 'Shared/Messenger'], function () { template: 'Settings/General/GeneralTemplate', initialize: function () { - NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this); }, saveSettings: function () { if (!this.model.isSaved) { - this.model.save(undefined, this.syncNotification("Naming Settings Saved", "Couldn't Save Naming Settings")); + this.model.save(undefined, this.syncNotification("General Settings Saved", "Couldn't Save General Settings")); } }, syncNotification: function (success, error) { return { success: function () { - NzbDrone.Shared.Messenger.show({message: 'General Settings Saved'}); + NzbDrone.Shared.Messenger.show({message: success}); }, error : function () { - NzbDrone.Shared.Messenger.show({message: "Couldn't Save General Settings", type: 'error'}); + NzbDrone.Shared.Messenger.show({message: error, type: 'error'}); } }; } diff --git a/UI/Settings/Indexers/CollectionView.js b/UI/Settings/Indexers/CollectionView.js index fca20bfd1..788609c46 100644 --- a/UI/Settings/Indexers/CollectionView.js +++ b/UI/Settings/Indexers/CollectionView.js @@ -12,7 +12,12 @@ define(['app', 'click .x-add': 'openSchemaModal' }, + initialize: function () { + NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this); + }, + openSchemaModal: function () { + var self = this; //TODO: Is there a better way to deal with changing URLs? var schemaCollection = new NzbDrone.Settings.Indexers.Collection(); schemaCollection.url = '/api/indexer/schema'; @@ -23,10 +28,29 @@ define(['app', model.set('id', undefined); model.set('name', ''); - var view = new NzbDrone.Settings.Indexers.EditView({ model: model}); + var view = new NzbDrone.Settings.Indexers.EditView({ model: model, indexerCollection: self.collection}); NzbDrone.modalRegion.show(view); } }); + }, + + saveSettings: function () { + //TODO: check if any models in the collection have changed and sync them only +// this.collection.sync(); +// if (!this.model.isSaved) { +// this.model.save(undefined, this.syncNotification("Naming Settings Saved", "Couldn't Save Naming Settings")); +// } + }, + + syncNotification: function (success, error) { + return { + success: function () { + NzbDrone.Shared.Messenger.show({message: 'General Settings Saved'}); + }, + error : function () { + NzbDrone.Shared.Messenger.show({message: "Couldn't Save General Settings", type: 'error'}); + } + }; } }); }); diff --git a/UI/Settings/Indexers/EditView.js b/UI/Settings/Indexers/EditView.js index 10d3e3ebd..54bdbe642 100644 --- a/UI/Settings/Indexers/EditView.js +++ b/UI/Settings/Indexers/EditView.js @@ -13,18 +13,23 @@ define([ 'click .x-save': 'save' }, - save: function () { - this.model.save(); - -// window.alert('saving'); -// this.model.save(undefined, this.syncNotification("Notification Settings Saved", "Couldn't Save Notification Settings")); + initialize: function (options) { + this.indexerCollection = options.indexerCollection; }, + save: function () { + this.model.save(undefined, this.syncNotification("Indexer Saved", "Couldn't Save Indexer", this)); + }, - syncNotification: function (success, error) { + syncNotification: function (success, error, context) { return { success: function () { - window.alert(success); + NzbDrone.Shared.Messenger.show({ + message: success + }); + + context.indexerCollection.add(context.model); + context.$el.parent().modal('hide'); }, error: function () {