161 lines
7.2 KiB
C#
161 lines
7.2 KiB
C#
using PostFundManagement.Application.Abstractions.Services;
|
|
using PostFundManagement.Application.Extensions;
|
|
using PostFundManagement.Application.Services.Shared;
|
|
using PostFundManagement.Domain;
|
|
using PostFundManagement.Domain.Models;
|
|
|
|
namespace PostFundManagement.Application.Services.Communications;
|
|
|
|
public sealed class CommunicationsCacheService(CommunicationsService service, IDistributedCache cache,
|
|
IFeatureManager featureManager) : ServiceCacheBase, ICommunicationsService
|
|
{
|
|
public async Task<Result> CreateNotificationAsync(CreateNotificationRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.CreateNotificationAsync(request, userId, cancellationToken);
|
|
|
|
public async Task<Result<ApplicantData[]>> GetApplicantDataAsync(Guid applicantUserId, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<ApplicantData[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetApplicantDataAsync(applicantUserId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getapplicantdata-{applicantUserId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetApplicantDataAsync(applicantUserId, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
}
|
|
|
|
return Result.Ok(cached!.FromJson<ApplicantData[]>());
|
|
}));
|
|
|
|
public async Task<Result<Invite>> GetApplicantInviteAsync(Guid applicantUserId, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Invite>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetApplicantInviteAsync(applicantUserId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getapplicantinvite-{applicantUserId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetApplicantInviteAsync(applicantUserId, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
}
|
|
|
|
return Result.Ok(cached!.FromJson<Invite>());
|
|
}));
|
|
|
|
public async Task<Result<Invite[]>> GetApplicantInvitesAsync(Guid applicantUserId, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Invite[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetApplicantInvitesAsync(applicantUserId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getapplicantinvites-{applicantUserId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetApplicantInvitesAsync(applicantUserId, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return result.Value;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Invite[]>());
|
|
}));
|
|
|
|
public async Task<Result<Invite[]>> GetInvitesAsync(Pagination pagination, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Invite[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetInvitesAsync(pagination, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getinvites-{pagination.Page}-{pagination.PageSize}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetInvitesAsync(pagination, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return Result.Ok(result.Value);
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Invite[]>());
|
|
}));
|
|
|
|
public async Task<Result<Notification>> GetNotificationAsync(string notificationIdHash, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Notification>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetNotificationAsync(notificationIdHash, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getnotification-{notificationIdHash}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetNotificationAsync(notificationIdHash, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return Result.Ok(result.Value);
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Notification>());
|
|
}));
|
|
|
|
public async Task<Result<Notification[]>> GetNotificationsAsync(string organisationIdHash, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Notification[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetNotificationsAsync(organisationIdHash, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getnotifications-{organisationIdHash}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var result = await service.GetNotificationsAsync(organisationIdHash, cancellationToken);
|
|
|
|
if (result.IsFailed) return result;
|
|
|
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return Result.Ok(result.Value);
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Notification[]>());
|
|
}));
|
|
|
|
public async Task<Result<Invite>> SendInviteAsync(InviteRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.SendInviteAsync(request, userId, cancellationToken);
|
|
|
|
public async Task<Result> UpdateNotificationAsync(UpdateNotificationRequest request, CancellationToken cancellationToken = default) =>
|
|
await service.UpdateNotificationAsync(request, cancellationToken);
|
|
}
|