2013-04-27 02:03:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2013-08-26 05:14:55 +00:00
|
|
|
|
using Nancy;
|
2013-04-27 02:03:34 +00:00
|
|
|
|
using NzbDrone.Api.Extensions;
|
2013-05-13 00:36:23 +00:00
|
|
|
|
using NzbDrone.Common.Composition;
|
2013-04-27 02:03:34 +00:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-08-28 06:51:42 +00:00
|
|
|
|
using NzbDrone.Common.Messaging.Manager;
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Commands
|
|
|
|
|
{
|
|
|
|
|
public class CommandModule : NzbDroneRestModule<CommandResource>
|
|
|
|
|
{
|
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-05-13 00:36:23 +00:00
|
|
|
|
private readonly IContainer _container;
|
2013-08-28 06:51:42 +00:00
|
|
|
|
private readonly IManageCommands _commandManager;
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-08-28 06:51:42 +00:00
|
|
|
|
public CommandModule(IMessageAggregator messageAggregator, IContainer container, IManageCommands commandManager)
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-05-13 00:36:23 +00:00
|
|
|
|
_container = container;
|
2013-08-28 06:51:42 +00:00
|
|
|
|
_commandManager = commandManager;
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-08-26 05:14:55 +00:00
|
|
|
|
Post["/"] = x => RunCommand(ReadResourceFromRequest());
|
2013-08-28 06:51:42 +00:00
|
|
|
|
Get["/"] = x => GetAllCommands();
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-26 05:14:55 +00:00
|
|
|
|
private Response RunCommand(CommandResource resource)
|
2013-04-27 02:03:34 +00:00
|
|
|
|
{
|
2013-05-13 00:36:23 +00:00
|
|
|
|
var commandType =
|
|
|
|
|
_container.GetImplementations(typeof(ICommand))
|
|
|
|
|
.Single(c => c.Name.Replace("Command", "")
|
2013-05-13 02:52:55 +00:00
|
|
|
|
.Equals(resource.Command, StringComparison.InvariantCultureIgnoreCase));
|
2013-04-27 02:03:34 +00:00
|
|
|
|
|
2013-05-21 03:20:29 +00:00
|
|
|
|
dynamic command = Request.Body.FromJson(commandType);
|
2013-05-08 05:47:15 +00:00
|
|
|
|
_messageAggregator.PublishCommand(command);
|
|
|
|
|
|
2013-08-26 05:25:56 +00:00
|
|
|
|
return resource.AsResponse(HttpStatusCode.Created);
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
2013-08-28 06:51:42 +00:00
|
|
|
|
|
|
|
|
|
private Response GetAllCommands()
|
|
|
|
|
{
|
|
|
|
|
return _commandManager.Items.AsResponse();
|
|
|
|
|
}
|
2013-04-27 02:03:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|