mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
5.0 KiB
149 lines
5.0 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Squidex.Domain.Apps.Entities.Assets;
|
|
using Squidex.Infrastructure;
|
|
|
|
namespace Squidex.Domain.Apps.Entities.Contents.Queries
|
|
{
|
|
public class QueryExecutionContext : Dictionary<string, object>
|
|
{
|
|
private readonly ConcurrentDictionary<Guid, IEnrichedContentEntity?> cachedContents = new ConcurrentDictionary<Guid, IEnrichedContentEntity?>();
|
|
private readonly ConcurrentDictionary<Guid, IEnrichedAssetEntity?> cachedAssets = new ConcurrentDictionary<Guid, IEnrichedAssetEntity?>();
|
|
private readonly IContentQueryService contentQuery;
|
|
private readonly IAssetQueryService assetQuery;
|
|
private readonly Context context;
|
|
|
|
public Context Context
|
|
{
|
|
get { return context; }
|
|
}
|
|
|
|
public QueryExecutionContext(Context context, IAssetQueryService assetQuery, IContentQueryService contentQuery)
|
|
{
|
|
Guard.NotNull(assetQuery, nameof(assetQuery));
|
|
Guard.NotNull(contentQuery, nameof(contentQuery));
|
|
Guard.NotNull(context, nameof(context));
|
|
|
|
this.assetQuery = assetQuery;
|
|
this.contentQuery = contentQuery;
|
|
this.context = context;
|
|
}
|
|
|
|
public virtual async Task<IEnrichedAssetEntity?> FindAssetAsync(Guid id)
|
|
{
|
|
var asset = cachedAssets.GetOrDefault(id);
|
|
|
|
if (asset == null)
|
|
{
|
|
asset = await assetQuery.FindAssetAsync(context, id);
|
|
|
|
if (asset != null)
|
|
{
|
|
cachedAssets[asset.Id] = asset;
|
|
}
|
|
}
|
|
|
|
return asset;
|
|
}
|
|
|
|
public virtual async Task<IEnrichedContentEntity?> FindContentAsync(Guid schemaId, Guid id)
|
|
{
|
|
var content = cachedContents.GetOrDefault(id);
|
|
|
|
if (content == null)
|
|
{
|
|
content = await contentQuery.FindContentAsync(context, schemaId.ToString(), id);
|
|
|
|
if (content != null)
|
|
{
|
|
cachedContents[content.Id] = content;
|
|
}
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
public virtual async Task<IResultList<IEnrichedAssetEntity>> QueryAssetsAsync(string odata)
|
|
{
|
|
var q = Q.Empty.WithODataQuery(odata);
|
|
|
|
var assets = await assetQuery.QueryAsync(context, null, q);
|
|
|
|
foreach (var asset in assets)
|
|
{
|
|
cachedAssets[asset.Id] = asset;
|
|
}
|
|
|
|
return assets;
|
|
}
|
|
|
|
public virtual async Task<IResultList<IEnrichedContentEntity>> QueryContentsAsync(string schemaIdOrName, string odata)
|
|
{
|
|
var q = Q.Empty.WithODataQuery(odata);
|
|
|
|
var result = await contentQuery.QueryAsync(context, schemaIdOrName, q);
|
|
|
|
foreach (var content in result)
|
|
{
|
|
cachedContents[content.Id] = content;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public virtual async Task<IReadOnlyList<IEnrichedAssetEntity>> GetReferencedAssetsAsync(ICollection<Guid> ids)
|
|
{
|
|
Guard.NotNull(ids, nameof(ids));
|
|
|
|
var notLoadedAssets = new HashSet<Guid>(ids.Where(id => !cachedAssets.ContainsKey(id)));
|
|
|
|
if (notLoadedAssets.Count > 0)
|
|
{
|
|
var assets = await assetQuery.QueryAsync(context, null, Q.Empty.WithIds(notLoadedAssets));
|
|
|
|
foreach (var asset in assets)
|
|
{
|
|
cachedAssets[asset.Id] = asset;
|
|
}
|
|
}
|
|
|
|
return ids.Select(cachedAssets.GetOrDefault).NotNull().ToList();
|
|
}
|
|
|
|
public virtual async Task<IReadOnlyList<IEnrichedContentEntity>> GetReferencedContentsAsync(ICollection<Guid> ids)
|
|
{
|
|
Guard.NotNull(ids, nameof(ids));
|
|
|
|
var notLoadedContents = ids.Where(id => !cachedContents.ContainsKey(id)).ToList();
|
|
|
|
if (notLoadedContents.Count > 0)
|
|
{
|
|
var result = await contentQuery.QueryAsync(context, notLoadedContents);
|
|
|
|
foreach (var content in result)
|
|
{
|
|
cachedContents[content.Id] = content;
|
|
}
|
|
}
|
|
|
|
return ids.Select(cachedContents.GetOrDefault).NotNull().ToList();
|
|
}
|
|
|
|
public Task<IResultList<IEnrichedContentEntity>> QueryReferencingContentsAsync(string schemaIdOrName, string odata, Guid reference)
|
|
{
|
|
var q = Q.Empty.WithODataQuery(odata).WithReference(reference);
|
|
|
|
return contentQuery.QueryAsync(context, schemaIdOrName, q);
|
|
}
|
|
}
|
|
}
|
|
|