This commit is contained in:
2026-04-27 21:43:06 +03:30
parent bb5dc64a7d
commit 2b5e09912f
14 changed files with 877 additions and 387 deletions
+43
View File
@@ -0,0 +1,43 @@
using AutoMapper;
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
namespace xAiApi.AI.Mppings
{
public class MappingProfiles : Profile
{
public MappingProfiles()
{
//
#region Entity to Dto ...
//
CreateMap<XAiProject, XAiProjectDto>()
.ForMember(
x => x.Owner,
opt => opt.MapFrom<XAiProjectOwnerMappingResolver>()
);
//
CreateMap<XAiMessage, XAiMessageDto>()
.ForMember(
x => x.Owner,
opt => opt.MapFrom<XAiMessageOwnerMappingResolver>()
);
//
CreateMap<XAiConversation, XAiConversationDto>()
.ForMember(
x => x.Owner,
opt => opt.MapFrom<XAiConversationOwnerMappingResolver>()
);
#endregion
//
#region Dto to Entity ...
CreateMap<XAiProjectDto, XAiProject>();
CreateMap<XAiMessageDto, XAiMessage>();
CreateMap<XAiConversationDto, XAiConversation>();
#endregion
}
}
}
@@ -0,0 +1,14 @@
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
using xIdentityService.Interfaces;
namespace xAiApi.AI.Mppings
{
public class XAiConversationOwnerMappingResolver : XBaseOwnerMappingResolver<XAiConversation, XAiConversationDto>
{
public XAiConversationOwnerMappingResolver(
IXIdentityProvider identityProvider
) : base(identityProvider)
{ }
}
}
@@ -0,0 +1,14 @@
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
using xIdentityService.Interfaces;
namespace xAiApi.AI.Mppings
{
public class XAiMessageOwnerMappingResolver : XBaseOwnerMappingResolver<XAiMessage, XAiMessageDto>
{
public XAiMessageOwnerMappingResolver(
IXIdentityProvider identityProvider
) : base(identityProvider)
{ }
}
}
@@ -0,0 +1,14 @@
using xAiApi.AI.Models.Entities;
using xAiModels.Models;
using xIdentityService.Interfaces;
namespace xAiApi.AI.Mppings
{
public class XAiProjectOwnerMappingResolver : XBaseOwnerMappingResolver<XAiProject, XAiProjectDto>
{
public XAiProjectOwnerMappingResolver(
IXIdentityProvider identityProvider
) : base(identityProvider)
{ }
}
}
+97
View File
@@ -0,0 +1,97 @@
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;
}
}
}