Indexers are saved when settings are saved

This commit is contained in:
Mark McDowall 2013-05-30 18:03:37 -07:00
parent 4a39faeb39
commit d2a59ea404
5 changed files with 67 additions and 25 deletions

View File

@ -17,7 +17,8 @@ namespace NzbDrone.Api.Indexers
{
_indexerService = indexerService;
GetResourceAll = GetAll;
CreateResource = Create;
CreateResource = CreateIndexer;
UpdateResource = UpdateIndexer;
}
private List<IndexerResource> GetAll()
@ -38,7 +39,31 @@ namespace NzbDrone.Api.Indexers
return result;
}
private IndexerResource Create(IndexerResource indexerResource)
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 =>
@ -50,16 +75,10 @@ namespace NzbDrone.Api.Indexers
throw new BadRequestException("Invalid Indexer Implementation");
}
indexer.Name = indexerResource.Name;
indexer.Enable = indexerResource.Enable;
indexer.InjectFrom(indexerResource);
indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields);
indexer = _indexerService.Create(indexer);
var response = indexer.InjectTo<IndexerResource>();
response.Fields = SchemaBuilder.GenerateSchema(indexer.Settings);
return response;
return indexer;
}
}
}

View File

@ -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<Indexer> All();
List<IIndexer> GetAvailableIndexers();
Indexer Get(int id);
Indexer Get(string name);
List<Indexer> Schema();
Indexer Create(Indexer indexer);
Indexer Update(Indexer indexer);
}
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
@ -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();

View File

@ -1,6 +1,6 @@
<div class="row">
<div class="span12">
<button class="btn btn-success x-add">Add</button>
<button class="btn btn-success x-add">Add Newznab</button>
</div>
</div>

View File

@ -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'});

View File

@ -3,7 +3,7 @@
{{#if id}}
<h3>Edit</h3>
{{else}}
<h3>Add</h3>
<h3>Add Newznab</h3>
{{/if}}
</div>
<div class="modal-body">