using System; using AutoMapper; using System.Linq; using xModels.Dtos; using System.Threading; using xCommons.Extensions; using xDataService.Providers; using System.Threading.Tasks; using System.Collections.Generic; using xContentService.Interfaces; using xContentService.Models.Dtos; using xIdentityService.Interfaces; using xIdentityService.Extensions; using xContentService.Interfaces.Dtos; using xContentService.Models.Entities; using xContentService.Interfaces.Entities; namespace xContentService.Providers.Dtos { public class XContentRepositoryService : XBaseRepositoryService, IXContentRepositoryService { private readonly IXIdentityProvider identityProvider; private readonly IXContentTitleResourceProvider titleResourceProvider; private readonly IXContentDescriptionResourceProvider descriptionResourceProvider; public XContentRepositoryService( IXContentRepository repository, IXIdentityProvider identityProvider, IXContentTitleResourceProvider titleResourceProvider, IXContentDescriptionResourceProvider descriptionResourceProvider, IEnumerable mapperProfiles = null ) : base( repository, mapperProfiles ) { this.identityProvider = identityProvider; this.titleResourceProvider = titleResourceProvider; this.descriptionResourceProvider = descriptionResourceProvider; } // #region Enrichers ... /// /// Enrich Specified Item ... /// /// /// /// public async Task Enrich( XContent source, CancellationToken cancellationToken = default ) { // // Mapping Default ... var result = Mapper.Map(source); result = await Enrich( source: result, cancellationToken: cancellationToken ); // return result; } /// /// Enrich Specified Item ... /// /// /// /// public async Task 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 = Mapper.Map(source, options => { // options.Items[nameof(source.Owner)] = owner; options.Items[nameof(source.Title)] = contentTitle; options.Items[nameof(source.TitleResource)] = titleResource; options.Items[nameof(source.Description)] = contentDescription; options.Items[nameof(source.DescriptionResource)] = descriptionResource; }); // return result; } /// /// Enrich Specified Items ... /// /// /// /// public async Task> EnrichRange( IEnumerable sources, CancellationToken cancellationToken = default ) { // var tasks = sources .Select(x => Enrich(x, cancellationToken)); // var result = await Task.WhenAll(tasks); return result; } /// /// Enrich Specified Items ... /// /// /// /// public async Task> EnrichRange( IEnumerable sources, CancellationToken cancellationToken = default ) { // var tasks = sources .Select(x => Enrich(x, cancellationToken)); // var result = await Task.WhenAll(tasks); return result; } #endregion } }