master
Tudor Stanciu 2022-01-10 13:15:08 +02:00
parent bfc1e863ad
commit 4d3b9cc4bf
5 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,47 @@
using MediatR;
using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.PublishedLanguage.Commands;
using NetworkResurrector.Agent.PublishedLanguage.Events;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace NetworkResurrector.Agent.Application.CommandHandlers
{
public class LockHandler : IRequestHandler<Lock, LockResult>
{
private readonly ILogger<LockHandler> _logger;
private readonly IPowerService _powerService;
private readonly IValidationService _validationService;
public LockHandler(ILogger<LockHandler> logger, IPowerService powerService, IValidationService validationService)
{
_logger=logger;
_powerService=powerService;
_validationService=validationService;
}
public async Task<LockResult> Handle(Lock command, CancellationToken cancellationToken)
{
return await Task.Run(() => Handle(command));
}
private LockResult Handle(Lock command)
{
_validationService.ValidateRestrictions(command.Owner);
_logger.LogDebug($"Start locking the user.");
var stopWatch = new Stopwatch();
stopWatch.Start();
_powerService.Lock();
stopWatch.Stop();
_logger.LogDebug($"User lock finished - {stopWatch.ElapsedMilliseconds:N0} ms");
return new LockResult() { Success = true };
}
}
}

View File

@ -55,7 +55,7 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
msg.Append($"Errors:\n {commandError}\n");
msg.Append($"Process ended at: {DateTime.Now}");
return (commandError.Length > 0, msg.ToString());
return (commandError.Length == 0, msg.ToString());
}
}
}

View File

@ -0,0 +1,12 @@
using NDB.Application.DataContracts;
using NetworkResurrector.Agent.PublishedLanguage.Dto;
using NetworkResurrector.Agent.PublishedLanguage.Events;
namespace NetworkResurrector.Agent.PublishedLanguage.Commands
{
public class Lock : Command<LockResult>
{
public ActionOwner Owner { get; set; }
public ActionOptions Options { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using NetworkResurrector.Agent.PublishedLanguage.Dto;
namespace NetworkResurrector.Agent.PublishedLanguage.Events
{
public class LockResult
{
public bool Success { get; set; }
public HostInfo Host { get; set; }
}
}

View File

@ -45,5 +45,12 @@ namespace NetworkResurrector.Agent.Controllers
var result = await _mediator.Send(logout);
return Ok(result);
}
[HttpPost("lock")]
public async Task<IActionResult> Lock([FromBody] Lock _lock)
{
var result = await _mediator.Send(_lock);
return Ok(result);
}
}
}