using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Login; using GB5Shared.EventLogPublish; using GB5Shared.Resource.Response; using GB5Shared.Telemetry; using GB5Shared.Validation; using Microsoft.Extensions.Logging; using OKRBLL.Interfaces; using OKRDAL.DTOs; using OKRDAL.Interfaces; using System.Text.Json; using static GB5Shared.GB5Constant.Constant; namespace OKRBLL.Implementations; public class OKRPerspectiveBLL : IOKRPerspectiveBLL { private readonly IOKRPerspectiveDAL _dal; private readonly IValidation _validation; private readonly KeyInvalidate _keyInvalidate; private readonly EventLogPublish _eventLog; private readonly ILogger _logger; public OKRPerspectiveBLL( IOKRPerspectiveDAL dal, IValidation validation, KeyInvalidate keyInvalidate, EventLogPublish eventLog, ILogger logger) { _dal = dal; _validation = validation; _keyInvalidate = keyInvalidate; _eventLog = eventLog; _logger = logger; } public async Task GetPerspectiveListAsync(LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("get-okrperspective-list", new { }); var list = await _dal.GetPerspectiveListAsync(login, ct).ConfigureAwait(false); return JsonSerializer.Serialize(list); } catch (Exception ex) { GB5Trace.MarkFailed("get-okrperspective-list-failed", ex); _logger.LogError(ex, "GetPerspectiveList failed"); throw; } } public async Task SavePerspectiveAsync(OKRPerspectiveDTO dto, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("validate-okrperspective", new { dto.PerspectiveId }); await _validation.NotEmpty(dto.Code, nameof(dto.Code)).ConfigureAwait(false); await _validation.NotEmpty(dto.Name, nameof(dto.Name)).ConfigureAwait(false); bool isNew = dto.PerspectiveId == 0; GB5Trace.Step("save-okrperspective", new { dto.PerspectiveId, isNew }); int perspectiveId = await _dal.SavePerspectiveAsync(dto, login, ct).ConfigureAwait(false); var cacheKey = new CacheKeyGeneration().KeyGeneration(perspectiveId, EntityConstant.OBJECTOKRCYCLE, CacheKeyLevel.CLIENT_LEVEL, login); await _keyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {perspectiveId}" : SuccessResponse.UpdateSuccess; } catch (Exception ex) { GB5Trace.MarkFailed("save-okrperspective-failed", ex); _logger.LogError(ex, "SavePerspective failed for Code {Code}", dto.Code); throw; } } }