new routes

master
Tudor Stanciu 2022-01-10 11:41:30 +02:00
parent f9881831dc
commit 8e665d2d73
7 changed files with 164 additions and 2 deletions

View File

@ -0,0 +1,52 @@
using AutoMapper;
using MediatR;
using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.Domain.Models;
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 RestartHandler : IRequestHandler<Restart, RestartResult>
{
private readonly ILogger<RestartHandler> _logger;
private readonly IPowerService _powerService;
private readonly IValidationService _validationService;
private readonly IMapper _mapper;
public RestartHandler(ILogger<RestartHandler> logger, IPowerService powerService, IValidationService validationService, IMapper mapper)
{
_logger=logger;
_powerService=powerService;
_validationService=validationService;
_mapper=mapper;
}
public async Task<RestartResult> Handle(Restart command, CancellationToken cancellationToken)
{
return await Task.Run(() => Handle(command));
}
private RestartResult Handle(Restart command)
{
_validationService.ValidateRestrictions(command.Owner);
_logger.LogDebug($"Start restarting the system.");
var stopWatch = new Stopwatch();
stopWatch.Start();
var options = _mapper.Map<PowerOptions>(command.Options);
_powerService.Restart(options);
stopWatch.Stop();
_logger.LogDebug($"System restart finished - {stopWatch.ElapsedMilliseconds:N0} ms");
return new RestartResult() { Success = true };
}
}
}

View File

@ -0,0 +1,52 @@
using AutoMapper;
using MediatR;
using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.Domain.Models;
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 SleepHandler : IRequestHandler<Sleep, SleepResult>
{
private readonly ILogger<SleepHandler> _logger;
private readonly IPowerService _powerService;
private readonly IValidationService _validationService;
private readonly IMapper _mapper;
public SleepHandler(ILogger<SleepHandler> logger, IPowerService powerService, IValidationService validationService, IMapper mapper)
{
_logger=logger;
_powerService=powerService;
_validationService=validationService;
_mapper=mapper;
}
public async Task<SleepResult> Handle(Sleep command, CancellationToken cancellationToken)
{
return await Task.Run(() => Handle(command));
}
private SleepResult Handle(Sleep command)
{
_validationService.ValidateRestrictions(command.Owner);
_logger.LogDebug($"Start sleeping the system.");
var stopWatch = new Stopwatch();
stopWatch.Start();
var options = _mapper.Map<PowerOptions>(command.Options);
_powerService.Sleep(options);
stopWatch.Stop();
_logger.LogDebug($"System sleeping finished - {stopWatch.ElapsedMilliseconds:N0} ms");
return new SleepResult() { Success = true };
}
}
}

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 Restart : Command<RestartResult>
{
public ActionOwner Owner { get; set; }
public ActionOptions Options { get; set; }
}
}

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 Sleep : Command<SleepResult>
{
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 RestartResult
{
public bool Success { get; set; }
public HostInfo Host { get; set; }
}
}

View File

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

View File

@ -19,9 +19,23 @@ namespace NetworkResurrector.Agent.Controllers
} }
[HttpPost("shutdown")] [HttpPost("shutdown")]
public async Task<IActionResult> Shutdown([FromBody] Shutdown shutdownMachine) public async Task<IActionResult> Shutdown([FromBody] Shutdown shutdown)
{ {
var result = await _mediator.Send(shutdownMachine); var result = await _mediator.Send(shutdown);
return Ok(result);
}
[HttpPost("restart")]
public async Task<IActionResult> Restart([FromBody] Restart restart)
{
var result = await _mediator.Send(restart);
return Ok(result);
}
[HttpPost("sleep")]
public async Task<IActionResult> Sleep([FromBody] Sleep sleep)
{
var result = await _mediator.Send(sleep);
return Ok(result); return Ok(result);
} }
} }