using AmpDAL.CustomCode.ApiParameter; using AmpDAL.DTO.ApiParameter; 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.ApiParameter; public class ApiParameterBLL( IApiParameterDAL _dal, IValidation _validation, AutoNumber _autoNumber, KeyInvalidate _keyInvalidate, EventLogPublish _eventLog, ILogger _logger ) : IApiParameterBLL { public async Task GetParameterAsync(long parameterId, LoginDTO login, CancellationToken ct) => await _dal.GetParameterAsync(parameterId, login, ct).ConfigureAwait(false); public async Task> GetParameterSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetParameterSelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetParameterList(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { if (firstNumber == -1 && maxResult == -1) return await _dal.GetParameterListAsync(criteriaDTO, login, ct).ConfigureAwait(false); return await _dal.GetPagedParameterList(firstNumber, maxResult, login, ct).ConfigureAwait(false); } public async Task SaveParameterAsync(ApiParameterSaveDTO dto, LoginDTO login, CancellationToken ct) { if (dto.ApiId == 0) throw new ArgumentException("ApiId must be greater than zero.", nameof(dto.ApiId)); if (dto.ApiOperationId == 0) throw new ArgumentException("ApiOperationId must be greater than zero.", nameof(dto.ApiOperationId)); if (string.IsNullOrWhiteSpace(dto.Name)) throw new ArgumentException("Parameter Name cannot be empty.", nameof(dto.Name)); bool isNew = dto.ParameterId == 0; long parameterId; var now = DateTime.UtcNow; dto.TenantId = login.ClientId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APIPARAMETER", login); dto.ParameterId = autoNumberDTO.StartNumber; dto.CreatedById = login.UserId; dto.CreatedOn = now; dto.ModifiedById = login.UserId; dto.ModifiedOn = now; parameterId = await _dal.SaveParameterAsync(dto, login, ct).ConfigureAwait(false); } else { dto.ModifiedById = login.UserId; dto.ModifiedOn = now; parameterId = await _dal.UpdateParameterAsync(dto, login, ct).ConfigureAwait(false); } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPPARAMETER, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Parameter {ParameterId}", parameterId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP API Parameter Registered" : "AMP API Parameter Updated", new { dto.Name, dto.ApiId, dto.ApiOperationId, ParameterId = parameterId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Parameter {ParameterId}", parameterId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {parameterId}" : $"{SuccessResponse.UpdateSuccessMessage} {parameterId}"; } public async Task DeleteById(int parameterId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(parameterId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }