diff --git a/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentCollection.cs b/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentCollection.cs index c7db4ccaf5..85de85c823 100644 --- a/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentCollection.cs +++ b/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentCollection.cs @@ -56,7 +56,7 @@ protected override string CollectionName() return "State_Contents"; } - public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, Query query, List ids, Status[] status = null, bool useDraft = false) + public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, Query query, List ids, Status[] status, bool useDraft) { try { @@ -67,6 +67,7 @@ public async Task> QueryAsync(IAppEntity app, ISchem var contentCount = Collection.Find(filter).CountDocumentsAsync(); var contentItems = Collection.Find(filter) + .WithoutDraft(useDraft) .ContentTake(query) .ContentSkip(query) .ContentSort(query) @@ -94,11 +95,11 @@ public async Task> QueryAsync(IAppEntity app, ISchem } } - public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, HashSet ids, Status[] status = null) + public async Task> QueryAsync(IAppEntity app, ISchemaEntity schema, HashSet ids, Status[] status, bool useDraft) { var find = Collection.Find(FilterFactory.Build(schema.Id, ids, status)); - var contentItems = find.ToListAsync(); + var contentItems = find.WithoutDraft(useDraft).ToListAsync(); var contentCount = find.CountDocumentsAsync(); await Task.WhenAll(contentItems, contentCount); @@ -111,11 +112,11 @@ public async Task> QueryAsync(IAppEntity app, ISchem return ResultList.Create(contentCount.Result, contentItems.Result); } - public async Task FindContentAsync(IAppEntity app, ISchemaEntity schema, Guid id, Status[] status = null) + public async Task FindContentAsync(IAppEntity app, ISchemaEntity schema, Guid id, Status[] status, bool useDraft) { var find = Collection.Find(FilterFactory.Build(schema.Id, id, status)); - var contentEntity = await find.FirstOrDefaultAsync(); + var contentEntity = await find.WithoutDraft(useDraft).FirstOrDefaultAsync(); contentEntity?.ParseData(schema.SchemaDef, Serializer); diff --git a/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentRepository.cs b/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentRepository.cs index cd9ee470e1..19f46be3a8 100644 --- a/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentRepository.cs +++ b/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentRepository.cs @@ -55,7 +55,7 @@ public async Task> QueryAsync(IAppEntity app, ISchem { using (Profiler.TraceMethod("QueryAsyncByQuery")) { - var useDraft = RequiresPublished(status); + var useDraft = UseDraft(status); var fullTextIds = await indexer.SearchAsync(query.FullText, app, schema.Id, useDraft); @@ -72,14 +72,9 @@ public async Task> QueryAsync(IAppEntity app, ISchem { using (Profiler.TraceMethod("QueryAsyncByIds")) { - if (RequiresPublished(status)) - { - return await contents.QueryAsync(app, schema, ids); - } - else - { - return await contents.QueryAsync(app, schema, ids, status); - } + var useDraft = UseDraft(status); + + return await contents.QueryAsync(app, schema, ids, status, useDraft); } } @@ -87,14 +82,9 @@ public async Task FindContentAsync(IAppEntity app, ISchemaEntity { using (Profiler.TraceMethod()) { - if (RequiresPublished(status)) - { - return await contents.FindContentAsync(app, schema, id); - } - else - { - return await contents.FindContentAsync(app, schema, id, status); - } + var useDraft = UseDraft(status); + + return await contents.FindContentAsync(app, schema, id, status, useDraft); } } @@ -132,9 +122,9 @@ public Task DeleteArchiveAsync() return database.DropCollectionAsync("States_Contents_Archive"); } - private static bool RequiresPublished(Status[] status) + private static bool UseDraft(Status[] status) { - return status?.Length == 1 && status[0] == Status.Published; + return !(status?.Length == 1 && status[0] == Status.Published); } } } diff --git a/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/Visitors/FilterFactory.cs b/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/Visitors/FilterFactory.cs index 152e98b877..1da6bf8d9e 100644 --- a/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/Visitors/FilterFactory.cs +++ b/src/Squidex.Domain.Apps.Entities.MongoDb/Contents/Visitors/FilterFactory.cs @@ -14,6 +14,7 @@ using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.GenerateEdmSchema; using Squidex.Domain.Apps.Core.Schemas; +using Squidex.Infrastructure.MongoDb; using Squidex.Infrastructure.MongoDb.Queries; using Squidex.Infrastructure.Queries; @@ -109,6 +110,11 @@ public static IFindFluent ContentSkip(th return cursor.Skip(query); } + public static IFindFluent WithoutDraft(this IFindFluent cursor, bool useDraft) + { + return !useDraft ? cursor.Not(x => x.DataDraftByIds, x => x.IsDeleted) : cursor; + } + public static FilterDefinition Build(Guid schemaId, Guid id, Status[] status) { return CreateFilter(schemaId, new List { id }, status, null);