using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Newtonsoft.Json; using WiDAL.DTOs; using WiDAL.Query.Wi; namespace WiDAL.CustomCode.WiProcess; public class WiProcessDAL : IWiProcessDAL { private readonly IQueryExecutor _qe; private readonly IValidation _Validation; public WiProcessDAL(IQueryExecutor qe, IValidation validation) { _qe = qe; _Validation = validation; } public async Task GetList(LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync(login, WiProcessQB.GET_LIST, new { TenantId = login.ClientId }, cancellationToken: ct); return JsonConvert.SerializeObject(result); } public async Task GetById(int wiProcessId, LoginDTO login, CancellationToken ct) { var result = await _qe.QuerySingleAsync(login, WiProcessQB.GET_BY_ID, new { WiProcessId = wiProcessId, TenantId = login.ClientId }); return JsonConvert.SerializeObject(result); } public async Task Save(WiProcessDTO dto, bool isNew, LoginDTO login, CancellationToken ct) { try { string sql = isNew ? WiProcessQB.INSERT : WiProcessQB.UPDATE; 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.WiProcessId; } catch (Exception ex) { string error = await _Validation.HandleException(ex, isNew ? ErrorResponse.SaveErrorMessage : ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task GetSelectListWiProcess(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, WiProcessQB.GET_SELECTLIST_WIPROCESS); return JsonConvert.SerializeObject(result); } public async Task DeleteWiProcess(int WiProcessId, LoginDTO loginDTO) { try { string sql = WiProcessQB.DELETE_WIPROCESS; var parameter = new { wiProcessId = WiProcessId }; 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); } } }