mirror of https://github.com/Squidex/squidex.git
24 changed files with 407 additions and 165 deletions
@ -0,0 +1,101 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Squidex.Domain.Apps.Entities.Assets.Repositories; |
|||
using Squidex.Domain.Apps.Entities.Tags; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Assets |
|||
{ |
|||
public sealed class AssetQueryService : IAssetQueryService |
|||
{ |
|||
private readonly ITagService tagService; |
|||
private readonly IAssetRepository assetRepository; |
|||
|
|||
public AssetQueryService(ITagService tagService, IAssetRepository assetRepository) |
|||
{ |
|||
Guard.NotNull(tagService, nameof(tagService)); |
|||
Guard.NotNull(assetRepository, nameof(assetRepository)); |
|||
|
|||
this.tagService = tagService; |
|||
|
|||
this.assetRepository = assetRepository; |
|||
} |
|||
|
|||
public async Task<IAssetEntity> FindAssetAsync(QueryContext context, Guid id) |
|||
{ |
|||
Guard.NotNull(context, nameof(context)); |
|||
|
|||
var asset = await assetRepository.FindAssetAsync(id); |
|||
|
|||
if (asset != null) |
|||
{ |
|||
await DenormalizeTagsAsync(context.App.Id, Enumerable.Repeat(asset, 1)); |
|||
} |
|||
|
|||
return asset; |
|||
} |
|||
|
|||
public async Task<IResultList<IAssetEntity>> QueryAsync(QueryContext context, Query query) |
|||
{ |
|||
Guard.NotNull(context, nameof(context)); |
|||
Guard.NotNull(query, nameof(query)); |
|||
|
|||
IResultList<IAssetEntity> assets; |
|||
|
|||
if (query.Ids != null) |
|||
{ |
|||
assets = await assetRepository.QueryAsync(context.App.Id, new HashSet<Guid>(query.Ids)); |
|||
assets = Sort(assets, query.Ids); |
|||
} |
|||
else |
|||
{ |
|||
assets = await assetRepository.QueryAsync(context.App.Id, query.ODataQuery); |
|||
} |
|||
|
|||
await DenormalizeTagsAsync(context.App.Id, assets); |
|||
|
|||
return assets; |
|||
} |
|||
|
|||
private IResultList<IAssetEntity> Sort(IResultList<IAssetEntity> assets, IList<Guid> ids) |
|||
{ |
|||
var sorted = ids.Select(id => assets.FirstOrDefault(x => x.Id == id)).Where(x => x != null); |
|||
|
|||
return ResultList.Create(sorted, assets.Total); |
|||
} |
|||
|
|||
private async Task DenormalizeTagsAsync(Guid appId, IEnumerable<IAssetEntity> assets) |
|||
{ |
|||
var tags = assets.SelectMany(x => x.Tags).Distinct().ToArray(); |
|||
|
|||
var tagsById = await tagService.DenormalizeTagsAsync(appId, "Assets", tags); |
|||
|
|||
foreach (var asset in assets) |
|||
{ |
|||
if (asset.Tags?.Length > 0) |
|||
{ |
|||
var tagNames = new List<string>(); |
|||
|
|||
foreach (var id in asset.Tags) |
|||
{ |
|||
if (tagsById.TryGetValue(id, out var name)) |
|||
{ |
|||
tagNames.Add(name); |
|||
} |
|||
} |
|||
|
|||
asset.Tags = tagNames.ToArray(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Assets |
|||
{ |
|||
public interface IAssetQueryService |
|||
{ |
|||
Task<IResultList<IAssetEntity>> QueryAsync(QueryContext contex, Query query); |
|||
|
|||
Task<IAssetEntity> FindAssetAsync(QueryContext context, Guid id); |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents |
|||
{ |
|||
public sealed class ContentQueryContext : Cloneable<ContentQueryContext> |
|||
{ |
|||
public string SchemaIdOrName { get; private set; } |
|||
|
|||
public QueryContext Base { get; private set; } |
|||
|
|||
public ContentQueryContext(QueryContext @base) |
|||
{ |
|||
Guard.NotNull(@base, nameof(@base)); |
|||
|
|||
Base = @base; |
|||
} |
|||
|
|||
public ContentQueryContext WithSchemaName(string name) |
|||
{ |
|||
return Clone(c => c.SchemaIdOrName = name); |
|||
} |
|||
|
|||
public ContentQueryContext WithArchived(bool archived) |
|||
{ |
|||
return Clone(c => c.Base = c.Base.WithArchived(archived)); |
|||
} |
|||
|
|||
public ContentQueryContext WithFlatten(bool flatten) |
|||
{ |
|||
return Clone(c => c.Base = c.Base.WithFlatten(flatten)); |
|||
} |
|||
|
|||
public ContentQueryContext WithSchemaId(Guid id) |
|||
{ |
|||
return Clone(c => c.SchemaIdOrName = id.ToString()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities |
|||
{ |
|||
public sealed class Query : Cloneable<Query> |
|||
{ |
|||
public static readonly Query Empty = new Query(); |
|||
|
|||
public List<Guid> Ids { get; private set; } |
|||
|
|||
public string ODataQuery { get; private set; } |
|||
|
|||
public Query WithODataQuery(string odataQuery) |
|||
{ |
|||
return Clone(c => c.ODataQuery = odataQuery); |
|||
} |
|||
|
|||
public Query WithIds(IEnumerable<Guid> ids) |
|||
{ |
|||
return Clone(c => c.Ids = ids.ToList()); |
|||
} |
|||
|
|||
public Query WithIds(string ids) |
|||
{ |
|||
if (string.IsNullOrEmpty(ids)) |
|||
{ |
|||
return Clone(c => |
|||
{ |
|||
c.Ids = new List<Guid>(); |
|||
|
|||
foreach (var id in ids.Split(',')) |
|||
{ |
|||
if (Guid.TryParse(id, out var guid)) |
|||
{ |
|||
c.Ids.Add(guid); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
return this; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue