apply fix on Models Enrichments ...
This commit is contained in:
@@ -1,30 +1,31 @@
|
||||
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 Microsoft.EntityFrameworkCore.Query;
|
||||
using xAiModels.Configurations;
|
||||
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 xIdentityService.Interfaces;
|
||||
using xModels.Dtos;
|
||||
using xIdentityService.Extensions;
|
||||
using xAiModels.Interfaces;
|
||||
using System.Linq;
|
||||
|
||||
namespace xAiModels.Providers.Dtos
|
||||
{
|
||||
public class XAiMessageRepositoryService : XBaseRepositoryService<XAiMessage, XAiMessageDto, Guid>, IXAiMessageRepositoryService
|
||||
{
|
||||
private readonly IXAiMessageEnricher enricher;
|
||||
private readonly IXIdentityProvider identityProvider;
|
||||
private readonly IXAiConversationTitleResourceProvider conversationTitleResourceProvider;
|
||||
|
||||
public XAiMessageRepositoryService(
|
||||
IXAiMessageEnricher enricher,
|
||||
IXAiMessageRepository repository
|
||||
IXAiMessageRepository repository,
|
||||
IXIdentityProvider identityProvider,
|
||||
IXAiConversationTitleResourceProvider conversationTitleResourceProvider
|
||||
) : base(
|
||||
repository,
|
||||
mapperProfiles:
|
||||
@@ -33,455 +34,132 @@ namespace xAiModels.Providers.Dtos
|
||||
]
|
||||
)
|
||||
{
|
||||
this.enricher = enricher;
|
||||
this.identityProvider = identityProvider;
|
||||
this.conversationTitleResourceProvider = conversationTitleResourceProvider;
|
||||
}
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
//
|
||||
#region Add ...
|
||||
#region Enrichers ...
|
||||
/// <summary>
|
||||
/// add a new Dto ...
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="saveChanges"></param>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<XAiMessageDto> AddAsync(
|
||||
XAiMessageDto item,
|
||||
bool saveChanges = true,
|
||||
public virtual async Task<XAiMessageDto> Enrich(
|
||||
XAiMessage source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await base.AddAsync(
|
||||
item: item,
|
||||
saveChanges: saveChanges,
|
||||
// Mapping Default ...
|
||||
var result = Mapper.Map<XAiMessageDto>(source);
|
||||
result = await Enrich(
|
||||
source: result,
|
||||
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) ...
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="saveChanges"></param>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<XAiMessageDto> AddOrUpdateAsync(
|
||||
XAiMessageDto item,
|
||||
bool saveChanges = true,
|
||||
public virtual async Task<XAiMessageDto> Enrich(
|
||||
XAiMessageDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await base.AddAsync(
|
||||
item: item,
|
||||
saveChanges: saveChanges,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Do Enrichment if result Exists ...
|
||||
if (!result.IsNullOrDefault())
|
||||
// Handdle Owner ...
|
||||
XPersonDto owner = null;
|
||||
if (!source.OwnerId.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
await enricher.EnrichAsync(
|
||||
source: result,
|
||||
destination: result,
|
||||
cancellationToken: cancellationToken
|
||||
// Try to Retrieve User Info ...
|
||||
var device = identityProvider.GetDevice();
|
||||
var userInfo = await identityProvider.GetUserInfo(
|
||||
device: device,
|
||||
userSelectByParam: source.OwnerId
|
||||
);
|
||||
owner = userInfo.ToXPersonDto();
|
||||
}
|
||||
|
||||
//
|
||||
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<XAiMessageDto> UpdateAsync(
|
||||
Guid id,
|
||||
XAiMessageDto 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())
|
||||
// Hanlde Conversation Title ...
|
||||
string conversationTitle = string.Empty;
|
||||
XResourceDto conversationTitleResource = null;
|
||||
if (!source.ConversationId.IsDefaultGuid())
|
||||
{
|
||||
//
|
||||
await enricher.EnrichAsync(
|
||||
source: result,
|
||||
destination: result,
|
||||
conversationTitleResource = await conversationTitleResourceProvider.GetResource(
|
||||
owner: source.OwnerId,
|
||||
identifier: source.ConversationId,
|
||||
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<XAiMessageDto> 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<XAiMessageDto> RemoveAsync(
|
||||
XAiMessageDto 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<XAiMessageDto> GetAsync(
|
||||
Guid id,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Func<IQueryable<XAiMessage>, IIncludableQueryable<XAiMessage, 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<XAiMessageDto>> GetAllAsync(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Func<IQueryable<XAiMessage>, IOrderedQueryable<XAiMessage>> orderBuilder = null,
|
||||
Func<IQueryable<XAiMessage>, IIncludableQueryable<XAiMessage, 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<XAiMessageDto> GetAllAsAsyncEnumerable(
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Func<IQueryable<XAiMessage>, IOrderedQueryable<XAiMessage>> orderBuilder = null,
|
||||
Func<IQueryable<XAiMessage>, IIncludableQueryable<XAiMessage, 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)
|
||||
if (!conversationTitleResource.IsNullOrDefault())
|
||||
{
|
||||
yield break;
|
||||
conversationTitle = conversationTitleResource.Resource;
|
||||
}
|
||||
|
||||
//
|
||||
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<XAiMessageDto> FindOneAsync(
|
||||
Expression<Func<XAiMessage, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Func<IQueryable<XAiMessage>, IIncludableQueryable<XAiMessage, 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())
|
||||
// Attach Extracted Items ...
|
||||
var result = Mapper.Map<XAiMessageDto>(source, options =>
|
||||
{
|
||||
//
|
||||
await enricher.EnrichAsync(
|
||||
source: result,
|
||||
destination: result,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
options.Items[nameof(source.Owner)] = owner;
|
||||
options.Items[nameof(source.ConversationTitle)] = conversationTitle;
|
||||
options.Items[nameof(source.ConversationTitleResouce)] = conversationTitleResource;
|
||||
});
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// find a collection of Dtos by proving a Conditional Expression ...
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<IEnumerable<XAiMessageDto>> FindManyAsync(
|
||||
Expression<Func<XAiMessage, bool>> predicate,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Func<IQueryable<XAiMessage>, IOrderedQueryable<XAiMessage>> orderBuilder = null,
|
||||
Func<IQueryable<XAiMessage>, IIncludableQueryable<XAiMessage, object>> includeBuilder = null,
|
||||
public virtual async Task<IEnumerable<XAiMessageDto>> EnrichRange(
|
||||
IEnumerable<XAiMessage> sources,
|
||||
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
|
||||
);
|
||||
}
|
||||
var tasks = sources
|
||||
.Select(x => Enrich(x, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// retrieve Dtos based on XQuery Pagination structure ...
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="ignoreSoftDeleteds"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBuilder"></param>
|
||||
/// <param name="includeBuilder"></param>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<XQueryResult<XAiMessageDto>> QueryAsync(
|
||||
XQuery query,
|
||||
bool ignoreSoftDeleteds = true,
|
||||
Expression<Func<XAiMessage, bool>> predicate = null,
|
||||
Func<IQueryable<XAiMessage>, IOrderedQueryable<XAiMessage>> orderBuilder = null,
|
||||
Func<IQueryable<XAiMessage>, IIncludableQueryable<XAiMessage, object>> includeBuilder = null,
|
||||
public virtual async Task<IEnumerable<XAiMessageDto>> EnrichRange(
|
||||
IEnumerable<XAiMessageDto> sources,
|
||||
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
|
||||
);
|
||||
}
|
||||
var tasks = sources
|
||||
.Select(x => Enrich(x, cancellationToken));
|
||||
|
||||
//
|
||||
var result = await Task.WhenAll(tasks);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user