2 changed files with 259 additions and 0 deletions
@ -0,0 +1,247 @@ |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using X1.Application.Features.TestScenarios.Queries.GetTestScenarios; |
|||
using X1.Application.Features.TestScenarios.Queries.GetTestScenarioById; |
|||
using X1.Application.Features.TestScenarios.Commands.CreateTestScenario; |
|||
using X1.Application.Features.TestScenarios.Commands.UpdateTestScenario; |
|||
using X1.Application.Features.TestScenarios.Commands.DeleteTestScenario; |
|||
using X1.Application.Features.ScenarioTestCases.Commands.CreateScenarioTestCase; |
|||
using X1.Application.Features.ScenarioTestCases.Queries.GetScenarioTestCases; |
|||
using X1.Presentation.Abstractions; |
|||
using X1.Domain.Common; |
|||
using MediatR; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace X1.Presentation.Controllers; |
|||
|
|||
/// <summary>
|
|||
/// 测试场景控制器
|
|||
/// </summary>
|
|||
[ApiController] |
|||
[Route("api/testscenarios")] |
|||
[Authorize] |
|||
public class TestScenariosController : ApiController |
|||
{ |
|||
private readonly ILogger<TestScenariosController> _logger; |
|||
|
|||
/// <summary>
|
|||
/// 初始化测试场景控制器
|
|||
/// </summary>
|
|||
public TestScenariosController(IMediator mediator, ILogger<TestScenariosController> logger) |
|||
: base(mediator) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取测试场景列表
|
|||
/// </summary>
|
|||
/// <param name="searchTerm">搜索关键词</param>
|
|||
/// <param name="type">场景类型</param>
|
|||
/// <param name="isEnabled">启用状态</param>
|
|||
/// <param name="pageNumber">页码</param>
|
|||
/// <param name="pageSize">每页大小</param>
|
|||
/// <returns>测试场景列表</returns>
|
|||
[HttpGet] |
|||
public async Task<OperationResult<GetTestScenariosResponse>> GetTestScenarios( |
|||
[FromQuery] string? searchTerm = null, |
|||
[FromQuery] string? type = null, |
|||
[FromQuery] bool? isEnabled = null, |
|||
[FromQuery] int pageNumber = 1, |
|||
[FromQuery] int pageSize = 10) |
|||
{ |
|||
_logger.LogInformation("获取测试场景列表,搜索条件: {SearchTerm}, 类型: {Type}, 启用状态: {IsEnabled}, 页码: {PageNumber}, 每页大小: {PageSize}", |
|||
searchTerm, type, isEnabled, pageNumber, pageSize); |
|||
|
|||
var query = new GetTestScenariosQuery |
|||
{ |
|||
SearchTerm = searchTerm, |
|||
Type = !string.IsNullOrEmpty(type) && Enum.TryParse<X1.Domain.Entities.TestCase.ScenarioType>(type, out var scenarioType) ? scenarioType : null, |
|||
IsEnabled = isEnabled, |
|||
PageNumber = pageNumber, |
|||
PageSize = pageSize |
|||
}; |
|||
|
|||
var result = await mediator.Send(query); |
|||
if (!result.IsSuccess) |
|||
{ |
|||
_logger.LogWarning("获取测试场景列表失败: {ErrorMessages}", string.Join(", ", result.ErrorMessages ?? new List<string>())); |
|||
return result; |
|||
} |
|||
|
|||
_logger.LogInformation("成功获取测试场景列表,总数: {TotalCount}", result.Data?.TotalCount); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据ID获取测试场景详情
|
|||
/// </summary>
|
|||
/// <param name="id">测试场景ID</param>
|
|||
/// <param name="includeTestCases">是否包含测试用例</param>
|
|||
/// <returns>测试场景详情</returns>
|
|||
[HttpGet("{id}")] |
|||
public async Task<OperationResult<GetTestScenarioByIdResponse>> GetTestScenarioById( |
|||
string id, |
|||
[FromQuery] bool includeTestCases = false) |
|||
{ |
|||
_logger.LogInformation("获取测试场景详情,ID: {Id}, 包含测试用例: {IncludeTestCases}", id, includeTestCases); |
|||
|
|||
var query = new GetTestScenarioByIdQuery |
|||
{ |
|||
TestScenarioId = id, |
|||
IncludeTestCases = includeTestCases |
|||
}; |
|||
var result = await mediator.Send(query); |
|||
if (!result.IsSuccess) |
|||
{ |
|||
_logger.LogWarning("获取测试场景详情失败,ID: {Id}, 错误: {ErrorMessages}", |
|||
id, string.Join(", ", result.ErrorMessages ?? new List<string>())); |
|||
return result; |
|||
} |
|||
|
|||
_logger.LogInformation("成功获取测试场景详情,ID: {Id}, 名称: {Name}", |
|||
id, result.Data?.TestScenario?.ScenarioName); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 创建测试场景
|
|||
/// </summary>
|
|||
/// <param name="command">创建测试场景命令</param>
|
|||
/// <returns>创建结果</returns>
|
|||
[HttpPost] |
|||
public async Task<OperationResult<CreateTestScenarioResponse>> CreateTestScenario([FromBody] CreateTestScenarioCommand command) |
|||
{ |
|||
_logger.LogInformation("开始创建测试场景,场景名称: {ScenarioName}, 类型: {Type}", |
|||
command.ScenarioName, command.Type); |
|||
|
|||
var result = await mediator.Send(command); |
|||
if (!result.IsSuccess) |
|||
{ |
|||
_logger.LogWarning("创建测试场景失败,场景名称: {ScenarioName}, 错误: {ErrorMessages}", |
|||
command.ScenarioName, string.Join(", ", result.ErrorMessages ?? new List<string>())); |
|||
return result; |
|||
} |
|||
|
|||
_logger.LogInformation("成功创建测试场景,ID: {Id}, 名称: {ScenarioName}", |
|||
result.Data?.Id, result.Data?.ScenarioName); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 更新测试场景
|
|||
/// </summary>
|
|||
/// <param name="id">测试场景ID</param>
|
|||
/// <param name="command">更新测试场景命令</param>
|
|||
/// <returns>更新结果</returns>
|
|||
[HttpPut("{id}")] |
|||
public async Task<OperationResult<UpdateTestScenarioResponse>> UpdateTestScenario( |
|||
string id, |
|||
[FromBody] UpdateTestScenarioCommand command) |
|||
{ |
|||
_logger.LogInformation("开始更新测试场景,场景ID: {Id}", id); |
|||
|
|||
// 确保命令中的ID与路由参数一致
|
|||
command.TestScenarioId = id; |
|||
|
|||
var result = await mediator.Send(command); |
|||
if (!result.IsSuccess) |
|||
{ |
|||
_logger.LogWarning("更新测试场景失败,场景ID: {Id}, 错误: {ErrorMessages}", |
|||
id, string.Join(", ", result.ErrorMessages ?? new List<string>())); |
|||
return result; |
|||
} |
|||
|
|||
_logger.LogInformation("成功更新测试场景,场景ID: {Id}", id); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 删除测试场景
|
|||
/// </summary>
|
|||
/// <param name="id">测试场景ID</param>
|
|||
/// <returns>删除结果</returns>
|
|||
[HttpDelete("{id}")] |
|||
public async Task<OperationResult<bool>> DeleteTestScenario(string id) |
|||
{ |
|||
_logger.LogInformation("开始删除测试场景,场景ID: {Id}", id); |
|||
|
|||
var command = new DeleteTestScenarioCommand { TestScenarioId = id }; |
|||
var result = await mediator.Send(command); |
|||
if (!result.IsSuccess) |
|||
{ |
|||
_logger.LogWarning("删除测试场景失败,场景ID: {Id}, 错误: {ErrorMessages}", |
|||
id, string.Join(", ", result.ErrorMessages ?? new List<string>())); |
|||
return result; |
|||
} |
|||
|
|||
_logger.LogInformation("成功删除测试场景,场景ID: {Id}", id); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取场景测试用例列表
|
|||
/// </summary>
|
|||
/// <param name="scenarioId">场景ID</param>
|
|||
/// <param name="isEnabled">是否只获取启用的测试用例</param>
|
|||
/// <param name="includeDetails">是否包含详细信息</param>
|
|||
/// <returns>场景测试用例列表</returns>
|
|||
[HttpGet("{scenarioId}/testcases")] |
|||
public async Task<OperationResult<GetScenarioTestCasesResponse>> GetScenarioTestCases( |
|||
string scenarioId, |
|||
[FromQuery] bool? isEnabled = null, |
|||
[FromQuery] bool includeDetails = false) |
|||
{ |
|||
_logger.LogInformation("获取场景测试用例列表,场景ID: {ScenarioId}, 启用状态: {IsEnabled}, 包含详情: {IncludeDetails}", |
|||
scenarioId, isEnabled, includeDetails); |
|||
|
|||
var query = new GetScenarioTestCasesQuery |
|||
{ |
|||
ScenarioId = scenarioId, |
|||
IsEnabled = isEnabled, |
|||
IncludeDetails = includeDetails |
|||
}; |
|||
|
|||
var result = await mediator.Send(query); |
|||
if (!result.IsSuccess) |
|||
{ |
|||
_logger.LogWarning("获取场景测试用例列表失败,场景ID: {ScenarioId}, 错误: {ErrorMessages}", |
|||
scenarioId, string.Join(", ", result.ErrorMessages ?? new List<string>())); |
|||
return result; |
|||
} |
|||
|
|||
_logger.LogInformation("成功获取场景测试用例列表,场景ID: {ScenarioId}, 测试用例数量: {Count}", |
|||
scenarioId, result.Data?.ScenarioTestCases?.Count ?? 0); |
|||
return result; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 创建场景测试用例
|
|||
/// </summary>
|
|||
/// <param name="scenarioId">场景ID</param>
|
|||
/// <param name="command">创建场景测试用例命令</param>
|
|||
/// <returns>创建结果</returns>
|
|||
[HttpPost("{scenarioId}/testcases")] |
|||
public async Task<OperationResult<CreateScenarioTestCaseResponse>> CreateScenarioTestCase( |
|||
string scenarioId, |
|||
[FromBody] CreateScenarioTestCaseCommand command) |
|||
{ |
|||
_logger.LogInformation("开始创建场景测试用例,场景ID: {ScenarioId}, 测试用例数量: {TestCaseCount}", |
|||
scenarioId, command.TestCases?.Count ?? 0); |
|||
|
|||
// 确保命令中的场景ID与路由参数一致
|
|||
command.ScenarioId = scenarioId; |
|||
|
|||
var result = await mediator.Send(command); |
|||
if (!result.IsSuccess) |
|||
{ |
|||
_logger.LogWarning("创建场景测试用例失败,场景ID: {ScenarioId}, 错误: {ErrorMessages}", |
|||
scenarioId, string.Join(", ", result.ErrorMessages ?? new List<string>())); |
|||
return result; |
|||
} |
|||
|
|||
_logger.LogInformation("成功创建场景测试用例,场景ID: {ScenarioId}, 成功数量: {SuccessCount}, 失败数量: {FailureCount}", |
|||
scenarioId, result.Data?.SuccessCount ?? 0, result.Data?.FailureCount ?? 0); |
|||
return result; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue