public virtual async Task <object> ProcessQueryAsync(
            HttpRequest request,
            object value,
            Type elementClrType,
            bool ignoreSkip = false,
            bool ignoreTop  = false)
        {
            var model = request.ODataFeature().Model;

            if (request.GetDisplayUrl() == null || value == null ||
                value.GetType().GetTypeInfo().IsValueType || value is string)
            {
                return(value);
            }

            var queryContext = new ODataQueryContext(
                model,
                elementClrType,
                request.ODataFeature().Path);

            var shouldApplyQuery =
                request.HasQueryOptions() ||
                PageSize.HasValue ||
                new InterceptorContainer(elementClrType, request.HttpContext.RequestServices).Any ||
                value is SingleResult ||
                ODataCountMediaTypeMapping.IsCountRequest(request.HttpContext) ||
                ContainsAutoExpandProperty(queryContext, QuerySettings);

            if (!shouldApplyQuery)
            {
                return(value);
            }

            var queryOptions = new ODataQueryOptions(queryContext, request, request.HttpContext.RequestServices);

            if (ignoreSkip)
            {
                queryOptions.IgnoreSkip = true;
            }
            if (ignoreSkip)
            {
                queryOptions.IgnoreTop = true;
            }
            var processedResult = await ApplyQueryOptionsAsync(
                value,
                queryOptions);

            var enumberable = processedResult as IEnumerable <object>;

            if (enumberable != null)
            {
                long?count = null;
                // Apply count to result, if necessary, and return as a page result
                if (queryOptions.Count)
                {
                    //count = request.ODataFeature().TotalCount;
                    count = await Count(value, queryOptions);
                }

                // We might be getting a single result, so no paging involved
                var nextPageLink = request.ODataFeature().NextLink;
                var pageResult   = new PageResult <object>(enumberable, nextPageLink, count);
                value = pageResult;
            }
            else
            {
                // Return just the single entity
                value = processedResult;
            }

            return(value);
        }
Exemplo n.º 2
0
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            var response = context.Result as StatusCodeResult;

            if (response != null)// && !response.IsSuccessStatusCode())
            {
                return;
            }

            var request = context.HttpContext.Request;
            var model   = request.ODataFeature().Model;

            if (model == null)
            {
                throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull);
            }

            var result = context.Result as ObjectResult;

            if (result == null)
            {
                if (context.Exception != null)
                {
                    throw context.Exception;
                }
                throw Error.Argument("context", SRResources.QueryingRequiresObjectContent, context.Result.GetType().FullName);
            }

            if (result?.Value is ODataError)
            {
                return;
            }

            var value = result.Value;

            if (request.GetDisplayUrl() == null || value == null ||
                value.GetType().GetTypeInfo().IsValueType || value is string)
            {
                return;
            }

            var elementClrType = result.GetElementType();
            var queryContext   = new ODataQueryContext(
                model,
                elementClrType,
                request.ODataFeature().Path);

            var shouldApplyQuery =
                request.HasQueryOptions() ||
                ResolvePageSize(_querySettings, context.ActionDescriptor).HasValue ||
                new InterceptorContainer(elementClrType, context.HttpContext.RequestServices).Any ||
                value is SingleResult ||
                ODataCountMediaTypeMapping.IsCountRequest(context.HttpContext) ||
                ContainsAutoExpandProperty(queryContext);

            if (!shouldApplyQuery)
            {
                return;
            }

            var queryOptions = new ODataQueryOptions(queryContext, request, context.HttpContext.RequestServices);

            long?count           = null;
            var  processedResult = ApplyQueryOptions(result.Value, queryOptions, context.ActionDescriptor);

            var enumberable = processedResult as IEnumerable <object>;

            if (enumberable != null)
            {
                // Apply count to result, if necessary, and return as a page result
                if (queryOptions.Count)
                {
                    count = request.ODataFeature().TotalCount;
                    count = Count(result.Value, queryOptions, context.ActionDescriptor);
                }
                // We might be getting a single result, so no paging involved
                var nextPageLink = request.ODataFeature().NextLink;
                var pageResult   = new PageResult <object>(enumberable, nextPageLink, count);
                result.Value = pageResult;
            }
            else
            {
                // Return just the single entity
                result.Value = processedResult;
            }
        }