using AmpDAL.CustomCode.ApiInterface; using AmpDAL.DTO.ApiInterface; 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.ApiInterface; public class ApiInterfaceBLL( IApiInterfaceDAL _dal, IValidation _validation, AutoNumber _autoNumber, KeyInvalidate _keyInvalidate, EventLogPublish _eventLog, ILogger _logger ) : IApiInterfaceBLL { public async Task GetInterfaceAsync(long apiInterfaceId, LoginDTO login, CancellationToken ct) => await _dal.GetInterfaceAsync(apiInterfaceId, login.ClientId, login, ct).ConfigureAwait(false); public async Task> GetInterfaceSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetInterfaceSelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetInterfaceList(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { if (firstNumber == -1 && maxResult == -1) return await _dal.GetInterfaceListAsync(criteriaDTO, login, ct).ConfigureAwait(false); return await _dal.GetPagedInterfaceList(firstNumber, maxResult, criteriaDTO, login, ct).ConfigureAwait(false); } public async Task SaveInterfaceAsync(ApiInterfaceSaveDTO dto, LoginDTO login, CancellationToken ct) { if (dto.ApiId == 0) throw new ArgumentException("ApiId must be greater than zero.", nameof(dto.ApiId)); bool isNew = dto.ApiInterfaceId == 0; long apiInterfaceId; var now = DateTime.UtcNow; dto.TenantId = login.ClientId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APIINTERFACE", login); dto.ApiInterfaceId = autoNumberDTO.StartNumber; dto.CreatedOn = now; dto.ModifiedOn = now; apiInterfaceId = await _dal.SaveInterfaceAsync(dto, login, ct).ConfigureAwait(false); } else { dto.ModifiedOn = now; apiInterfaceId = await _dal.UpdateInterfaceAsync(dto, login, ct).ConfigureAwait(false); } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPINTERFACE, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Interface {ApiInterfaceId}", apiInterfaceId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP Interface Registered" : "AMP Interface Updated", new { dto.InterfaceType, dto.ApiId, ApiInterfaceId = apiInterfaceId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Interface {ApiInterfaceId}", apiInterfaceId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {apiInterfaceId}" : $"{SuccessResponse.UpdateSuccessMessage} {apiInterfaceId}"; } public async Task UpdateParseResultAsync(long apiInterfaceId, bool parsedSuccessfully, string? specHash, LoginDTO login, CancellationToken ct) { await _dal.UpdateParseResultAsync(apiInterfaceId, parsedSuccessfully, specHash, login, ct).ConfigureAwait(false); try { await _eventLog.PublishEventLogAsync( "AMP Interface Parse Result Updated", new { ApiInterfaceId = apiInterfaceId, ParsedSuccessfully = parsedSuccessfully }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Interface parse result {ApiInterfaceId}", apiInterfaceId); } return $"{SuccessResponse.UpdateSuccessMessage} {apiInterfaceId}"; } public async Task DeleteById(int apiInterfaceId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(apiInterfaceId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }