mirror of https://github.com/Squidex/squidex.git
24 changed files with 278 additions and 137 deletions
@ -0,0 +1,78 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Squidex.Domain.Apps.Core.Tags; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Assets |
|||
{ |
|||
public sealed class AssetEnricher : IAssetEnricher |
|||
{ |
|||
private readonly ITagService tagService; |
|||
|
|||
public AssetEnricher(ITagService tagService) |
|||
{ |
|||
Guard.NotNull(tagService, nameof(tagService)); |
|||
|
|||
this.tagService = tagService; |
|||
} |
|||
|
|||
public async Task<IEnrichedAssetEntity> EnrichAsync(IAssetEntity asset) |
|||
{ |
|||
Guard.NotNull(asset, nameof(asset)); |
|||
|
|||
var enriched = await EnrichAsync(Enumerable.Repeat(asset, 1)); |
|||
|
|||
return enriched[0]; |
|||
} |
|||
|
|||
public async Task<IReadOnlyList<IEnrichedAssetEntity>> EnrichAsync(IEnumerable<IAssetEntity> assets) |
|||
{ |
|||
Guard.NotNull(assets, nameof(assets)); |
|||
|
|||
var results = new List<IEnrichedAssetEntity>(); |
|||
|
|||
foreach (var group in assets.GroupBy(x => x.AppId.Id)) |
|||
{ |
|||
var tagsById = await CalculateTags(group); |
|||
|
|||
foreach (var asset in group) |
|||
{ |
|||
var result = SimpleMapper.Map(asset, new AssetEntity()); |
|||
|
|||
result.TagNames = new HashSet<string>(); |
|||
|
|||
if (asset.Tags != null) |
|||
{ |
|||
foreach (var id in asset.Tags) |
|||
{ |
|||
if (tagsById.TryGetValue(id, out var name)) |
|||
{ |
|||
result.TagNames.Add(name); |
|||
} |
|||
} |
|||
} |
|||
|
|||
results.Add(result); |
|||
} |
|||
} |
|||
|
|||
return results; |
|||
} |
|||
|
|||
private async Task<Dictionary<string, string>> CalculateTags(IGrouping<System.Guid, IAssetEntity> group) |
|||
{ |
|||
var uniqueIds = group.Where(x => x.Tags != null).SelectMany(x => x.Tags).ToHashSet(); |
|||
|
|||
return await tagService.DenormalizeTagsAsync(group.Key, TagGroups.Assets, uniqueIds); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Assets |
|||
{ |
|||
public interface IAssetEnricher |
|||
{ |
|||
Task<IEnrichedAssetEntity> EnrichAsync(IAssetEntity asset); |
|||
|
|||
Task<IReadOnlyList<IEnrichedAssetEntity>> EnrichAsync(IEnumerable<IAssetEntity> assets); |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Squidex.Domain.Apps.Core.Contents; |
|||
using Squidex.Infrastructure.Log; |
|||
using Squidex.Infrastructure.Reflection; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents |
|||
{ |
|||
public sealed class ContentEnricher : IContentEnricher |
|||
{ |
|||
private readonly IContentWorkflow contentWorkflow; |
|||
|
|||
public ContentEnricher(IContentWorkflow contentWorkflow) |
|||
{ |
|||
this.contentWorkflow = contentWorkflow; |
|||
} |
|||
|
|||
public async Task<IReadOnlyList<IEnrichedContentEntity>> EnrichAsync(IEnumerable<IContentEntity> contents) |
|||
{ |
|||
var results = new List<ContentEntity>(); |
|||
|
|||
using (Profiler.TraceMethod<ContentEnricher>()) |
|||
{ |
|||
var cache = new Dictionary<Status, StatusInfo>(); |
|||
|
|||
foreach (var content in contents) |
|||
{ |
|||
var result = SimpleMapper.Map(content, new ContentEntity()); |
|||
|
|||
if (!cache.TryGetValue(content.Status, out var info)) |
|||
{ |
|||
info = await contentWorkflow.GetInfoAsync(content.Status); |
|||
|
|||
cache[content.Status] = info; |
|||
} |
|||
|
|||
result.StatusInfo = info; |
|||
result.Nexts = await contentWorkflow.GetNextsAsync(content); |
|||
|
|||
results.Add(result); |
|||
} |
|||
} |
|||
|
|||
return results; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents |
|||
{ |
|||
public interface IContentEnricher |
|||
{ |
|||
Task<IReadOnlyList<IEnrichedContentEntity>> EnrichAsync(IEnumerable<IContentEntity> contents); |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Squidex.Domain.Apps.Core.Contents; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents |
|||
{ |
|||
public interface IEnrichedContentEntity : IContentEntity |
|||
{ |
|||
StatusInfo StatusInfo { get; } |
|||
|
|||
StatusInfo[] Nexts { get; } |
|||
} |
|||
} |
|||
Loading…
Reference in new issue