From d2a59ea4046b0589639e5fa3ec3ede1818b3d66d Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 30 May 2013 18:03:37 -0700 Subject: [PATCH] Indexers are saved when settings are saved --- NzbDrone.Api/Indexers/IndexerModule.cs | 51 ++++++++++++++------ NzbDrone.Core/Indexers/IndexerService.cs | 19 ++++++++ UI/Settings/Indexers/CollectionTemplate.html | 2 +- UI/Settings/Indexers/CollectionView.js | 18 ++++--- UI/Settings/Indexers/EditTemplate.html | 2 +- 5 files changed, 67 insertions(+), 25 deletions(-) diff --git a/NzbDrone.Api/Indexers/IndexerModule.cs b/NzbDrone.Api/Indexers/IndexerModule.cs index e08f74e74..ba626948f 100644 --- a/NzbDrone.Api/Indexers/IndexerModule.cs +++ b/NzbDrone.Api/Indexers/IndexerModule.cs @@ -17,7 +17,8 @@ namespace NzbDrone.Api.Indexers { _indexerService = indexerService; GetResourceAll = GetAll; - CreateResource = Create; + CreateResource = CreateIndexer; + UpdateResource = UpdateIndexer; } private List GetAll() @@ -38,22 +39,9 @@ namespace NzbDrone.Api.Indexers return result; } - private IndexerResource Create(IndexerResource indexerResource) + private IndexerResource CreateIndexer(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); - + var indexer = GetIndexer(indexerResource); indexer = _indexerService.Create(indexer); var response = indexer.InjectTo(); @@ -61,5 +49,36 @@ namespace NzbDrone.Api.Indexers 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(); + 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; + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/IndexerService.cs b/NzbDrone.Core/Indexers/IndexerService.cs index 400704ffd..55aa89672 100644 --- a/NzbDrone.Core/Indexers/IndexerService.cs +++ b/NzbDrone.Core/Indexers/IndexerService.cs @@ -6,6 +6,7 @@ using NzbDrone.Common.Messaging; using NzbDrone.Common.Serializer; using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.Lifecycle; +using Omu.ValueInjecter; namespace NzbDrone.Core.Indexers { @@ -23,9 +24,11 @@ namespace NzbDrone.Core.Indexers { List All(); List GetAvailableIndexers(); + Indexer Get(int id); Indexer Get(string name); List Schema(); Indexer Create(Indexer indexer); + Indexer Update(Indexer indexer); } public class IndexerService : IIndexerService, IHandle @@ -52,6 +55,11 @@ namespace NzbDrone.Core.Indexers return All().Where(c => c.Enable && c.Settings.IsValid).Select(c => c.Instance).ToList(); } + public Indexer Get(int id) + { + return ToIndexer(_indexerRepository.Get(id)); + } + public Indexer Get(string name) { return ToIndexer(_indexerRepository.Get(name)); @@ -73,6 +81,7 @@ namespace NzbDrone.Core.Indexers return indexers; } + public Indexer Create(Indexer indexer) { var definition = new IndexerDefinition @@ -89,6 +98,16 @@ namespace NzbDrone.Core.Indexers return indexer; } + public Indexer Update(Indexer indexer) + { + var definition = _indexerRepository.Get(indexer.Id); + definition.InjectFrom(indexer); + definition.Settings = indexer.Settings.ToJson(); + _indexerRepository.Update(definition); + + return indexer; + } + private Indexer ToIndexer(IndexerDefinition definition) { var indexer = new Indexer(); diff --git a/UI/Settings/Indexers/CollectionTemplate.html b/UI/Settings/Indexers/CollectionTemplate.html index c65a8121f..b539ad0f9 100644 --- a/UI/Settings/Indexers/CollectionTemplate.html +++ b/UI/Settings/Indexers/CollectionTemplate.html @@ -1,6 +1,6 @@ 
- +
diff --git a/UI/Settings/Indexers/CollectionView.js b/UI/Settings/Indexers/CollectionView.js index 788609c46..c16aaf163 100644 --- a/UI/Settings/Indexers/CollectionView.js +++ b/UI/Settings/Indexers/CollectionView.js @@ -35,17 +35,21 @@ define(['app', }, 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")); -// } + var self = this; + + //For now loop through and save all the models + + _.each(this.collection.models, function (model, index, list) { + var name = model.get('name'); + var error = 'Failed to save indexer: ' + name; + + model.save(undefined, self.syncNotification(error)); + }); }, - syncNotification: function (success, error) { + syncNotification: function (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/EditTemplate.html b/UI/Settings/Indexers/EditTemplate.html index 33b77847e..2930bf730 100644 --- a/UI/Settings/Indexers/EditTemplate.html +++ b/UI/Settings/Indexers/EditTemplate.html @@ -3,7 +3,7 @@ {{#if id}}

Edit

{{else}} -

Add

+

Add Newznab

{{/if}}