191 lines
8.7 KiB
C#
191 lines
8.7 KiB
C#
using PostFundManagement.Application.Abstractions.Services;
|
|
using PostFundManagement.Application.Extensions;
|
|
using PostFundManagement.Application.Services.Shared;
|
|
using PostFundManagement.Domain;
|
|
using PostFundManagement.Domain.Models;
|
|
using PostFundManagement.Domain.Models.Security;
|
|
using Claim = PostFundManagement.Domain.Models.Security.Claim;
|
|
|
|
namespace PostFundManagement.Application.Services.Users;
|
|
|
|
public sealed class UserCacheService(UserService service, IDistributedCache cache,
|
|
IFeatureManager featureManager) : ServiceCacheBase, IUserService
|
|
{
|
|
public async Task<Result> AddUserClaimAsync(AddUserClaimRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.AddUserClaimAsync(request, userId, cancellationToken);
|
|
|
|
public async Task<Result> AddUserRoleAsync(Guid targetUserId, Guid roleId, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.AddUserRoleAsync(targetUserId, roleId, userId, cancellationToken);
|
|
|
|
public async Task<Result> CreateUserAsync(string tenantIdHash, string idNumberHash, CreateUserRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.CreateUserAsync(tenantIdHash, idNumberHash, request, userId, cancellationToken);
|
|
|
|
public async Task<Result> DeleteUserClaimAsync(string claimIdHash, Guid targetUserId, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.DeleteUserClaimAsync(claimIdHash, targetUserId, userId, cancellationToken);
|
|
|
|
public async Task<Result> DeleteUserRoleAsync(Guid targetUserId, Guid roleId, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.DeleteUserRoleAsync(targetUserId, roleId, userId, cancellationToken);
|
|
|
|
public async Task<Result<Claim[]>> GetRoleClaimsAsync(Guid roleId, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Claim[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetRoleClaimsAsync(roleId, userId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getsystemroles-{userId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var claimResult = await service.GetRoleClaimsAsync(roleId, userId, cancellationToken);
|
|
|
|
if (claimResult.IsFailed) return claimResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, claimResult.Value.ToJson(),
|
|
cacheOptions, cancellationToken);
|
|
|
|
return claimResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Claim[]>());
|
|
}));
|
|
|
|
public async Task<Result<Role[]>> GetSystemRolesAsync(Guid userId, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Role[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetSystemRolesAsync(userId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getsystemroles-{userId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var rolesResult = await service.GetSystemRolesAsync(userId, cancellationToken);
|
|
|
|
if (rolesResult.IsFailed) return rolesResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, rolesResult.Value.ToJson(),
|
|
cacheOptions, cancellationToken);
|
|
|
|
return rolesResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Role[]>());
|
|
}));
|
|
|
|
public async Task<Result<User>> GetUserAsync(Guid userId, CancellationToken cancellationToken) =>
|
|
await Result.Try((Func<Task<Result<User>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetUserAsync(userId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getuser-{userId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var userResult = await service.GetUserAsync(userId, cancellationToken);
|
|
|
|
if (userResult.IsFailed) return userResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, userResult.Value.ToJson(), cacheOptions, cancellationToken);
|
|
|
|
return userResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<User>());
|
|
}));
|
|
|
|
public async Task<Result<Claim[]>> GetUserClaimsAsync(Guid targetUserId, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Claim[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetUserClaimsAsync(targetUserId, userId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getuserclaims-{userId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var claimsResult = await service.GetUserClaimsAsync(targetUserId, userId, cancellationToken);
|
|
|
|
if (claimsResult.IsFailed) return claimsResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, claimsResult.Value.ToJson(),
|
|
cacheOptions, cancellationToken);
|
|
|
|
return claimsResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Claim[]>());
|
|
}));
|
|
|
|
public async Task<Result<Role[]>> GetUserRolesAsync(Guid targetUserId, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<Role[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetUserRolesAsync(targetUserId, userId, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getuserroles-{userId}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var rolesResult = await service.GetUserRolesAsync(targetUserId, userId, cancellationToken);
|
|
|
|
if (rolesResult.IsFailed) return rolesResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, rolesResult.Value.ToJson(),
|
|
cacheOptions, cancellationToken);
|
|
|
|
return rolesResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<Role[]>());
|
|
}));
|
|
|
|
public async Task<Result<User[]>> GetUsersAsync(Pagination pagination, UserFilterRequest request, CancellationToken cancellationToken = default) =>
|
|
await Result.Try((Func<Task<Result<User[]>>>)(async () =>
|
|
{
|
|
if (!await featureManager.IsEnabledAsync(ServiceCachingFeature))
|
|
return await service.GetUsersAsync(pagination, request, cancellationToken);
|
|
|
|
var cacheKey = HashService.ToMd5Hash($"getusers-{pagination.Page}-{pagination.PageSize}-" +
|
|
$"{request.Email}-{request.Status}-{request.SortBy}-{request.SortDescending}").Value;
|
|
|
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
|
|
|
if (string.IsNullOrWhiteSpace(cached))
|
|
{
|
|
var usersResult = await service.GetUsersAsync(pagination, request, cancellationToken);
|
|
|
|
if (usersResult.IsFailed) return usersResult;
|
|
|
|
await cache.SetStringAsync(cacheKey, usersResult.Value.ToJson(),
|
|
cacheOptions, cancellationToken);
|
|
|
|
return usersResult;
|
|
}
|
|
|
|
return Result.Ok(cached.FromJson<User[]>());
|
|
}));
|
|
|
|
public async Task<Result> LogoutUserAsync(Guid targetUserId, Guid userId, CancellationToken cancellationToken = default) =>
|
|
await service.LogoutUserAsync(targetUserId, userId, cancellationToken);
|
|
|
|
public async Task<Result> SetUserLockoutAsync(Guid targetUserId, Guid userId, bool disable, CancellationToken cancellationToken = default) =>
|
|
await service.SetUserLockoutAsync(targetUserId, userId, disable, cancellationToken);
|
|
|
|
public async Task<Result> SyncUserAsync(Guid userId, string email, string role, CancellationToken cancellationToken = default) =>
|
|
await service.SyncUserAsync(userId, email, role, cancellationToken);
|
|
|
|
public async Task<Result> UpdateUserStatusAsync(Guid userId, ActivityStatus status, CancellationToken cancellationToken = default) =>
|
|
await service.UpdateUserStatusAsync(userId, status, cancellationToken);
|
|
}
|