sonarr-repo-only/NzbDrone.Api/Commands/CommandModule.cs

69 lines
2.3 KiB
C#
Raw Normal View History

using System;
2013-09-11 06:33:47 +00:00
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Api.Extensions;
using NzbDrone.Api.Mapping;
2013-09-11 06:33:47 +00:00
using NzbDrone.Api.Validation;
using NzbDrone.Common.Composition;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.Messaging;
using NzbDrone.Core.Messaging.Commands;
2013-09-14 06:42:09 +00:00
using NzbDrone.Core.Messaging.Commands.Tracking;
using NzbDrone.Core.Messaging.Events;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.ProgressMessaging;
namespace NzbDrone.Api.Commands
{
2013-09-11 06:33:47 +00:00
public class CommandModule : NzbDroneRestModuleWithSignalR<CommandResource, Command>, IHandle<CommandUpdatedEvent>
{
private readonly ICommandExecutor _commandExecutor;
private readonly IContainer _container;
private readonly ITrackCommands _trackCommands;
public CommandModule(ICommandExecutor commandExecutor, IContainer container, ITrackCommands trackCommands)
: base(commandExecutor)
{
_commandExecutor = commandExecutor;
_container = container;
_trackCommands = trackCommands;
2013-09-11 06:33:47 +00:00
GetResourceById = GetCommand;
CreateResource = StartCommand;
GetResourceAll = GetAllCommands;
PostValidator.RuleFor(c => c.Name).NotBlank();
}
2013-09-11 06:33:47 +00:00
private CommandResource GetCommand(int id)
{
return _trackCommands.GetById(id).InjectTo<CommandResource>();
}
private int StartCommand(CommandResource commandResource)
{
var commandType =
2013-09-11 06:33:47 +00:00
_container.GetImplementations(typeof(Command))
.Single(c => c.Name.Replace("Command", "")
.Equals(commandResource.Name, StringComparison.InvariantCultureIgnoreCase));
2013-05-21 03:20:29 +00:00
dynamic command = Request.Body.FromJson(commandType);
var trackedCommand = (Command)_commandExecutor.PublishCommandAsync(command);
2013-09-11 06:33:47 +00:00
return trackedCommand.Id;
}
2013-09-11 06:33:47 +00:00
private List<CommandResource> GetAllCommands()
{
return ToListResource(_trackCommands.RunningCommands);
}
2013-09-11 06:33:47 +00:00
public void Handle(CommandUpdatedEvent message)
{
2013-09-11 06:33:47 +00:00
if (message.Command.SendUpdatesToClient)
{
BroadcastResourceChange(ModelAction.Updated, message.Command.Id);
}
}
}
}