using FrameworkDAL.CustomCode.ECMRole; using FrameworkDAL.DTO.ECMRole; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.EventLogPublish; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace FrameworkBLL.ECMRole { public class ECMRoleBLL : IECMRoleBLL { private readonly IECMRoleDAL _ECMRoleDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly EventLogPublish _EventLogPublish; private readonly IQueryExecutor _QueryExecutor; public ECMRoleBLL(IECMRoleDAL ECMRoleDAL, AutoNumber AutoNumber, IValidation Validation, EventLogPublish eventLogPublish, IQueryExecutor queryExecutor) { _ECMRoleDAL = ECMRoleDAL; _AutoNumber = AutoNumber; _Validation = Validation; _EventLogPublish = eventLogPublish; _QueryExecutor = queryExecutor; } public async Task GetECMRole(int ECMRoleId, LoginDTO LoginDTO) { try { return await _ECMRoleDAL.GetECMRole(ECMRoleId, LoginDTO); } catch (Exception ex) { throw new Exception("Error while fetching ECM Role", ex); } } public async Task SaveECMRole(ECMRoleDTO ECMRoleDTO, LoginDTO LoginDTO) { await _Validation.NotEmpty(ECMRoleDTO.ECMRoleCode, nameof(ECMRoleDTO.ECMRoleCode)); await _Validation.NotEmpty(ECMRoleDTO.ECMRoleName, nameof(ECMRoleDTO.ECMRoleName)); ECMRoleDTO.ECMRoleModifiedById = LoginDTO.UserId; ECMRoleDTO.ECMRoleModifiedOn = DateTime.UtcNow; bool isNew = ECMRoleDTO.ECMRoleId == 0; if (isNew) { ECMRoleDTO.ECMRoleCreatedById = LoginDTO.UserId; ECMRoleDTO.ECMRoleCreatedOn = DateTime.UtcNow; } var trans = await _QueryExecutor.BeginTransactionAsync(LoginDTO); try { if (isNew) await _ECMRoleDAL.SaveECMRole(ECMRoleDTO, LoginDTO, trans); else await _ECMRoleDAL.UpdateECMRole(ECMRoleDTO, LoginDTO, trans); await _QueryExecutor.CommitAsync(trans); } catch { await _QueryExecutor.RollbackAsync(trans); throw; } // Publish after commit — Dapr is fire-and-forget, must not roll back the DB save await _EventLogPublish.PublishEventLogAsync( isNew ? $"ECMRole Created: Code={ECMRoleDTO.ECMRoleCode}, Name={ECMRoleDTO.ECMRoleName}" : $"ECMRole Updated: Code={ECMRoleDTO.ECMRoleCode}, Name={ECMRoleDTO.ECMRoleName}", ECMRoleDTO, isNew ? EventTypeConstant.SAVEECMROLEEVENTTYPEID : EventTypeConstant.UPDATEECMROLEEVENTTYPEID, ECMRoleDTO.ECMRoleId, LoginDTO, "eventlog-topic" ); return isNew ? "Details have been saved successfully" : "Details have been updated successfully"; } public async Task> DeleteECMRole(int ECMRoleId, LoginDTO LoginDTO) { try { return await _ECMRoleDAL.DeleteECMRole(ECMRoleId, LoginDTO); } catch (Exception ex) { throw new Exception("Error while deleting ECM Role", ex); } } public async Task GetSelectListECMRole(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _ECMRoleDAL.GetSelectListECMRole(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception ex) { throw new Exception("Error while fetching ECMRole select list.", ex); } } } }