1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 07:52:37 +01:00

Notifications wired up

This commit is contained in:
Mark McDowall 2013-05-28 19:49:17 -07:00
parent e4410d8cb7
commit 1d007be8fd
11 changed files with 174 additions and 64 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using NzbDrone.Common.Reflection;
using NzbDrone.Core.Annotations;
@ -17,7 +18,17 @@ namespace NzbDrone.Api.ClientSchema
if (fieldAttribute != null)
{
var field = fields.Find(f => f.Name == propertyInfo.Name);
propertyInfo.SetValue(model, field.Value, null);
if (propertyInfo.PropertyType == typeof (Int32))
{
var intValue = Convert.ToInt32(field.Value);
propertyInfo.SetValue(model, intValue, null);
}
else
{
propertyInfo.SetValue(model, field.Value, null);
}
}
}

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Common.Reflection;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Notifications;
using Omu.ValueInjecter;
@ -16,7 +16,9 @@ namespace NzbDrone.Api.Notifications
_notificationService = notificationService;
GetResourceAll = GetAll;
CreateResource = Create;
UpdateResource = Update;
DeleteResource = DeleteNotification;
}
private List<NotificationResource> GetAll()
@ -37,32 +39,49 @@ namespace NzbDrone.Api.Notifications
return result;
}
private NotificationResource Update(NotificationResource notificationResource)
private NotificationResource Create(NotificationResource notificationResource)
{
//Todo: Convert Resource back to Settings
var notification = GetNotification(notificationResource);
var notification = _notificationService.Get(notificationResource.Id);
notification.OnGrab = notificationResource.OnGrab;
notification.OnDownload = notificationResource.OnDownload;
var properties = notification.Settings.GetType().GetSimpleProperties();
foreach (var propertyInfo in properties)
{
var fieldAttribute = propertyInfo.GetAttribute<FieldDefinitionAttribute>(false);
if (fieldAttribute != null)
{
//Find coresponding field
var field = notificationResource.Fields.Find(f => f.Name == propertyInfo.Name);
propertyInfo.SetValue(notification.Settings, field.Value, null);
}
}
notification = _notificationService.Create(notification);
notificationResource.Id = notification.Id;
return notificationResource;
}
private NotificationResource Update(NotificationResource notificationResource)
{
var notification = GetNotification(notificationResource);
notification.Id = notificationResource.Id;
_notificationService.Update(notification);
return notificationResource;
}
private void DeleteNotification(int id)
{
_notificationService.Delete(id);
}
private Notification GetNotification(NotificationResource notificationResource)
{
var notification = _notificationService.Schema()
.SingleOrDefault(i =>
i.Implementation.Equals(notificationResource.Implementation,
StringComparison.InvariantCultureIgnoreCase));
//TODO: How should be handle this error?
if (notification == null)
{
throw new InvalidOperationException();
}
notification.Name = notificationResource.Name;
notification.OnGrab = notificationResource.OnGrab;
notificationResource.OnDownload = notificationResource.OnDownload;
notification.Settings = (INotifcationSettings)SchemaDeserializer.DeserializeSchema(notification.Settings, notificationResource.Fields);
return notification;
}
}
}

View File

@ -6,6 +6,7 @@ using Newtonsoft.Json;
using NzbDrone.Common;
using NzbDrone.Common.Composition;
using NzbDrone.Common.Messaging;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Download;
using NzbDrone.Core.MediaFiles.Events;
@ -16,6 +17,9 @@ namespace NzbDrone.Core.Notifications
List<Notification> All();
Notification Get(int id);
List<Notification> Schema();
Notification Create(Notification notification);
Notification Update(Notification notification);
void Delete(int id);
}
public class NotificationService
@ -76,6 +80,43 @@ namespace NzbDrone.Core.Notifications
return notifications.OrderBy(n => n.Name).ToList();
}
public Notification Create(Notification notification)
{
var definition = new NotificationDefinition()
{
Name = notification.Name,
OnGrab = notification.OnGrab,
OnDownload = notification.OnDownload,
Implementation = notification.Implementation,
Settings = Json.Serialize(notification.Settings)
};
definition = _notificationRepository.Insert(definition);
notification.Id = definition.Id;
return notification;
}
public Notification Update(Notification notification)
{
var definition = _notificationRepository.Get(notification.Id);
definition.Name = notification.Name;
definition.OnGrab = notification.OnGrab;
definition.OnDownload = notification.OnDownload;
definition.Implementation = notification.Implementation;
definition.Settings = Json.Serialize(notification.Settings);
_notificationRepository.Update(definition);
return notification;
}
public void Delete(int id)
{
_notificationRepository.Delete(id);
}
private Notification ToNotification(NotificationDefinition definition)
{
var notification = new Notification();

View File

@ -7,27 +7,6 @@ define([
], function () {
NzbDrone.Settings.Indexers.ItemView = Backbone.Marionette.ItemView.extend({
template : 'Settings/Indexers/ItemTemplate',
initialize: function () {
NzbDrone.vent.on(NzbDrone.Commands.SaveSettings, this.saveSettings, this);
},
saveSettings: function () {
//this.model.save(undefined, this.syncNotification("Naming Settings Saved", "Couldn't Save Naming Settings"));
},
syncNotification: function (success, error) {
return {
success: function () {
window.alert(success);
},
error: function () {
window.alert(error);
}
};
}
template : 'Settings/Indexers/ItemTemplate'
});
});

View File

@ -14,10 +14,14 @@ define([
'click': 'addNotification'
},
initialize: function (options) {
this.notificationCollection = options.notificationCollection;
},
addNotification: function () {
this.model.set('id', undefined);
this.model.set('name', '');
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model});
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model, notificationCollection: this.notificationCollection });
NzbDrone.modalRegion.show(view);
}
});
@ -25,6 +29,16 @@ define([
NzbDrone.Settings.Notifications.AddView = Backbone.Marionette.CompositeView.extend({
itemView : NzbDrone.Settings.Notifications.AddItemView,
itemViewContainer : '.notifications .items',
template : 'Settings/Notifications/AddTemplate'
template : 'Settings/Notifications/AddTemplate',
itemViewOptions: function () {
return {
notificationCollection: this.notificationCollection
};
},
initialize: function (options) {
this.notificationCollection = options.notificationCollection;
}
});
});

