using AmpDAL.CustomCode.ApiSchema; using AmpDAL.DTO.ApiSchema; 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.APISchema; public class ApiSchemaBLL( IApiSchemaDAL _dal, IValidation _validation, AutoNumber _autoNumber, KeyInvalidate _keyInvalidate, EventLogPublish _eventLog, ILogger _logger ) : IApiSchemaBLL { public async Task GetSchemaAsync(long apiSchemaId, long apiInterfaceId, LoginDTO login, CancellationToken ct) => await _dal.GetSchemaAsync(apiSchemaId, apiInterfaceId, login, ct).ConfigureAwait(false); public async Task> GetSchemaSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetSchemaSelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetSchemaList(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { if (firstNumber == -1 && maxResult == -1) return await _dal.GetSchemaListAsync(criteriaDTO, login, ct).ConfigureAwait(false); return await _dal.GetPagedSchemaList(firstNumber, maxResult, login, ct).ConfigureAwait(false); } public async Task SaveSchemaAsync(ApiSchemaSaveDTO 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.ApiName)) throw new ArgumentException("ApiName cannot be empty.", nameof(dto.ApiName)); bool isNew = dto.ApiSchemaId == 0; long apiSchemaId; var now = DateTime.UtcNow; dto.TenantId = login.ClientId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APISCHEMA", login); dto.ApiSchemaId = autoNumberDTO.StartNumber; dto.CreatedById = login.UserId; dto.CreatedOn = now; dto.ModifiedById = login.UserId; dto.ModifiedOn = now; apiSchemaId = await _dal.SaveSchemaAsync(dto, login, ct).ConfigureAwait(false); } else { dto.ModifiedById = login.UserId; dto.ModifiedOn = now; apiSchemaId = await _dal.UpdateSchemaAsync(dto, login, ct).ConfigureAwait(false); } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPSCHEMA, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Schema {ApiSchemaId}", apiSchemaId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP API Schema Registered" : "AMP API Schema Updated", new { dto.ApiName, dto.ApiId, dto.ApiInterfaceId, ApiSchemaId = apiSchemaId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Schema {ApiSchemaId}", apiSchemaId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {apiSchemaId}" : $"{SuccessResponse.UpdateSuccessMessage} {apiSchemaId}"; } public async Task DeleteById(int apiSchemaId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(apiSchemaId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }