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

92 lines
3.1 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.Api.Mapping;
2013-05-29 09:24:45 +03:00
using NzbDrone.Api.REST;
2013-05-20 04:32:05 +03: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 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;
var response = notification.InjectTo<NotificationResource>();
response.Fields = SchemaBuilder.GenerateSchema(notification.Settings);
2013-05-29 09:24:45 +03:00
return response;
2013-05-29 05:49:17 +03:00
}
2013-05-29 05:49:17 +03:00
private NotificationResource Update(NotificationResource notificationResource)
{
var notification = GetNotification(notificationResource);
notification.Id = notificationResource.Id;
2013-05-29 09:24:45 +03:00
notification = _notificationService.Update(notification);
var response = notification.InjectTo<NotificationResource>();
response.Fields = SchemaBuilder.GenerateSchema(notification.Settings);
return response;
2013-05-29 05:49:17 +03:00
}
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
if (notification == null)
{
2013-05-29 09:24:45 +03:00
throw new BadRequestException("Invalid Notification Implementation");
}
2013-05-29 09:24:45 +03:00
notification.InjectFrom(notificationResource);
notification.Settings = SchemaDeserializer.DeserializeSchema(notification.Settings, notificationResource.Fields);
2013-05-29 05:49:17 +03:00
return notification;
}
2013-05-20 04:32:05 +03:00
}
}