// ========================================================================== // 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.Linq; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; using Squidex.Domain.Apps.Entities.Assets; using Squidex.Domain.Apps.Entities.Assets.Repositories; using Squidex.Infrastructure; using Squidex.Infrastructure.MongoDb; namespace Squidex.Domain.Apps.Entities.MongoDb.Assets { public sealed partial class MongoAssetRepository : MongoRepositoryBase, IAssetRepository { public MongoAssetRepository(IMongoDatabase database) : base(database) { } protected override string CollectionName() { return "States_Assets"; } protected override Task SetupCollectionAsync(IMongoCollection collection) { return collection.Indexes.CreateOneAsync( Index .Ascending(x => x.State.AppId) .Ascending(x => x.State.IsDeleted) .Ascending(x => x.State.FileName) .Ascending(x => x.State.MimeType) .Descending(x => x.State.LastModified)); } public async Task> QueryAsync(Guid appId, HashSet mimeTypes = null, HashSet ids = null, string query = null, int take = 10, int skip = 0) { var filters = new List> { Filter.Eq(x => x.State.AppId, appId), Filter.Eq(x => x.State.IsDeleted, false) }; if (ids != null && ids.Count > 0) { filters.Add(Filter.In(x => x.Id, ids)); } if (mimeTypes != null && mimeTypes.Count > 0) { filters.Add(Filter.In(x => x.State.MimeType, mimeTypes)); } if (!string.IsNullOrWhiteSpace(query)) { filters.Add(Filter.Regex(x => x.State.FileName, new BsonRegularExpression(query, "i"))); } var filter = Filter.And(filters); var assetItems = Collection.Find(filter).Skip(skip).Limit(take).SortByDescending(x => x.State.LastModified).ToListAsync(); var assetCount = Collection.Find(filter).CountAsync(); await Task.WhenAll(assetItems, assetCount); return ResultList.Create(assetItems.Result.Select(x => x.State), assetCount.Result); } public async Task FindAssetAsync(Guid id) { var assetEntity = await Collection.Find(x => x.Id == id) .FirstOrDefaultAsync(); return assetEntity?.State; } } }