using AMPBLL.ApiVersion; using AmpDAL.CustomCode.ApiVersion; using AmpDAL.DTO.ApiVersion; 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.ApiVersion; public class ApiVersionBLL( IApiVersionDAL _dal, IValidation _validation, KeyInvalidate _keyInvalidate, AutoNumber _autoNumber, EventLogPublish _eventLog, ILogger _logger ) : IApiVersionBLL { public async Task GetVersionAsync(long apiVersionId, LoginDTO login, CancellationToken ct) => await _dal.GetVersionAsync(apiVersionId, login, ct).ConfigureAwait(false); public async Task> GetVersionSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _dal.GetVersionSelectListAsync(criteriaDTO, login, ct).ConfigureAwait(false); public async Task> GetVersionList(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) { if (firstNumber == -1 && maxResult == -1) return await _dal.GetVersionListAsync(criteriaDTO, login, ct).ConfigureAwait(false); return await _dal.GetPagedVersionList(firstNumber, maxResult, login, ct).ConfigureAwait(false); } public async Task SaveVersionAsync(ApiVersionSaveDTO dto, LoginDTO login, CancellationToken ct) { if (dto == null) throw new ArgumentNullException(nameof(dto)); bool isNew = dto.ApiVersionId == 0; int apiVersionId; dto.TenantId = login.ClientId; if (isNew) { var autoNumberDTO = await _autoNumber.GetAutoNumber(1, "APIVERSION", login); dto.ApiVersionId = autoNumberDTO.StartNumber; dto.CreatedById = login.UserId; dto.CreatedOn = DateTime.UtcNow; dto.ModifiedById = login.UserId; dto.ModifiedOn = DateTime.UtcNow; apiVersionId = (int)await _dal.SaveVersionAsync(dto, login, ct).ConfigureAwait(false); } else { dto.ModifiedById = login.UserId; dto.ModifiedOn = DateTime.UtcNow; await _dal.UpdateVersionAsync(dto, login, ct).ConfigureAwait(false); apiVersionId = dto.ApiVersionId; } var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPVERSION, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Version {ApiVersionId}", apiVersionId); } try { await _eventLog.PublishEventLogAsync( isNew ? "AMP Version Created" : "AMP Version Updated", new { dto.Version, dto.ApiId, ApiVersionId = apiVersionId }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Version {ApiVersionId}", apiVersionId); } return isNew ? $"{SuccessResponse.SaveSuccessMessage} {apiVersionId}" : $"{SuccessResponse.UpdateSuccessMessage} {apiVersionId}"; } public async Task UpdateVersionStatusAsync(long apiVersionId, int versionStatus, LoginDTO login, CancellationToken ct) { await _dal.UpdateVersionStatusAsync(apiVersionId, versionStatus, login.UserId, login, ct).ConfigureAwait(false); var cacheKey = new CacheKeyGeneration().KeyGeneration( login.ClientId.ToString(), Constant.EntityConstant.OBJECTAMPVERSION, Constant.CacheKeyLevel.CLIENT_LEVEL, login); try { await _keyInvalidate.InvalidateCache(cacheKey); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr cache invalidation failed for Version status {ApiVersionId}", apiVersionId); } try { await _eventLog.PublishEventLogAsync( "AMP Version Status Updated", new { ApiVersionId = apiVersionId, VersionStatus = versionStatus }, Constant.EventTypeConstant.SAVEDOCUMENTSET, 0, login).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "Dapr event publish failed for Version status {ApiVersionId}", apiVersionId); } return $"{SuccessResponse.UpdateSuccessMessage} {apiVersionId}"; } public async Task DeleteById(int apiVersionId, LoginDTO login, CancellationToken ct) { await _dal.DeleteById(apiVersionId, login, ct).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } }