backup Mapping Actions and remove them and try to use Enricher Service for AiConversation ...

This commit is contained in:
2026-08-08 00:30:31 +03:30
parent 5578c6cb2e
commit 17bbe401ae
9 changed files with 700 additions and 164 deletions
@@ -1,23 +1,485 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore.Query;
using xAiModels.Interfaces.Dtos;
using xAiModels.Interfaces.Enrichers;
using xAiModels.Interfaces.Entities;
using xAiModels.Models.Dtos;
using xAiModels.Models.Entities;
using xCommons.Extensions;
using xDataService.Providers;
using xModels.Dtos;
namespace xAiModels.Providers.Dtos
{
public class XAiConversationRepositoryService : XBaseRepositoryService<XAiConversation, XAiConversationDto, Guid>, IXAiConversationRepositoryService
{
private readonly IXAiConversationEnricher enricher;
public XAiConversationRepositoryService(
IXAiConversationEnricher enricher,
IXAiConversationRepository repository,
IEnumerable<Profile> mapperProfiles = null
) : base(
repository,
mapperProfiles
)
{ }
{
this.enricher = enricher;
}
//
#region Actions ...
//
#region Add ...
/// <summary>
/// add a new Dto ...
/// </summary>
/// <param name="item"></param>
/// <param name="saveChanges"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XAiConversationDto> AddAsync(
XAiConversationDto item,
bool saveChanges = true,
CancellationToken cancellationToken = default
)
{
//
var result = await base.AddAsync(
item: item,
saveChanges: saveChanges,
cancellationToken: cancellationToken
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichAsync(
source: result,
destination: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
/// <summary>
/// add or update a Dto (add if not exists/update if exists) ...
/// </summary>
/// <param name="item"></param>
/// <param name="saveChanges"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XAiConversationDto> AddOrUpdateAsync(
XAiConversationDto item,
bool saveChanges = true,
CancellationToken cancellationToken = default
)
{
//
var result = await base.AddAsync(
item: item,
saveChanges: saveChanges,
cancellationToken: cancellationToken
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichAsync(
source: result,
destination: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
#endregion
//
#region Update ...
/// <summary>
/// Update an Dto values ...
/// </summary>
/// <param name="id"></param>
/// <param name="item"></param>
/// <param name="saveChanges"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XAiConversationDto> UpdateAsync(
Guid id,
XAiConversationDto item,
bool saveChanges = true,
CancellationToken cancellationToken = default
)
{
//
var result = await base.UpdateAsync(
id: id,
item: item,
saveChanges: saveChanges,
cancellationToken: cancellationToken
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichAsync(
source: result,
destination: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
#endregion
//
#region Remove ...
/// <summary>
/// remove an Dto by it's Id ...
/// </summary>
/// <param name="id"></param>
/// <param name="softDelete"></param>
/// <param name="saveChanges"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XAiConversationDto> RemoveAsync(
Guid id,
bool softDelete = true,
bool saveChanges = true,
CancellationToken cancellationToken = default
)
{
//
var result = await base.RemoveAsync(
id: id,
softDelete: softDelete,
saveChanges: saveChanges,
cancellationToken: cancellationToken
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichAsync(
source: result,
destination: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
/// <summary>
/// remove an Dto ...
/// </summary>
/// <param name="item"></param>
/// <param name="softDelete"></param>
/// <param name="saveChanges"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XAiConversationDto> RemoveAsync(
XAiConversationDto item,
bool softDelete = true,
bool saveChanges = true,
CancellationToken cancellationToken = default
)
{
//
var result = await base.RemoveAsync(
item: item,
softDelete: softDelete,
saveChanges: saveChanges,
cancellationToken: cancellationToken
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichAsync(
source: result,
destination: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
#endregion
//
#region Retrieve ...
/// <summary>
/// retrieve an Dto by it's Id ...
/// </summary>
/// <param name="id"></param>
/// <param name="ignoreSoftDeleteds"></param>
/// <param name="includeBuilder"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XAiConversationDto> GetAsync(
Guid id,
bool ignoreSoftDeleteds = true,
Func<IQueryable<XAiConversation>, IIncludableQueryable<XAiConversation, object>> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await base.GetAsync(
id: id,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken,
ignoreSoftDeleteds: ignoreSoftDeleteds
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichAsync(
source: result,
destination: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
/// <summary>
/// retrieve all exists Dtos ...
/// </summary>
/// <param name="ignoreSoftDeleteds"></param>
/// <param name="orderBuilder"></param>
/// <param name="includeBuilder"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<IEnumerable<XAiConversationDto>> GetAllAsync(
bool ignoreSoftDeleteds = true,
Func<IQueryable<XAiConversation>, IOrderedQueryable<XAiConversation>> orderBuilder = null,
Func<IQueryable<XAiConversation>, IIncludableQueryable<XAiConversation, object>> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await base.GetAllAsync(
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken,
ignoreSoftDeleteds: ignoreSoftDeleteds
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichRangeAsync(
sources: result,
destinations: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
/// <summary>
/// retrieve all exists Dtos
/// as Async Enumerable ...
/// </summary>
/// <param name="ignoreSoftDeleteds"></param>
/// <param name="orderBuilder"></param>
/// <param name="includeBuilder"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async IAsyncEnumerable<XAiConversationDto> GetAllAsAsyncEnumerable(
bool ignoreSoftDeleteds = true,
Func<IQueryable<XAiConversation>, IOrderedQueryable<XAiConversation>> orderBuilder = null,
Func<IQueryable<XAiConversation>, IIncludableQueryable<XAiConversation, object>> includeBuilder = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
//
var enumerable = base.GetAllAsAsyncEnumerable(
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
ignoreSoftDeleteds: ignoreSoftDeleteds
);
await foreach (var dto in enumerable)
{
//
if (cancellationToken.IsCancellationRequested)
{
yield break;
}
//
await enricher.EnrichAsync(
source: dto,
destination: dto,
cancellationToken: cancellationToken
);
yield return dto;
}
}
/// <summary>
/// find an Dto by providing a Conditional Expression ...
/// </summary>
/// <param name="predicate"></param>
/// <param name="ignoreSoftDeleteds"></param>
/// <param name="includeBuilder"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XAiConversationDto> FindOneAsync(
Expression<Func<XAiConversation, bool>> predicate,
bool ignoreSoftDeleteds = true,
Func<IQueryable<XAiConversation>, IIncludableQueryable<XAiConversation, object>> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await base.FindOneAsync(
predicate: predicate,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken,
ignoreSoftDeleteds: ignoreSoftDeleteds
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichAsync(
source: result,
destination: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
/// <summary>
/// find a collection of Dtos by proving a Conditional Expression ...
/// </summary>
/// <param name="predicate"></param>
/// <param name="ignoreSoftDeleteds"></param>
/// <param name="orderBuilder"></param>
/// <param name="includeBuilder"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<IEnumerable<XAiConversationDto>> FindManyAsync(
Expression<Func<XAiConversation, bool>> predicate,
bool ignoreSoftDeleteds = true,
Func<IQueryable<XAiConversation>, IOrderedQueryable<XAiConversation>> orderBuilder = null,
Func<IQueryable<XAiConversation>, IIncludableQueryable<XAiConversation, object>> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await base.FindManyAsync(
predicate: predicate,
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken,
ignoreSoftDeleteds: ignoreSoftDeleteds
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichRangeAsync(
sources: result,
destinations: result,
cancellationToken: cancellationToken
);
}
//
return result;
}
/// <summary>
/// retrieve Dtos based on XQuery Pagination structure ...
/// </summary>
/// <param name="query"></param>
/// <param name="ignoreSoftDeleteds"></param>
/// <param name="predicate"></param>
/// <param name="orderBuilder"></param>
/// <param name="includeBuilder"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<XQueryResult<XAiConversationDto>> QueryAsync(
XQuery query,
bool ignoreSoftDeleteds = true,
Expression<Func<XAiConversation, bool>> predicate = null,
Func<IQueryable<XAiConversation>, IOrderedQueryable<XAiConversation>> orderBuilder = null,
Func<IQueryable<XAiConversation>, IIncludableQueryable<XAiConversation, object>> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await base.QueryAsync(
query: query,
predicate: predicate,
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken,
ignoreSoftDeleteds: ignoreSoftDeleteds
);
//
// Do Enrichment if result Exists ...
if (!result.IsNullOrDefault())
{
//
await enricher.EnrichRangeAsync(
sources: result.Items,
destinations: result.Items,
cancellationToken: cancellationToken
);
}
//
return result;
}
#endregion
#endregion
}
}
@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using xAiModels.Interfaces;
using xAiModels.Interfaces.Dtos;
using xAiModels.Interfaces.Enrichers;
using xAiModels.Models.Dtos;
using xAiModels.Models.Entities;
using xCommons.Extensions;
using xIdentityService.Interfaces;
using xIdentityService.Extensions;
using xModels.Dtos;
using System.Linq;
namespace xAiModels.Providers.Enrichers
{
public class XAiConversationEnricher : IXAiConversationEnricher
{
private readonly IXIdentityProvider identityProvider;
private readonly IXAiProjectRepositoryService projectRepositoryService;
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
public XAiConversationEnricher(
IXIdentityProvider identityProvider,
IXAiProjectRepositoryService projectRepositoryService,
IXAiConversationTitleResourceProvider conversationTitleResourceProvider
)
{
this.identityProvider = identityProvider;
this.projectRepositoryService = projectRepositoryService;
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
}
/// <summary>
/// Converts a Signle Entity to Dto ...
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task EnrichAsync(
XAiConversationDto source,
XAiConversationDto destination,
CancellationToken cancellationToken = default
)
{
//
// Run independent async operations concurrently
var ownerTask = ResolveOwnerAsync(source.OwnerId, cancellationToken);
var projectTask = ResolveProjectAsync(source.ProjectId, cancellationToken);
var titleLocalesTask = ResolveTitleLocalesAsync(source.Title, cancellationToken);
//
// wait until All Tasks Resolved ...
await Task.WhenAll(ownerTask, titleLocalesTask, projectTask);
//
// Attached Resolved Info to Destination ...
destination.Owner = ownerTask.Result;
destination.TitleLocales = titleLocalesTask.Result;
var project = projectTask.Result;
if (!project.IsNullOrDefault())
{
destination.ProjectTitle = project.Title;
destination.ProjectDescription = project.Description;
destination.ProjectTitleLocales = project.TitleLocales;
destination.ProjectDescriptionLocales = project.DescriptionLocales;
}
}
/// <summary>
/// Converts a Collection of Entities to Dtos ...
/// </summary>
/// <param name="sources"></param>
/// <param name="destinations"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IEnumerable<XAiConversationDto>> EnrichRangeAsync(
IEnumerable<XAiConversationDto> sources,
IEnumerable<XAiConversationDto> destinations,
CancellationToken cancellationToken = default
)
{
//
// Make Tasks Ready ...
var tasks = sources.Zip(destinations, (src, dst) =>
EnrichAsync(src, dst, cancellationToken)
);
//
await Task.WhenAll(tasks);
//
return destinations;
}
//
#region Private ...
/// <summary>
/// Reading Owner User Info ...
/// </summary>
/// <param name="ownerId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<XPersonDto> ResolveOwnerAsync(
string ownerId,
CancellationToken cancellationToken = default
)
{
//
XPersonDto result = null;
//
// Validate ...
if (ownerId.IsNullOrEmpty())
{
return result;
}
//
// Try to Retrieve User Info ...
var device = identityProvider.GetDevice();
var userInfo = await identityProvider.GetUserInfo(
device: device,
userSelectByParam: ownerId
);
result = userInfo.ToXPersonDto();
//
return result;
}
/// <summary>
/// Reading Title Locales ...
/// </summary>
/// <param name="title"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<XLocaleResourceDto[]> ResolveTitleLocalesAsync(
string title,
CancellationToken cancellationToken = default
)
{
//
XLocaleResourceDto[] result = [];
if (title.IsNullOrEmpty())
{
return result;
}
//
var locales = await conversationTitleResourceProvider.GetLocales(
identifier: title,
cancellationToken: cancellationToken
);
result = [.. locales];
//
return result;
}
/// <summary>
/// Retrieve Project ...
/// </summary>
/// <param name="projectId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<XAiProjectDto> ResolveProjectAsync(
Guid projectId,
CancellationToken cancellationToken = default
)
{
//
XAiProjectDto result = null;
if (projectId.IsNull() || projectId.IsDefaultGuid())
{
return result;
}
//
result = await projectRepositoryService.GetAsync(
id: projectId,
includeBuilder: null,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
//
return result;
}
#endregion
}
}