From 1d007be8fd8172c190a86ea0a3d079f6364bb95c Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 28 May 2013 19:49:17 -0700 Subject: [PATCH] Notifications wired up --- .../ClientSchema/SchemaDeserializer.cs | 15 +++- .../Notifications/NotificationModule.cs | 69 ++++++++++++------- .../Notifications/NotificationService.cs | 41 +++++++++++ UI/Settings/Indexers/ItemView.js | 23 +------ UI/Settings/Notifications/AddView.js | 18 ++++- UI/Settings/Notifications/CollectionView.js | 3 +- UI/Settings/Notifications/DeleteTemplate.html | 11 +++ UI/Settings/Notifications/DeleteView.js | 24 +++++++ UI/Settings/Notifications/EditView.js | 23 +++++-- UI/Settings/Notifications/ItemTemplate.html | 2 +- UI/Settings/Notifications/ItemView.js | 9 +-- 11 files changed, 174 insertions(+), 64 deletions(-) create mode 100644 UI/Settings/Notifications/DeleteTemplate.html create mode 100644 UI/Settings/Notifications/DeleteView.js diff --git a/NzbDrone.Api/ClientSchema/SchemaDeserializer.cs b/NzbDrone.Api/ClientSchema/SchemaDeserializer.cs index d638bb0bc..22aac90e4 100644 --- a/NzbDrone.Api/ClientSchema/SchemaDeserializer.cs +++ b/NzbDrone.Api/ClientSchema/SchemaDeserializer.cs @@ -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); + } } } diff --git a/NzbDrone.Api/Notifications/NotificationModule.cs b/NzbDrone.Api/Notifications/NotificationModule.cs index c7e5a74e3..7532bbc4d 100644 --- a/NzbDrone.Api/Notifications/NotificationModule.cs +++ b/NzbDrone.Api/Notifications/NotificationModule.cs @@ -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 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(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; + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Notifications/NotificationService.cs b/NzbDrone.Core/Notifications/NotificationService.cs index 8fbab06d6..a425b5ad7 100644 --- a/NzbDrone.Core/Notifications/NotificationService.cs +++ b/NzbDrone.Core/Notifications/NotificationService.cs @@ -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 All(); Notification Get(int id); List 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(); diff --git a/UI/Settings/Indexers/ItemView.js b/UI/Settings/Indexers/ItemView.js index 24ae065dc..0dc112854 100644 --- a/UI/Settings/Indexers/ItemView.js +++ b/UI/Settings/Indexers/ItemView.js @@ -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' }); }); diff --git a/UI/Settings/Notifications/AddView.js b/UI/Settings/Notifications/AddView.js index 6b418ade6..3a0212beb 100644 --- a/UI/Settings/Notifications/AddView.js +++ b/UI/Settings/Notifications/AddView.js @@ -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; + } }); }); diff --git a/UI/Settings/Notifications/CollectionView.js b/UI/Settings/Notifications/CollectionView.js index a3232f405..b44988011 100644 --- a/UI/Settings/Notifications/CollectionView.js +++ b/UI/Settings/Notifications/CollectionView.js @@ -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); } }); diff --git a/UI/Settings/Notifications/DeleteTemplate.html b/UI/Settings/Notifications/DeleteTemplate.html new file mode 100644 index 000000000..ce1352f09 --- /dev/null +++ b/UI/Settings/Notifications/DeleteTemplate.html @@ -0,0 +1,11 @@ + + + \ No newline at end of file diff --git a/UI/Settings/Notifications/DeleteView.js b/UI/Settings/Notifications/DeleteView.js new file mode 100644 index 000000000..65474233e --- /dev/null +++ b/UI/Settings/Notifications/DeleteView.js @@ -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'); + } + }); + } + }); +}); diff --git a/UI/Settings/Notifications/EditView.js b/UI/Settings/Notifications/EditView.js index e8a2108c4..13724b49a 100644 --- a/UI/Settings/Notifications/EditView.js +++ b/UI/Settings/Notifications/EditView.js @@ -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 () { diff --git a/UI/Settings/Notifications/ItemTemplate.html b/UI/Settings/Notifications/ItemTemplate.html index cefcc0b48..3b16ea0a3 100644 --- a/UI/Settings/Notifications/ItemTemplate.html +++ b/UI/Settings/Notifications/ItemTemplate.html @@ -4,5 +4,5 @@ - | Delete + \ No newline at end of file diff --git a/UI/Settings/Notifications/ItemView.js b/UI/Settings/Notifications/ItemView.js index a6c4ca59a..d6882d36f 100644 --- a/UI/Settings/Notifications/ItemView.js +++ b/UI/Settings/Notifications/ItemView.js @@ -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); }