// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using NodaTime; using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Entities.Apps; using Squidex.Domain.Apps.Entities.Contents; using Squidex.Domain.Apps.Entities.Contents.Repositories; using Squidex.Domain.Apps.Entities.Contents.Text; using Squidex.Domain.Apps.Entities.Schemas; using Squidex.Domain.Apps.Events.Assets; using Squidex.Domain.Apps.Events.Contents; using Squidex.Infrastructure; using Squidex.Infrastructure.Json; using Squidex.Infrastructure.Log; using Squidex.Infrastructure.Queries; using Squidex.Infrastructure.Reflection; namespace Squidex.Domain.Apps.Entities.MongoDb.Contents { public partial class MongoContentRepository : IContentRepository, IInitializable { private readonly IAppProvider appProvider; private readonly IJsonSerializer serializer; private readonly ITextIndexer indexer; private readonly string typeAssetDeleted; private readonly string typeContentDeleted; private readonly MongoContentCollection contents; static MongoContentRepository() { StatusSerializer.Register(); } public MongoContentRepository(IMongoDatabase database, IAppProvider appProvider, IJsonSerializer serializer, ITextIndexer indexer, TypeNameRegistry typeNameRegistry) { Guard.NotNull(appProvider, nameof(appProvider)); Guard.NotNull(serializer, nameof(serializer)); Guard.NotNull(indexer, nameof(indexer)); Guard.NotNull(typeNameRegistry, nameof(typeNameRegistry)); this.appProvider = appProvider; this.indexer = indexer; this.serializer = serializer; typeAssetDeleted = typeNameRegistry.GetName(); typeContentDeleted = typeNameRegistry.GetName(); contents = new MongoContentCollection(database, serializer, appProvider); } public Task InitializeAsync(CancellationToken ct = default) { return contents.InitializeAsync(ct); } public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, Status[] status, bool inDraft, ClrQuery query, bool includeDraft = true) { Guard.NotNull(app, nameof(app)); Guard.NotNull(schema, nameof(schema)); Guard.NotNull(query, nameof(query)); using (Profiler.TraceMethod("QueryAsyncByQuery")) { var fullTextIds = await indexer.SearchAsync(query.FullText, app, schema.Id, inDraft ? Scope.Draft : Scope.Published); if (fullTextIds?.Count == 0) { return ResultList.CreateFrom(0); } return await contents.QueryAsync(schema, query, fullTextIds, status, inDraft, includeDraft); } } public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, Status[] status, HashSet ids, bool includeDraft = true) { Guard.NotNull(app, nameof(app)); Guard.NotNull(ids, nameof(ids)); Guard.NotNull(schema, nameof(schema)); using (Profiler.TraceMethod("QueryAsyncByIds")) { return await contents.QueryAsync(schema, ids, status, includeDraft); } } public async Task> QueryAsync(IAppEntity app, Status[] status, HashSet ids, bool includeDraft = true) { Guard.NotNull(app, nameof(app)); Guard.NotNull(ids, nameof(ids)); using (Profiler.TraceMethod("QueryAsyncByIdsWithoutSchema")) { return await contents.QueryAsync(app, ids, status, includeDraft); } } public async Task FindContentAsync(IAppEntity app, ISchemaEntity schema, Status[] status, Guid id, bool includeDraft = true) { Guard.NotNull(app, nameof(app)); Guard.NotNull(schema, nameof(schema)); using (Profiler.TraceMethod()) { return await contents.FindContentAsync(schema, id, status, includeDraft); } } public async Task> QueryIdsAsync(Guid appId, Guid schemaId, FilterNode filterNode) { using (Profiler.TraceMethod()) { return await contents.QueryIdsAsync(await appProvider.GetSchemaAsync(appId, schemaId), filterNode); } } public async Task QueryScheduledWithoutDataAsync(Instant now, Func callback) { using (Profiler.TraceMethod()) { await contents.QueryScheduledWithoutDataAsync(now, callback); } } public Task ClearAsync() { return contents.ClearAsync(); } } }