using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using TCMSDAL.DTO; using TCMSDAL.Interfaces; using TCMSDAL.QueryBuilders; namespace TCMSDAL.Implementations; /// /// DAL for MTCMSTESTCASE + MTCMSTESTSTEP. /// Steps are fully replaced on every save (delete-all + bulk-insert). /// public class TCMSTestCaseDAL : ITCMSTestCaseDAL { private readonly IQueryExecutor _QueryExecutor; public TCMSTestCaseDAL(IQueryExecutor queryExecutor) { _QueryExecutor = queryExecutor; } public async Task GetTestCaseAsync( int testCaseId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, TCMSTestCaseQB.GET_TESTCASE, new { TestCaseId = testCaseId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetStepsAsync( int testCaseId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, TCMSTestCaseQB.GET_STEPS, new { TestCaseId = testCaseId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); public async Task> GetTestCaseListAsync( int moduleId, string? priority, string? testType, string? status, bool? isActive, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, TCMSTestCaseQB.GET_TESTCASE_LIST, new { TenantId = login.ClientId, ModuleId = moduleId, Priority = priority, TestType = testType, Status = status, IsActive = isActive }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); public async Task> GetSelectListAsync( int moduleId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, TCMSTestCaseQB.GET_SELECT_LIST, new { TenantId = login.ClientId, ModuleId = moduleId }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); public async Task SaveTestCaseAsync( TCMSTestCaseDTO dto, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteIdentityAsync( login, TCMSTestCaseQB.SAVE_TESTCASE, new { dto.TcId, dto.Title, dto.Description, dto.ModuleId, dto.FeatureId, dto.Priority, dto.TestType, dto.ExecutionType, dto.ScenarioType, dto.Status, dto.Preconditions, dto.PostConditions, dto.Tags, dto.PieScenId, dto.PieVariantId, dto.IsAutoGenerated, TenantId = login.ClientId, CreatedBy = login.UserId }).ConfigureAwait(false); public async Task UpdateTestCaseAsync( TCMSTestCaseDTO dto, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync( login, TCMSTestCaseQB.UPDATE_TESTCASE, new { dto.TestCaseId, dto.Title, dto.Description, dto.ModuleId, dto.FeatureId, dto.Priority, dto.TestType, dto.ExecutionType, dto.ScenarioType, dto.Status, dto.Preconditions, dto.PostConditions, dto.Tags, dto.IsActive, TenantId = login.ClientId, ModifiedBy = login.UserId }, cancellationToken: ct).ConfigureAwait(false); public async Task DeleteTestCaseAsync( int testCaseId, int modifiedBy, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync( login, TCMSTestCaseQB.DELETE_TESTCASE, new { TestCaseId = testCaseId, ModifiedBy = modifiedBy, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task SaveStepsAsync( int testCaseId, IEnumerable steps, LoginDTO login, CancellationToken ct) { // Fully replace steps in a transaction await using var tx = await _QueryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { await _QueryExecutor.ExecuteAsync( login, TCMSTestCaseQB.DELETE_STEPS_FOR_TESTCASE, new { TestCaseId = testCaseId, TenantId = login.ClientId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); int order = 1; foreach (var step in steps) { await _QueryExecutor.ExecuteAsync( login, TCMSTestCaseQB.SAVE_STEP, new { TestCaseId = testCaseId, StepOrder = order++, step.StepType, step.StepText, step.ExpectedResult, step.TestData, TenantId = login.ClientId }, transaction: tx, cancellationToken: ct).ConfigureAwait(false); } await tx.CommitAsync(ct).ConfigureAwait(false); } catch { await tx.RollbackAsync(ct).ConfigureAwait(false); throw; } } public async Task UpdateLastRunStatusAsync( int testCaseId, string status, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync( login, TCMSTestCaseQB.UPDATE_LAST_RUN_STATUS, new { TestCaseId = testCaseId, LastRunStatus = status, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task GetNextSequenceAsync( string prefix, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteScalarAsync( login, TCMSTestCaseQB.GET_NEXT_SEQUENCE, new { Prefix = prefix, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task GetByPieScenIdAsync( Guid pieScenId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteScalarAsync( login, TCMSTestCaseQB.CHECK_PIESCENID_EXISTS, new { PieScenId = pieScenId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); }