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