97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using AutoMapper;
|
|
using xModels.Dtos;
|
|
using xCommons.Extensions;
|
|
using xIdentityService.Interfaces;
|
|
using xIdentityService.Extensions;
|
|
|
|
namespace xAiApi.AI.Mppings
|
|
{
|
|
/// <summary>
|
|
/// an AutoMapper Value Resolver for Mapping TEntity to TDto
|
|
/// When TEntity Has OwnerId Property and Owner ...
|
|
/// </summary>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
/// <typeparam name="TDto"></typeparam>
|
|
public abstract class XBaseOwnerMappingResolver<TEntity, TDto> : IValueResolver<TEntity, TDto, XPersonDto>
|
|
where TDto : class
|
|
where TEntity : class
|
|
{
|
|
//
|
|
private string destPropertyName = "Owner";
|
|
private string srcPropertyName = "OwnerId";
|
|
private readonly IXIdentityProvider identityProvider;
|
|
|
|
public XBaseOwnerMappingResolver(
|
|
IXIdentityProvider identityProvider
|
|
)
|
|
{
|
|
this.identityProvider = identityProvider;
|
|
}
|
|
|
|
public XPersonDto Resolve(
|
|
TEntity source,
|
|
TDto destination,
|
|
XPersonDto destMember,
|
|
ResolutionContext context
|
|
)
|
|
{
|
|
//
|
|
XPersonDto result = null;
|
|
|
|
//
|
|
// Check Source is not Null ...
|
|
if (source.IsNull())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check OwnerId Exists ...
|
|
var isOwnerExists = source.HasProperty(srcPropertyName);
|
|
if (!isOwnerExists)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Retrieve OwnerId from Source ...
|
|
var ownerId = source.GetProperty<TEntity, string>(srcPropertyName);
|
|
isOwnerExists = !ownerId.IsNullOrEmpty();
|
|
if (!isOwnerExists)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Retrieve Private Trusted Device ...
|
|
var device = identityProvider.GetDevice();
|
|
|
|
//
|
|
// Retrieve Specified OwnerId's UserInfo ...
|
|
var userInfo = identityProvider.GetUserInfo(
|
|
device: device,
|
|
userSelectByParam: ownerId
|
|
).RunTask();
|
|
|
|
//
|
|
// Check UserInfo Exists ...
|
|
if (!userInfo.IsNullOrDefault())
|
|
{
|
|
//
|
|
// Converts UserInfo to XPersonDto ...
|
|
result = userInfo.ToXPersonDto();
|
|
}
|
|
|
|
//
|
|
// Setting Dest Member ...
|
|
destMember = result;
|
|
|
|
//
|
|
// Setting Destination Property Value ...
|
|
destination.SetProperty(destPropertyName, result);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |