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

68 lines
2.2 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;
using NzbDrone.Core.Messaging.Tracking;
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 IMessageAggregator _messageAggregator;
private readonly IContainer _container;
private readonly ITrackCommands _trackCommands;
public CommandModule(IMessageAggregator messageAggregator, IContainer container, ITrackCommands trackCommands)
2013-09-11 06:33:47 +00:00
: base(messageAggregator)
{
_messageAggregator = messageAggregator;
_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);
2013-09-11 06:33:47 +00:00
var trackedCommand = (Command)_messageAggregator.PublishCommandAsync(command);
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);
}
}
}
}