131 lines
5.8 KiB
C#
131 lines
5.8 KiB
C#
using PostFundManagement.Application.Abstractions;
|
|
using PostFundManagement.Application.Abstractions.Services;
|
|
using PostFundManagement.Application.Extensions;
|
|
using PostFundManagement.Application.Services.Shared;
|
|
using PostFundManagement.Domain;
|
|
using PostFundManagement.Domain.Attributes;
|
|
using PostFundManagement.Domain.Models;
|
|
using static PostFundManagement.Domain.Extensions.Constants;
|
|
|
|
namespace PostFundManagement.Application.Services.Execution;
|
|
|
|
public sealed class ExecutionCacheService(ExecutionService service, IDistributedCache cache,
|
|
IFeatureManager featureManager) : ServiceCacheBase, IExecutionService
|
|
{
|
|
[Audit]
|
|
public async Task<Result> ApproveDisbursementAsync(string disbursementIdHash, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.ApproveDisbursementAsync(disbursementIdHash, userId, cancellationToken);
|
|
|
|
[Audit]
|
|
public async Task<Result> CreateDisbursementAsync(CreateDisbursementRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.CreateDisbursementAsync(request, userId, cancellationToken);
|
|
|
|
[Audit]
|
|
public async Task<Result> CreateRiskAsync(CreateRiskRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.CreateRiskAsync(request, userId, cancellationToken);
|
|
|
|
public async Task<Result<Disbursement>> GetDisbursementAsync(string disbursementIdHash, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Disbursement>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetDisbursementAsync(disbursementIdHash, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getdisbursement-{disbursementIdHash}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetDisbursementAsync(disbursementIdHash, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return result.Value;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Disbursement>());
|
|
}));
|
|
|
|
public async Task<Result<Disbursement[]>> GetDisbursementsAsync(Pagination pagination, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Disbursement[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetDisbursementsAsync(pagination, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getdisbursements-{pagination.Page}-{pagination.PageSize}-{pagination.Index}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetDisbursementsAsync(pagination, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return result.Value;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Disbursement[]>());
|
|
}));
|
|
|
|
public async Task<Result<Risk>> GetRiskAsync(string riskIdHash, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Risk>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetRiskAsync(riskIdHash, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getrisk-{riskIdHash}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetRiskAsync(riskIdHash, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return result.Value;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Risk>());
|
|
}));
|
|
|
|
public async Task<Result<Risk[]>> GetRisksAsync(Pagination pagination, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Risk[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetRisksAsync(pagination, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getrisks-{pagination.Page}-{pagination.PageSize}-{pagination.Index}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetRisksAsync(pagination, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return result.Value;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Risk[]>());
|
|
}));
|
|
|
|
[Audit]
|
|
public async Task<Result> UpdateDisbursementAsync(UpdateDisbursementRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.UpdateDisbursementAsync(request, userId, cancellationToken);
|
|
|
|
[Audit]
|
|
public async Task<Result> UpdateRiskAsync(UpdateRiskRequest request, CancellationToken cancellationToken = default) =>
|
|
await service.UpdateRiskAsync(request, cancellationToken);
|
|
}
|