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.WiSkillRequirement { public class WiSkillRequirementDAL : IWiSkillRequirementDAL { private readonly IQueryExecutor _qe; private readonly IValidation _Validation; public WiSkillRequirementDAL(IQueryExecutor qe, IValidation validation) { _qe = qe; _Validation = validation; } public async Task GetById(int WiSkillRequirementId, LoginDTO login, CancellationToken ct) { var result = await _qe.QuerySingleAsync(login, WiSkillRequirementQB.GET_BY_ID, new { wiskillrequirementId = WiSkillRequirementId, TenantId = login.ClientId }); return JsonConvert.SerializeObject(result); } public async Task Save(WiSkillRequirementDTO dto, bool isNew, LoginDTO login, CancellationToken ct) { try { string sql = isNew ? WiSkillRequirementQB.SAVE_WISKILLREQUIREMENT : WiSkillRequirementQB.UPDATE_WISKILLREQUIREMENT; 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.WiSkillRequirementId; } catch (Exception ex) { string error = await _Validation.HandleException(ex, isNew ? ErrorResponse.SaveErrorMessage : ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task GetSelectListWiSkillRequirement(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, WiSkillRequirementQB.GET_SELECTLIST_WISKILLREQUIREMENT); return JsonConvert.SerializeObject(result); } public async Task DeleteWiSkillRequirement(int WiSkillRequirementId, LoginDTO loginDTO) { try { string sql = WiSkillRequirementQB.DELETE_WISKILLREQUIREMENT; var parameter = new { wiskillrequirementId = WiSkillRequirementId }; 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); } } } }