public void ToBinaryTree_PropertyNotExists() { var filter = new Dictionary <string, string> { { "p", "d" } }; ExpressionTreeBuilder.BuildBinaryTreeExpression <TestClass>(filter).ShouldBeNull(); }
public Task <IEnumerable <TEntity> > GetAll(Pagination <TEntity> pagination) { var data = LiteDbUtility.Query <IEnumerable <TEntity> >(_dbName, db => { var col = db.GetCollection <TEntity>(); if (pagination == null) { return(col.FindAll().ToArray()); } var query = ExpressionTreeBuilder.BuildBinaryTreeExpression <TEntity>(pagination.QueryOrFilter); if (query == null) { return(null); } return(col.Find(query).ToArray()); }); return(Task.FromResult(data)); }
public void ToBinaryTree_BuildsExpression() { var col = new[] { new TestClass { Id = "1", NumericValue = 1, StringValue = "1" }, new TestClass { Id = "2", NumericValue = 1, StringValue = "2" }, new TestClass { Id = "3", NumericValue = 3 }, }; var filter1 = new Dictionary <string, string> { { nameof(TestClass.NumericValue), "1" } }; var f1 = ExpressionTreeBuilder.BuildBinaryTreeExpression <TestClass>(filter1); f1.ShouldNotBeNull(); var res1 = col.Where(f1).ToArray(); res1.Count().ShouldBe(2); var filter2 = new Dictionary <string, string> { { nameof(TestClass.StringValue), "1" }, { nameof(TestClass.NumericValue), "1" }, }; var f2 = ExpressionTreeBuilder.BuildBinaryTreeExpression <TestClass>(filter2); f2.ShouldNotBeNull(); var res2 = col.Where(f2).ToArray(); res2.Count().ShouldBe(1); }
public void ToBinaryTree_FromString(string query, string expResult) { var e = ExpressionTreeBuilder.BuildBinaryTreeExpression(typeof(TestClass), query); e.ToString().ShouldBe(expResult); }
[InlineData("\"this-is-id\" == id")] // property name is second public void ToBinaryTree_FromString_ReturnsNull(string query) { ExpressionTreeBuilder.BuildBinaryTreeExpression(typeof(TestClass), query).ShouldBeNull(); }
public void ToBinaryTree_EmptyOrNullFilter_ReturnsNull(IDictionary <string, string> filter) { ExpressionTreeBuilder.BuildBinaryTreeExpression <TestClass>(filter).ShouldBeNull(); }
private async Task <Pagination <TEntity> > NormalizePagination(Pagination <TEntity> pagination) { var p = pagination ??= new Pagination <TEntity>(); if (!p.QueryOrFilter.HasValue() && p.QueryFunc == null) { p.QueryFunc = x => true; } var filter = p.QueryOrFilter.HasValue() ? await FilterFactory.GetFilter <TEntity>(p.QueryOrFilter) : null; if (filter != null) { var f = filter(new object());//payload is not supported yet if (f == null) { return(null); } p.QueryFunc = c => f(c); } else { if (p.QueryFunc == null) //build only if func not exists { var right = ExpressionTreeBuilder.BuildBinaryTreeExpression <TEntity>(p.QueryOrFilter)?.Compile(); if (right == null) { return(null); } var ecr = WorkContext.CurrentEntityConfigRecord; if (Config.ManageEntityPermissions) { var permittedIds = await PermissionManager.GetPermittedIds(WorkContext.CurrentUserId, ecr.EntityKey, ecr.PermissionRecord.ReadKey); Func <TEntity, bool> left = a => permittedIds.Contains(a.Id); p.QueryFunc = x => left(x) && right(x); } else { p.QueryFunc = x => right(x); } } } if (p.QueryFunc == null) { return(null); } if (!CurrentEntityConfigRecord.Metadata.ShowSoftDeleted && CurrentEntityConfigRecord.Metadata.IsSoftDeleted) { p.QueryFunc = p.QueryFunc.AndAlso(HideSoftDeletedFunc); } var paginationSettings = WorkContext.CurrentEntityConfigRecord.PaginationSettings; p.OrderBy ??= paginationSettings.DefaultOrderBy; p.Offset = p.Offset != 0 ? p.Offset : paginationSettings.DefaultOffset; p.PageSize = p.PageSize != 0 ? p.PageSize : paginationSettings.DefaultPageSize; p.IncludeNested = p.IncludeNested; p.SortOrder ??= paginationSettings.DefaultSortOrder; return(p); }