Exemple #1
0
        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);
        }