Sonarr/NzbDrone.Api/Notifications/NotificationModule.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2013-05-20 01:32:05 +00:00
using System.Collections.Generic;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Common.Reflection;
using NzbDrone.Core.Annotations;
2013-05-20 01:32:05 +00:00
using NzbDrone.Core.Notifications;
using Omu.ValueInjecter;
namespace NzbDrone.Api.Notifications
{
public class NotificationModule : NzbDroneRestModule<NotificationResource>
{
private readonly INotificationService _notificationService;
public NotificationModule(INotificationService notificationService)
{
_notificationService = notificationService;
2013-05-20 01:32:05 +00:00
GetResourceAll = GetAll;
UpdateResource = Update;
2013-05-20 01:32:05 +00:00
}
private List<NotificationResource> GetAll()
{
var notifications = _notificationService.All();
var result = new List<NotificationResource>(notifications.Count);
foreach (var notification in notifications)
{
var notificationResource = new NotificationResource();
notificationResource.InjectFrom(notification);
notificationResource.Fields = SchemaBuilder.GenerateSchema(notification.Settings);
2013-05-20 01:32:05 +00:00
result.Add(notificationResource);
2013-05-20 01:32:05 +00:00
}
return result;
}
private NotificationResource Update(NotificationResource notificationResource)
{
//Todo: Convert Resource back to Settings
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);
}
}
return notificationResource;
}
2013-05-20 01:32:05 +00:00
}
}