using AmpDAL.CustomCode.ApiArtifact; using AMPDAL.DTO.ApiArtifact; 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.ApiArtifact; public class ApiArtifactBLL( IApiArtifactDAL _dal, IValidation _validation, AutoNumber _autoNumber, KeyInvalidate _keyInvalidate, EventLogPublish _eventLog, ILogger _logger ) : IApiArtifactBLL { public async Task GetArtifactAsync(long apiArtifactId, LoginDTO login, CancellationToken ct) => await _dal.GetArtifactAsync(apiArtifactId, login, ct).ConfigureAwait(false); public async Task> GetArtifactSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetArtifactSelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetArtifactList(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { if (firstNumber == -1 && maxResult == -1) return await _dal.GetArtifactListAsync(criteriaDTO, login, ct).ConfigureAwait(false); return await _dal.GetPagedArtifactList(firstNumber, maxResult, login, ct).ConfigureAwait(false); } public async Task SaveArtifactAsync(ApiArtifactSaveDTO dto, LoginDTO login, CancellationToken ct) { if (dto == null) throw new ArgumentNullException(nameof(dto)); if (dto.ApiId == 0) throw new ArgumentException("ApiId must be greater than zero.", nameof(dto.ApiId)); if (string.IsNullOrWhiteSpace(dto.Title)) throw new ArgumentException("Title cannot be empty.", nameof(dto.Title)); if (string.IsNullOrWhiteSpace(dto.Location)) throw new ArgumentException("Location cannot be empty.", nameof(dto.Location)); bool isNew = dto.ApiArtifactId == 0; long artifactId; var now = DateTime.UtcNow; dto.TenantId = login.ClientId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APIARTIFACT", login); dto.ApiArtifactId = autoNumberDTO.StartNumber; dto.CreatedById = login.UserId; dto.CreatedOn = now; dto.ModifiedById = login.UserId; dto.ModifiedOn = now; artifactId = await _dal.SaveArtifactAsync(dto, login, ct).ConfigureAwait(false); } else { dto.ModifiedById = login.UserId; dto.ModifiedOn = now; artifactId = await _dal.UpdateArtifactAsync(dto, login, ct).ConfigureAwait(false); } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPARTIFACT, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Artifact {ArtifactId}", artifactId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP Artifact Added" : "AMP Artifact Updated", new { dto.Title, dto.ApiId, ArtifactId = artifactId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Artifact {ArtifactId}", artifactId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {artifactId}" : $"{SuccessResponse.UpdateSuccessMessage} {artifactId}"; } public async Task DeleteById(long apiArtifactId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(apiArtifactId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }