1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-10-05 23:57:20 +02:00
Radarr/NzbDrone.Api/Notifications/NotificationModule.cs

93 lines
3.3 KiB
C#
Raw Normal View History

2013-05-29 04:49:17 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2013-05-20 03:32:05 +02:00
using NzbDrone.Api.ClientSchema;
using NzbDrone.Api.Mapping;
2013-05-29 08:24:45 +02:00
using NzbDrone.Api.REST;
2013-05-20 03:32:05 +02: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 03:32:05 +02:00
GetResourceAll = GetAll;
2013-05-29 04:49:17 +02:00
CreateResource = Create;
UpdateResource = Update;
2013-05-29 04:49:17 +02:00
DeleteResource = DeleteNotification;
2013-05-20 03:32:05 +02: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-06-13 17:21:38 +02:00
notificationResource.TestCommand = String.Format("test{0}", notification.Implementation.ToLowerInvariant());
2013-05-20 03:32:05 +02:00
result.Add(notificationResource);
2013-05-20 03:32:05 +02:00
}
return result;
}
2013-05-29 04:49:17 +02:00
private NotificationResource Create(NotificationResource notificationResource)
{
2013-05-29 04:49:17 +02:00
var notification = GetNotification(notificationResource);
2013-05-29 04:49:17 +02:00
notification = _notificationService.Create(notification);
notificationResource.Id = notification.Id;
var response = notification.InjectTo<NotificationResource>();
response.Fields = SchemaBuilder.GenerateSchema(notification.Settings);
2013-05-29 08:24:45 +02:00
return response;
2013-05-29 04:49:17 +02:00
}
2013-05-29 04:49:17 +02:00
private NotificationResource Update(NotificationResource notificationResource)
{
var notification = GetNotification(notificationResource);
notification.Id = notificationResource.Id;
2013-05-29 08:24:45 +02:00
notification = _notificationService.Update(notification);
var response = notification.InjectTo<NotificationResource>();
response.Fields = SchemaBuilder.GenerateSchema(notification.Settings);
return response;
2013-05-29 04:49:17 +02:00
}
2013-05-29 04:49:17 +02:00
private void DeleteNotification(int id)
{
_notificationService.Delete(id);
}
2013-05-29 04:49:17 +02:00
private Notification GetNotification(NotificationResource notificationResource)
{
var notification = _notificationService.Schema()
.SingleOrDefault(i =>
i.Implementation.Equals(notificationResource.Implementation,
StringComparison.InvariantCultureIgnoreCase));
2013-05-29 04:49:17 +02:00
if (notification == null)
{
2013-05-29 08:24:45 +02:00
throw new BadRequestException("Invalid Notification Implementation");
}
2013-05-29 08:24:45 +02:00
notification.InjectFrom(notificationResource);
notification.Settings = SchemaDeserializer.DeserializeSchema(notification.Settings, notificationResource.Fields);
2013-05-29 04:49:17 +02:00
return notification;
}
2013-05-20 03:32:05 +02:00
}
}