1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Able to create new Newznab indexers

This commit is contained in:
Mark McDowall 2013-05-28 17:44:29 -07:00
parent 5660b5086c
commit e4410d8cb7
8 changed files with 117 additions and 13 deletions

View File

@ -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<Field> fields)
{
var properties = model.GetType().GetSimpleProperties();
foreach (var propertyInfo in properties)
{
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
if (fieldAttribute != null)
{
var field = fields.Find(f => f.Name == propertyInfo.Name);
propertyInfo.SetValue(model, field.Value, null);
}
}
return model;
}
}
}

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Api.ClientSchema; using NzbDrone.Api.ClientSchema;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using Omu.ValueInjecter; using Omu.ValueInjecter;
@ -13,6 +15,7 @@ public IndexerModule(IIndexerService indexerService)
{ {
_indexerService = indexerService; _indexerService = indexerService;
GetResourceAll = GetAll; GetResourceAll = GetAll;
CreateResource = Create;
} }
private List<IndexerResource> GetAll() private List<IndexerResource> GetAll()
@ -32,5 +35,28 @@ private List<IndexerResource> GetAll()
return result; 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;
}
} }
} }

View File

@ -10,5 +10,6 @@ public class IndexerResource : RestResource
public Boolean Enable { get; set; } public Boolean Enable { get; set; }
public String Name { get; set; } public String Name { get; set; }
public List<Field> Fields { get; set; } public List<Field> Fields { get; set; }
public String Implementation { get; set; }
} }
} }

View File

@ -95,6 +95,7 @@
<Compile Include="Authentication\NzbDroneUser.cs" /> <Compile Include="Authentication\NzbDroneUser.cs" />
<Compile Include="AutomapperBootstraper.cs" /> <Compile Include="AutomapperBootstraper.cs" />
<Compile Include="Calendar\CalendarModule.cs" /> <Compile Include="Calendar\CalendarModule.cs" />
<Compile Include="ClientSchema\SchemaDeserializer.cs" />
<Compile Include="ClientSchema\FieldDefinitionAttribute.cs" /> <Compile Include="ClientSchema\FieldDefinitionAttribute.cs" />
<Compile Include="ClientSchema\Field.cs" /> <Compile Include="ClientSchema\Field.cs" />
<Compile Include="ClientSchema\SchemaBuilder.cs" /> <Compile Include="ClientSchema\SchemaBuilder.cs" />

View File

@ -3,6 +3,7 @@
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.Indexers.Newznab;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
@ -15,6 +16,7 @@ public class Indexer
public bool Enable { get; set; } public bool Enable { get; set; }
public IIndexerSetting Settings { get; set; } public IIndexerSetting Settings { get; set; }
public IIndexer Instance { get; set; } public IIndexer Instance { get; set; }
public string Implementation { get; set; }
} }
public interface IIndexerService public interface IIndexerService
@ -23,6 +25,7 @@ public interface IIndexerService
List<IIndexer> GetAvailableIndexers(); List<IIndexer> GetAvailableIndexers();
Indexer Get(string name); Indexer Get(string name);
List<Indexer> Schema(); List<Indexer> Schema();
Indexer Create(Indexer indexer);
} }
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent> public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
@ -63,12 +66,29 @@ public List<Indexer> Schema()
newznab.Id = 1; newznab.Id = 1;
newznab.Name = "Newznab"; newznab.Name = "Newznab";
newznab.Settings = new NewznabSettings(); newznab.Settings = new NewznabSettings();
newznab.Implementation = "Newznab";
indexers.Add(newznab); indexers.Add(newznab);
return indexers; 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) private Indexer ToIndexer(IndexerDefinition definition)
{ {
var indexer = new Indexer(); var indexer = new Indexer();
@ -76,6 +96,7 @@ private Indexer ToIndexer(IndexerDefinition definition)
indexer.Enable = definition.Enable; indexer.Enable = definition.Enable;
indexer.Instance = GetInstance(definition); indexer.Instance = GetInstance(definition);
indexer.Name = definition.Name; indexer.Name = definition.Name;
indexer.Implementation = definition.Implementation;
if (indexer.Instance.GetType().GetMethod("ImportSettingsFromJson") != null) if (indexer.Instance.GetType().GetMethod("ImportSettingsFromJson") != null)
{ {

View File

@ -5,23 +5,22 @@ define(['app', 'Settings/SettingsModel', 'Shared/Messenger'], function () {
template: 'Settings/General/GeneralTemplate', template: 'Settings/General/GeneralTemplate',
initialize: function () { initialize: function () {
NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this); NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this);
}, },
saveSettings: function () { saveSettings: function () {
if (!this.model.isSaved) { 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) { syncNotification: function (success, error) {
return { return {
success: function () { success: function () {
NzbDrone.Shared.Messenger.show({message: 'General Settings Saved'}); NzbDrone.Shared.Messenger.show({message: success});
}, },
error : function () { error : function () {
NzbDrone.Shared.Messenger.show({message: "Couldn't Save General Settings", type: 'error'}); NzbDrone.Shared.Messenger.show({message: error, type: 'error'});
} }
}; };
} }

View File

@ -12,7 +12,12 @@ define(['app',
'click .x-add': 'openSchemaModal' 'click .x-add': 'openSchemaModal'
}, },
initialize: function () {
NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this);
},
openSchemaModal: function () { openSchemaModal: function () {
var self = this;
//TODO: Is there a better way to deal with changing URLs? //TODO: Is there a better way to deal with changing URLs?
var schemaCollection = new NzbDrone.Settings.Indexers.Collection(); var schemaCollection = new NzbDrone.Settings.Indexers.Collection();
schemaCollection.url = '/api/indexer/schema'; schemaCollection.url = '/api/indexer/schema';
@ -23,10 +28,29 @@ define(['app',
model.set('id', undefined); model.set('id', undefined);
model.set('name', ''); 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); 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'});
}
};
} }
}); });
}); });

View File

@ -13,18 +13,23 @@ define([
'click .x-save': 'save' 'click .x-save': 'save'
}, },
save: function () { initialize: function (options) {
this.model.save(); this.indexerCollection = options.indexerCollection;
// window.alert('saving');
// this.model.save(undefined, this.syncNotification("Notification Settings Saved", "Couldn't Save Notification Settings"));
}, },
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 { return {
success: function () { success: function () {
window.alert(success); NzbDrone.Shared.Messenger.show({
message: success
});
context.indexerCollection.add(context.model);
context.$el.parent().modal('hide');
}, },
error: function () { error: function () {