1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-09-17 15:02:34 +02:00
Radarr/NzbDrone.Api/Notifications/NotificationModule.cs

87 lines
3.0 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-08-26 09:14:46 +02:00
GetResourceById = GetNotification;
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
}
2013-08-26 09:14:46 +02:00
private NotificationResource GetNotification(int id)
{
return _notificationService.Get(id).InjectTo<NotificationResource>();
}
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;
}
private int Create(NotificationResource notificationResource)
{
2013-08-26 09:14:46 +02:00
var notification = ConvertToNotification(notificationResource);
return _notificationService.Create(notification).Id;
2013-05-29 04:49:17 +02:00
}
private void Update(NotificationResource notificationResource)
2013-05-29 04:49:17 +02:00
{
2013-08-26 09:14:46 +02:00
var notification = ConvertToNotification(notificationResource);
2013-05-29 04:49:17 +02:00
notification.Id = notificationResource.Id;
_notificationService.Update(notification);
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-08-26 09:14:46 +02:00
private Notification ConvertToNotification(NotificationResource notificationResource)
2013-05-29 04:49:17 +02:00
{
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
}
}