View File

@ -13,8 +13,9 @@ define(['app', 'Settings/Notifications/ItemView', 'Settings/Notifications/AddVie
var schemaCollection = new NzbDrone.Settings.Notifications.Collection();
schemaCollection.url = '/api/notification/schema';
schemaCollection.fetch();
schemaCollection.url = '/api/notification';
var view = new NzbDrone.Settings.Notifications.AddView({ collection: schemaCollection});
var view = new NzbDrone.Settings.Notifications.AddView({ collection: schemaCollection, notificationCollection: this.collection});
NzbDrone.modalRegion.show(view);
}
});

View File

@ -0,0 +1,11 @@
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h3>Remove: {{name}}</h3>
</div>
<div class="modal-body">
<p>Are you sure you want to remove '{{name}}'?</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">cancel</button>
<button class="btn btn-danger x-confirm-delete">delete</button>
</div>

View File

@ -0,0 +1,24 @@
'use strict';
define(['app', 'Settings/Notifications/Model'], function () {
NzbDrone.Settings.Notifications.DeleteView = Backbone.Marionette.ItemView.extend({
template: 'Settings/Notifications/DeleteTemplate',
events: {
'click .x-confirm-delete': 'removeNotification'
},
removeNotification: function () {
var self = this;
//Success is not getting triggered: http://stackoverflow.com/questions/6988873/backbone-model-destroy-not-triggering-success-function-on-success
this.model.destroy({
wait : true,
success: function (model) {
model.collection.remove(model);
self.$el.parent().modal('hide');
}
});
}
});
});

View File

@ -13,18 +13,27 @@ 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.notificationCollection = options.notificationCollection;
},
save: function () {
var name = this.model.get('name');
var success = 'Notification Saved: ' + name;
var fail = 'Failed to save notification: ' + name;
syncNotification: function (success, error) {
this.model.save(undefined, this.syncNotification(success, fail, this));
},
syncNotification: function (success, error, context) {
return {
success: function () {
window.alert(success);
NzbDrone.Shared.Messenger.show({
message: success
});
context.notificationCollection.add(context.model, { merge: true });
context.$el.parent().modal('hide');
},
error: function () {

View File

@ -4,5 +4,5 @@
<td name="cutoffName"></td>
<td>
<i class="icon-cog x-edit" title="Edit"></i>
| Delete
<i class="icon-remove x-delete" title="Delete"></i>
</td>

View File

@ -3,7 +3,8 @@
define([
'app',
'Settings/Notifications/Collection',
'Settings/Notifications/EditView'
'Settings/Notifications/EditView',
'Settings/Notifications/DeleteView'
], function () {
@ -13,15 +14,15 @@ define([
events: {
'click .x-edit' : 'edit',
'click .x-remove': 'removeNotification'
'click .x-delete': 'deleteNotification'
},
edit: function () {
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model});
var view = new NzbDrone.Settings.Notifications.EditView({ model: this.model, notificationCollection: this.model.collection});
NzbDrone.modalRegion.show(view);
},
removeNotification: function () {
deleteNotification: function () {
var view = new NzbDrone.Settings.Notifications.DeleteView({ model: this.model});
NzbDrone.modalRegion.show(view);
}