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.WiStepContent { public class WiStepContentDAL : IWiStepContentDAL { private readonly IQueryExecutor _qe; private readonly IValidation _Validation; public WiStepContentDAL(IQueryExecutor qe, IValidation validation) { _qe = qe; _Validation = validation; } public async Task GetById(int WiStepContentId, LoginDTO login, CancellationToken ct) { var result = await _qe.QuerySingleAsync(login, WiStepContentQB.GET_BY_ID, new { wiStepContentId = WiStepContentId}); return JsonConvert.SerializeObject(result); } public async Task Save(WiStepContentDTO dto, bool isNew, LoginDTO login, CancellationToken ct) { try { string sql = isNew ? WiStepContentQB.INSERT : WiStepContentQB.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.WiStepContentId; } catch (Exception ex) { string error = await _Validation.HandleException(ex, isNew ? ErrorResponse.SaveErrorMessage : ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task GetSelectListWiStepContent(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, WiStepContentQB.GET_SELECTLIST_WISTEPCONTENT); return JsonConvert.SerializeObject(result); } public async Task DeleteWiStepContent(int WiStepContentId, LoginDTO loginDTO) { try { string sql = WiStepContentQB.DELETE_WISTEPCONTENT; var parameter = new { wiStepContentId = WiStepContentId }; 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); } } } }