// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using MongoDB.Bson; using MongoDB.Driver; using Squidex.Domain.Apps.Entities.Apps; using Squidex.Domain.Apps.Entities.Assets.DomainObject; using Squidex.Infrastructure; using Squidex.Infrastructure.MongoDb; using Squidex.Infrastructure.States; #pragma warning disable MA0048 // File name must match type name namespace Squidex.Domain.Apps.Entities.MongoDb.Assets { public sealed partial class MongoAssetRepository : ISnapshotStore, IDeleter { Task IDeleter.DeleteAppAsync(IAppEntity app, CancellationToken ct) { return Collection.DeleteManyAsync(Filter.Eq(x => x.IndexedAppId, app.Id), ct); } IAsyncEnumerable> ISnapshotStore.ReadAllAsync( CancellationToken ct) { return Collection.Find(new BsonDocument(), Batching.Options).ToAsyncEnumerable(ct) .Select(x => new SnapshotResult(x.DocumentId, x.ToState(), x.Version)); } async Task> ISnapshotStore.ReadAsync(DomainId key, CancellationToken ct) { using (Telemetry.Activities.StartActivity("MongoAssetRepository/ReadAsync")) { var existing = await Collection.Find(x => x.DocumentId == key) .FirstOrDefaultAsync(ct); if (existing != null) { return new SnapshotResult(existing.DocumentId, existing.ToState(), existing.Version); } return new SnapshotResult(default, null!, EtagVersion.Empty); } } async Task ISnapshotStore.WriteAsync(SnapshotWriteJob job, CancellationToken ct) { using (Telemetry.Activities.StartActivity("MongoAssetRepository/WriteAsync")) { var entity = MongoAssetEntity.Create(job); await Collection.UpsertVersionedAsync(job.Key, job.OldVersion, job.NewVersion, entity, ct); } } async Task ISnapshotStore.WriteManyAsync(IEnumerable> jobs, CancellationToken ct) { using (Telemetry.Activities.StartActivity("MongoAssetRepository/WriteManyAsync")) { var updates = jobs.Select(MongoAssetEntity.Create).Select(x => new ReplaceOneModel( Filter.Eq(y => y.DocumentId, x.DocumentId), x) { IsUpsert = true }).ToList(); if (updates.Count == 0) { return; } await Collection.BulkWriteAsync(updates, BulkUnordered, ct); } } async Task ISnapshotStore.RemoveAsync(DomainId key, CancellationToken ct) { using (Telemetry.Activities.StartActivity("MongoAssetRepository/RemoveAsync")) { await Collection.DeleteOneAsync(x => x.DocumentId == key, null, ct); } } } }