sonarr-repo-only/NzbDrone.Api/Notifications/NotificationModule.cs

87 lines
3.0 KiB
C#
Raw Normal View History

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