1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-21 01:49:57 +02:00
Sonarr/NzbDrone.Api/Commands/CommandModule.cs

68 lines
2.2 KiB
C#
Raw Normal View History

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