using Raven.Client.Documents.Session; using System; using System.Collections.Generic; using zero.Core.Api; using zero.Core.Entities; namespace zero.Core.Extensions { public static class RavenAsyncDocumentQueryExtensions { static Type _appAwareEntity = typeof(IAppAwareEntity); public static IAsyncDocumentQuery Scope(this IAsyncDocumentQuery source, string appId, bool includeShared = true) { if (appId.IsNullOrEmpty() || !_appAwareEntity.IsAssignableFrom(typeof(T))) { return source; } HashSet ids = new HashSet(); ids.Add(appId); if (includeShared) { ids.Add(Constants.Database.SharedAppId); } return source.WhereIn(nameof(IAppAwareEntity.AppId), ids); } public static IAsyncDocumentQuery Scope(this IAsyncDocumentQuery source, ApiScope scope) { if (scope == null || scope.IsShared) { return source; } if (scope.AppId.IsNullOrEmpty() || !_appAwareEntity.IsAssignableFrom(typeof(T))) { return source; } HashSet ids = new HashSet(); ids.Add(scope.AppId); if (scope.IncludeShared) { ids.Add(Constants.Database.SharedAppId); } return source.WhereIn(nameof(IAppAwareEntity.AppId), ids); } } }