using AmpDAL.CustomCode.ApiDomain; using AmpDAL.DTO.ApiDomain; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.EventLogPublish; using GB5Shared.GB5Constant; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using Microsoft.Extensions.Logging; namespace AMPBLL.ApiDomain; public class ApiDomainBLL( IApiDomainDAL _dal, IValidation _validation, KeyInvalidate _keyInvalidate, AutoNumber _autoNumber, EventLogPublish _eventLog, ILogger _logger ) : IApiDomainBLL { public async Task GetDomainAsync(long apiDomainId, LoginDTO login, CancellationToken ct) => await _dal.GetDomainAsync(apiDomainId, login.ClientId, login, ct).ConfigureAwait(false); public async Task> GetDomainSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetDomainSelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetDomainList(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { if (firstNumber == -1 && maxResult == -1) return await _dal.GetDomainListAsync(criteriaDTO, login, ct).ConfigureAwait(false); return await _dal.GetPagedDomainList(firstNumber, maxResult, login, ct).ConfigureAwait(false); } public async Task SaveDomainAsync(ApiDomainSaveDTO dto, LoginDTO login, CancellationToken ct) { if (dto == null) throw new ArgumentNullException(nameof(dto)); await _validation.NotEmpty(dto.DomainName, nameof(dto.DomainName)); dto.TenantId = login.ClientId; bool isNew = dto.ApiDomainId == 0; long apiDomainId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APIDOMAIN", login); dto.ApiDomainId = autoNumberDTO.StartNumber; apiDomainId = await _dal.SaveDomainAsync(dto, login, ct).ConfigureAwait(false); } else { apiDomainId = await _dal.UpdateDomainAsync(dto, login, ct).ConfigureAwait(false); } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPDOMAIN, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Domain {ApiDomainId}", apiDomainId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP Domain Created" : "AMP Domain Updated", new { dto.DomainName, ApiDomainId = apiDomainId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Domain {ApiDomainId}", apiDomainId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {apiDomainId}" : $"{SuccessResponse.UpdateSuccessMessage} {apiDomainId}"; } public async Task DeleteById(int apiDomainId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(apiDomainId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }