using System.Threading.Tasks;
using CoreAgent.Application.Commands.NetworkConfig;
using CoreAgent.Application.Queries.NetworkConfig;
using CoreAgent.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace CoreAgent.API.Controllers
{
///
/// 网络配置控制器
///
[ApiVersion("1.0")]
public class NetworkConfigController : BaseApiController
{
///
/// 构造函数
///
/// 中介者
/// 日志记录器
public NetworkConfigController(IMediator mediator, ILogger logger)
: base(mediator, logger)
{
}
///
/// 获取所有网络配置
///
/// 网络配置列表
[HttpGet]
[ProducesResponseType(typeof(List), 200)]
public async Task GetAll()
{
return await HandleRequest>(
new GetAllNetworkConfigurationsQuery());
}
///
/// 根据配置键获取网络配置
///
/// 配置键值
/// 网络配置
[HttpGet("{configKey}")]
[ProducesResponseType(typeof(NetworkConfiguration), 200)]
[ProducesResponseType(404)]
public async Task GetByKey(string configKey)
{
return await HandleRequest(
new GetNetworkConfigurationByKeyQuery { ConfigKey = configKey });
}
///
/// 创建网络配置
///
/// 创建网络配置命令
/// 创建的网络配置
[HttpPost]
[ProducesResponseType(typeof(NetworkConfiguration), 201)]
[ProducesResponseType(400)]
public async Task Create([FromBody] CreateNetworkConfigurationCommand command)
{
var result = await HandleRequest(command);
if (result is OkObjectResult okResult)
{
return CreatedAtAction(nameof(GetByKey),
new { configKey = command.ConfigKey },
okResult.Value);
}
return result;
}
///
/// 删除网络配置
///
/// 配置键值
/// 操作结果
[HttpDelete("{configKey}")]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public async Task Delete(string configKey)
{
var command = new DeleteNetworkConfigurationCommand { ConfigKey = configKey };
return await HandleRequest(command);
}
}
}