207 lines
7.2 KiB
C#
207 lines
7.2 KiB
C#
using System;
|
|
using System.Linq;
|
|
using xModels.Dtos;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xAiModels.Interfaces;
|
|
using xAiModels.Models.Dtos;
|
|
using System.Threading.Tasks;
|
|
using xDataService.Providers;
|
|
using xAiModels.Configurations;
|
|
using xAiModels.Interfaces.Dtos;
|
|
using xAiModels.Models.Entities;
|
|
using System.Collections.Generic;
|
|
using xIdentityService.Interfaces;
|
|
using xIdentityService.Extensions;
|
|
using xAiModels.Interfaces.Entities;
|
|
|
|
namespace xAiModels.Providers.Dtos
|
|
{
|
|
public class XAiConversationRepositoryService : XBaseRepositoryService<XAiConversation, XAiConversationDto, Guid>, IXAiConversationRepositoryService
|
|
{
|
|
private readonly IXIdentityProvider identityProvider;
|
|
private readonly IXAiConversationRepository repository;
|
|
private readonly IXAiProjectTitleResourceProvider projectTitleResourceProvider;
|
|
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
|
private readonly IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider;
|
|
|
|
public XAiConversationRepositoryService(
|
|
IXIdentityProvider identityProvider,
|
|
IXAiConversationRepository repository,
|
|
IXAiProjectTitleResourceProvider projectTitleResourceProvider,
|
|
IXAiConversationTitleResourceProvider conversationTitleResourceProvider,
|
|
IXAiProjectDescriptionResourceProvider projectDescriptionResourceProvider
|
|
) : base(
|
|
repository,
|
|
mapperProfiles:
|
|
[
|
|
new XAiModelMappersProfile()
|
|
]
|
|
)
|
|
{
|
|
this.repository = repository;
|
|
this.identityProvider = identityProvider;
|
|
this.projectTitleResourceProvider = projectTitleResourceProvider;
|
|
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
|
this.projectDescriptionResourceProvider = projectDescriptionResourceProvider;
|
|
}
|
|
|
|
//
|
|
#region Enrichers ...
|
|
/// <summary>
|
|
/// Enrich Specified Item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XAiConversationDto> Enrich(
|
|
XAiConversation source,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Mapping Default ...
|
|
var result = Mapper.Map<XAiConversationDto>(source);
|
|
result = await Enrich(
|
|
source: result,
|
|
fillMessages: fillMessages,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<XAiConversationDto> Enrich(
|
|
XAiConversationDto source,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Handdle Owner ...
|
|
XPersonDto owner = null;
|
|
if (!source.OwnerId.IsNullOrEmpty())
|
|
{
|
|
//
|
|
// Try to Retrieve User Info ...
|
|
var device = identityProvider.GetDevice();
|
|
var userInfo = await identityProvider.GetUserInfo(
|
|
device: device,
|
|
userSelectByParam: source.OwnerId
|
|
);
|
|
owner = userInfo.ToXPersonDto();
|
|
}
|
|
|
|
//
|
|
// Handle Title Resource ...
|
|
XResourceDto titleResource = null;
|
|
if (!source.Title.IsNullOrEmpty())
|
|
{
|
|
//
|
|
titleResource = await conversationTitleResourceProvider.GetResource(
|
|
owner: source.OwnerId,
|
|
identifier: source.Id,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
// Handle Project Info ...
|
|
XResourceDto projectTitleResource = null;
|
|
XResourceDto projectDescriptionResource = null;
|
|
if (!source.ProjectId.IsDefaultGuid())
|
|
{
|
|
//
|
|
projectTitleResource = await projectTitleResourceProvider.GetResource(
|
|
owner: source.OwnerId,
|
|
identifier: source.ProjectId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
projectDescriptionResource = await projectDescriptionResourceProvider.GetResource(
|
|
owner: source.OwnerId,
|
|
identifier: source.ProjectId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
// Attach Extracted Items ...
|
|
var result = Mapper.Map<XAiConversationDto>(source, options =>
|
|
{
|
|
//
|
|
options.Items[nameof(source.Owner)] = owner;
|
|
options.Items[nameof(source.TitleResource)] = titleResource;
|
|
options.Items[nameof(source.ProjectTitleResource)] = projectTitleResource;
|
|
options.Items[nameof(source.ProjectDescriptionResource)] = projectDescriptionResource;
|
|
});
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Items ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XAiConversationDto>> EnrichRange(
|
|
IEnumerable<XAiConversation> sources,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var tasks = sources
|
|
.Select(x => Enrich(
|
|
source: x,
|
|
fillMessages: fillMessages,
|
|
cancellationToken: cancellationToken
|
|
));
|
|
|
|
//
|
|
var result = await Task.WhenAll(tasks);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Items ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="fillMessages"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IEnumerable<XAiConversationDto>> EnrichRange(
|
|
IEnumerable<XAiConversationDto> sources,
|
|
bool fillMessages = false,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var tasks = sources
|
|
.Select(x => Enrich(
|
|
source: x,
|
|
fillMessages: fillMessages,
|
|
cancellationToken: cancellationToken
|
|
));
|
|
|
|
//
|
|
var result = await Task.WhenAll(tasks);
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |