You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
4.2 KiB
99 lines
4.2 KiB
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
using X1.Domain.Common;
|
|
using X1.Domain.Entities.TestCase;
|
|
using X1.Domain.Repositories.TestCase;
|
|
using X1.Application.Features.Common.Dtos;
|
|
|
|
namespace X1.Application.Features.ScenarioTestCases.Queries.GetScenarioTestCases;
|
|
|
|
/// <summary>
|
|
/// 获取场景测试用例列表查询处理器
|
|
/// </summary>
|
|
public class GetScenarioTestCasesQueryHandler : IRequestHandler<GetScenarioTestCasesQuery, OperationResult<GetScenarioTestCasesResponse>>
|
|
{
|
|
private readonly IScenarioTestCaseRepository _scenarioTestCaseRepository;
|
|
private readonly ITestScenarioRepository _testScenarioRepository;
|
|
private readonly ILogger<GetScenarioTestCasesQueryHandler> _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化查询处理器
|
|
/// </summary>
|
|
public GetScenarioTestCasesQueryHandler(
|
|
IScenarioTestCaseRepository scenarioTestCaseRepository,
|
|
ITestScenarioRepository testScenarioRepository,
|
|
ILogger<GetScenarioTestCasesQueryHandler> logger)
|
|
{
|
|
_scenarioTestCaseRepository = scenarioTestCaseRepository;
|
|
_testScenarioRepository = testScenarioRepository;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理获取场景测试用例列表查询
|
|
/// </summary>
|
|
public async Task<OperationResult<GetScenarioTestCasesResponse>> Handle(GetScenarioTestCasesQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("开始获取场景测试用例列表,场景ID: {ScenarioId}, 包含详细信息: {IncludeDetails}",
|
|
request.ScenarioId, request.IncludeDetails);
|
|
|
|
// 验证场景是否存在
|
|
var scenario = await _testScenarioRepository.GetByIdAsync(request.ScenarioId, cancellationToken:cancellationToken);
|
|
if (scenario == null)
|
|
{
|
|
_logger.LogWarning("测试场景不存在,场景ID: {ScenarioId}", request.ScenarioId);
|
|
return OperationResult<GetScenarioTestCasesResponse>.CreateFailure($"测试场景不存在,场景ID: {request.ScenarioId}");
|
|
}
|
|
|
|
// 获取场景测试用例
|
|
IEnumerable<ScenarioTestCase> testCases;
|
|
if (request.IncludeDetails)
|
|
{
|
|
testCases = await _scenarioTestCaseRepository.GetWithDetailsByScenarioIdAsync(request.ScenarioId, cancellationToken);
|
|
}
|
|
else if (request.IsEnabled.HasValue)
|
|
{
|
|
testCases = await _scenarioTestCaseRepository.GetEnabledByScenarioIdAsync(request.ScenarioId, cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
testCases = await _scenarioTestCaseRepository.GetByScenarioIdAsync(request.ScenarioId, cancellationToken);
|
|
}
|
|
|
|
// 转换为DTO
|
|
var testCaseDtos = testCases.Select(x => new ScenarioTestCaseDto
|
|
{
|
|
ScenarioTestCaseId = x.Id,
|
|
ScenarioId = x.ScenarioId,
|
|
TestCaseFlowId = x.TestCaseFlowId,
|
|
TestCaseFlowName = x.TestCaseFlow?.Name,
|
|
ExecutionOrder = x.ExecutionOrder,
|
|
LoopCount = x.LoopCount,
|
|
IsEnabled = x.IsEnabled,
|
|
CreatedAt = x.CreatedAt,
|
|
CreatedBy = x.CreatedBy,
|
|
UpdatedAt = x.UpdatedAt,
|
|
UpdatedBy = x.UpdatedBy
|
|
}).ToList();
|
|
|
|
// 构建响应
|
|
var response = new GetScenarioTestCasesResponse
|
|
{
|
|
ScenarioId = request.ScenarioId,
|
|
TestCases = testCaseDtos,
|
|
TotalCount = testCaseDtos.Count
|
|
};
|
|
|
|
_logger.LogInformation("获取场景测试用例列表成功,场景ID: {ScenarioId}, 测试用例数量: {TestCaseCount}",
|
|
request.ScenarioId, testCaseDtos.Count);
|
|
return OperationResult<GetScenarioTestCasesResponse>.CreateSuccess(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取场景测试用例列表时发生错误,场景ID: {ScenarioId}", request.ScenarioId);
|
|
return OperationResult<GetScenarioTestCasesResponse>.CreateFailure($"获取场景测试用例列表时发生错误: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|