using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WiDAL.DTOs; using WiDAL.Query.Wi; namespace WiDAL.CustomCode.WiStepExecution { public class WiStepExecutionDAL : IWiStepExecutionDAL { private readonly IQueryExecutor _qe; private readonly IValidation _Validation; public WiStepExecutionDAL(IQueryExecutor qe, IValidation validation) { _qe = qe; _Validation = validation; } public async Task GetById(int WiStepExecutionId, LoginDTO login, CancellationToken ct) { var result = await _qe.QuerySingleAsync(login, WiStepExecutionQB.GET_WISTEPEXECUTION_BYID, new { wistepexecutionid = WiStepExecutionId, TenantId = login.ClientId }); return JsonConvert.SerializeObject(result); } public async Task Save(WiStepExecutionDTO dto, bool isNew, LoginDTO login, CancellationToken ct) { try { string sql = isNew ? WiStepExecutionQB.SAVE_WISTEPECXECUTION : WiStepExecutionQB.UPDATE_WISTEPEXECUTION; var rows = await _qe.ExecuteAsync(login, sql, dto); // 🔥 important check if (rows == 0) { throw new Exception(isNew ? "Insert failed!" : "Update failed! No matching record."); } return dto.WiStepExecutionId; } catch (Exception ex) { string error = await _Validation.HandleException(ex, isNew ? ErrorResponse.SaveErrorMessage : ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task GetSelectListWiStepExecution(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, WiStepExecutionQB.GET_SELECTLIST_WISTEPXECUTION); return JsonConvert.SerializeObject(result); } public async Task DeleteWiStepExecution(int WiStepExecutionId, LoginDTO loginDTO) { try { string sql = WiStepExecutionQB.DELETE_WISTEPEXECUTION; var parameter = new { wistepexecutionid = WiStepExecutionId }; var result = await _qe.ExecuteAsync(loginDTO, sql, parameter); if (result > 0) return SuccessResponse.DeleteSuccessMessage; else return ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } } }