162 lines
5.2 KiB
C#
162 lines
5.2 KiB
C#
using System.Linq;
|
|
using xModels.Dtos;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using xContentService.Interfaces;
|
|
using xContentService.Models.Dtos;
|
|
using xIdentityService.Interfaces;
|
|
using xIdentityService.Extensions;
|
|
using xContentService.Models.Entities;
|
|
using xContentService.Interfaces.Enrichers;
|
|
|
|
namespace xContentService.Providers.Enrichers
|
|
{
|
|
public class XContentEnricher : IXContentEnricher
|
|
{
|
|
private readonly IXIdentityProvider identityProvider;
|
|
private readonly IXContentTitleResourceProvider titleResourceProvider;
|
|
private readonly IXContentDescriptionResourceProvider descriptionResourceProvider;
|
|
|
|
public XContentEnricher(
|
|
IXIdentityProvider identityProvider,
|
|
IXContentTitleResourceProvider titleResourceProvider,
|
|
IXContentDescriptionResourceProvider descriptionResourceProvider
|
|
)
|
|
{
|
|
this.identityProvider = identityProvider;
|
|
this.titleResourceProvider = titleResourceProvider;
|
|
this.descriptionResourceProvider = descriptionResourceProvider;
|
|
}
|
|
|
|
//
|
|
#region Enrichers ...
|
|
/// <summary>
|
|
/// Enrich Specified Item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XContentDto> Enrich(
|
|
XContent source,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Mapping Default ...
|
|
var result = source.MapConvert<XContentDto, XContent>();
|
|
result = await Enrich(
|
|
source: result,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XContentDto> Enrich(
|
|
XContentDto source,
|
|
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();
|
|
}
|
|
|
|
//
|
|
// Hanlde Content Title ...
|
|
string contentTitle = string.Empty;
|
|
XResourceDto titleResource = await titleResourceProvider.GetResource(
|
|
identifier: source.Id,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!titleResource.IsNullOrDefault())
|
|
{
|
|
contentTitle = titleResource.Resource;
|
|
}
|
|
|
|
//
|
|
// Hanlde Content Description ...
|
|
string contentDescription = string.Empty;
|
|
XResourceDto descriptionResource = await descriptionResourceProvider.GetResource(
|
|
identifier: source.Id,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!descriptionResource.IsNullOrDefault())
|
|
{
|
|
contentDescription = descriptionResource.Resource;
|
|
}
|
|
|
|
//
|
|
// Attach Extracted Items ...
|
|
var result = source.MapConvert();
|
|
result.Owner = owner;
|
|
result.Title = contentTitle;
|
|
result.TitleResource = titleResource;
|
|
result.Description = contentDescription;
|
|
result.DescriptionResource = descriptionResource;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Items ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XContentDto>> EnrichRange(
|
|
IEnumerable<XContent> sources,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var tasks = sources
|
|
.Select(x => Enrich(x, cancellationToken));
|
|
|
|
//
|
|
var result = await Task.WhenAll(tasks);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enrich Specified Items ...
|
|
/// </summary>
|
|
/// <param name="sources"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XContentDto>> EnrichRange(
|
|
IEnumerable<XContentDto> sources,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var tasks = sources
|
|
.Select(x => Enrich(x, cancellationToken));
|
|
|
|
//
|
|
var result = await Task.WhenAll(tasks);
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |