// ========================================================================== // 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.Tasks; using Microsoft.OData.UriParser; 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.Schemas; using Squidex.Infrastructure; using Squidex.Infrastructure.Log; namespace Squidex.Domain.Apps.Entities.MongoDb.Contents { public partial class MongoContentRepository : IContentRepository, IInitializable { private readonly IMongoDatabase database; private readonly IAppProvider appProvider; private readonly MongoContentDraftCollection contentsDraft; private readonly MongoContentPublishedCollection contentsPublished; public MongoContentRepository(IMongoDatabase database, IAppProvider appProvider) { Guard.NotNull(appProvider, nameof(appProvider)); this.appProvider = appProvider; contentsDraft = new MongoContentDraftCollection(database); contentsPublished = new MongoContentPublishedCollection(database); this.database = database; } public void Initialize() { contentsDraft.Initialize(); contentsPublished.Initialize(); } public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, Status[] status, ODataUriParser odataQuery) { using (Profiler.TraceMethod("QueryAsyncByQuery")) { if (RequiresPublished(status)) { return await contentsPublished.QueryAsync(app, schema, odataQuery); } else { return await contentsDraft.QueryAsync(app, schema, odataQuery, status, true); } } } public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, Status[] status, HashSet ids) { using (Profiler.TraceMethod("QueryAsyncByIds")) { if (RequiresPublished(status)) { return await contentsPublished.QueryAsync(app, schema, ids); } else { return await contentsDraft.QueryAsync(app, schema, ids, status); } } } public async Task FindContentAsync(IAppEntity app, ISchemaEntity schema, Status[] status, Guid id) { using (Profiler.TraceMethod()) { if (RequiresPublished(status)) { return await contentsPublished.FindContentAsync(app, schema, id); } else { return await contentsDraft.FindContentAsync(app, schema, id); } } } public async Task> QueryNotFoundAsync(Guid appId, Guid schemaId, IList ids) { using (Profiler.TraceMethod()) { await contentsDraft.QueryNotFoundAsync(appId, schemaId, ids); } } public async Task QueryScheduledWithoutDataAsync(Instant now, Func callback) { using (Profiler.TraceMethod()) { await contentsDraft.QueryScheduledWithoutDataAsync(now, callback); } } public Task ClearAsync() { return Task.WhenAll( contentsDraft.ClearAsync(), contentsPublished.ClearAsync()); } public Task DeleteArchiveAsync() { return database.DropCollectionAsync("States_Contents_Archive"); } private static bool RequiresPublished(Status[] status) { return status?.Length == 1 && status[0] == Status.Published; } } }