using AmpDAL.CustomCode.ApiOperation; using AmpDAL.DTO.ApiOperation; 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 Microsoft.Extensions.Logging; namespace AMPBLL.ApiOperation; public class ApiOperationBLL( IApiOperationDAL _dal, AutoNumber _autoNumber, KeyInvalidate _keyInvalidate, EventLogPublish _eventLog, ILogger _logger ) : IApiOperationBLL { public async Task GetOperationAsync(long apiOperationId, LoginDTO login, CancellationToken ct) => await _dal.GetOperationAsync(apiOperationId, login, ct).ConfigureAwait(false); public async Task> GetOperationSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetOperationSelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetOperationList(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { if (firstNumber == -1 && maxResult == -1) return await _dal.GetOperationListAsync(criteriaDTO, login, ct).ConfigureAwait(false); return await _dal.GetPagedOperationList(firstNumber, maxResult, login, ct).ConfigureAwait(false); } public async Task SaveOperationAsync(ApiOperationSaveDTO dto, LoginDTO login, CancellationToken ct) { if (dto.ApiId == 0) throw new ArgumentException("ApiId must be greater than zero.", nameof(dto.ApiId)); if (dto.ApiInterfaceId == 0) throw new ArgumentException("ApiInterfaceId must be greater than zero.", nameof(dto.ApiInterfaceId)); if (string.IsNullOrWhiteSpace(dto.OperationKey)) throw new ArgumentException("OperationKey cannot be empty.", nameof(dto.OperationKey)); if (string.IsNullOrWhiteSpace(dto.HttpMethod)) throw new ArgumentException("HttpMethod cannot be empty.", nameof(dto.HttpMethod)); bool isNew = dto.ApiOperationId == 0; long apiOperationId; var now = DateTime.UtcNow; dto.TenantId = login.ClientId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APIOPERATION", login); dto.ApiOperationId = autoNumberDTO.StartNumber; dto.CreatedById = login.UserId; dto.CreatedOn = now; dto.ModifiedById = login.UserId; dto.ModifiedOn = now; apiOperationId = await _dal.SaveOperationAsync(dto, login, ct).ConfigureAwait(false); } else { dto.ModifiedById = login.UserId; dto.ModifiedOn = now; apiOperationId = await _dal.UpdateOperationAsync(dto, login, ct).ConfigureAwait(false); } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPOPERATION, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Operation {ApiOperationId}", apiOperationId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP API Operation Registered" : "AMP API Operation Updated", new { dto.OperationKey, dto.HttpMethod, dto.ApiId, dto.ApiInterfaceId, ApiOperationId = apiOperationId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Operation {ApiOperationId}", apiOperationId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {apiOperationId}" : $"{SuccessResponse.UpdateSuccessMessage} {apiOperationId}"; } public async Task DeleteById(int apiOperationId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(apiOperationId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }