1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Api/Notifications/NotificationModule.cs

87 lines
3.0 KiB
C#
Raw Normal View History

2013-05-29 05:49:17 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
2013-05-20 04:32:05 +03:00
using NzbDrone.Api.ClientSchema;
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 04:32:05 +03:00
GetResourceAll = GetAll;
2013-05-29 05:49:17 +03:00
CreateResource = Create;
UpdateResource = Update;
2013-05-29 05:49:17 +03:00
DeleteResource = DeleteNotification;
2013-05-20 04:32:05 +03: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 04:32:05 +03:00
result.Add(notificationResource);
2013-05-20 04:32:05 +03:00
}
return result;
}
2013-05-29 05:49:17 +03:00
private NotificationResource Create(NotificationResource notificationResource)
{
2013-05-29 05:49:17 +03:00
var notification = GetNotification(notificationResource);
2013-05-29 05:49:17 +03:00
notification = _notificationService.Create(notification);
notificationResource.Id = notification.Id;
2013-05-29 05:49:17 +03:00
return notificationResource;
}
2013-05-29 05:49:17 +03:00
private NotificationResource Update(NotificationResource notificationResource)
{
var notification = GetNotification(notificationResource);
notification.Id = notificationResource.Id;
_notificationService.Update(notification);
2013-05-29 05:49:17 +03:00
return notificationResource;
}
2013-05-29 05:49:17 +03:00
private void DeleteNotification(int id)
{
_notificationService.Delete(id);
}
2013-05-29 05:49:17 +03:00
private Notification GetNotification(NotificationResource notificationResource)
{
var notification = _notificationService.Schema()
.SingleOrDefault(i =>
i.Implementation.Equals(notificationResource.Implementation,
StringComparison.InvariantCultureIgnoreCase));
2013-05-29 05:49:17 +03:00
//TODO: How should be handle this error?
if (notification == null)
{
throw new InvalidOperationException();
}
2013-05-29 05:49:17 +03:00
notification.Name = notificationResource.Name;
notification.OnGrab = notificationResource.OnGrab;
notificationResource.OnDownload = notificationResource.OnDownload;
notification.Settings = (INotifcationSettings)SchemaDeserializer.DeserializeSchema(notification.Settings, notificationResource.Fields);
return notification;
}
2013-05-20 04:32:05 +03:00
}
}