Exemplo n.º 1
0
        private IQuery CreateQuery(IEntityDefinition entityDefinition, RestQueryString restQuery, RestProcessParameters param)
        {
            var query = EntityService.CreateQuery(entityDefinition);

            foreach (
                var queryStringValue in
                restQuery.QueryStringValues.Where(x => !x.Key.StartsWith(RestConstants.ReservedOperatorsPrefix)))
            {
                RecursiveFillQuery(query, null, queryStringValue.Key, queryStringValue.Value);
            }

            FillOperators(query, restQuery, param);

            if (restQuery.PathValues.Count == 0)
            {
                return(query);
            }

            if (restQuery.PathValues.Count > 1)
            {
                throw new BadRequestException("Multi-level query expressions in path is not supported.");
            }

            var pathValue = restQuery.PathValues.First();

            var targetEntityDef = entityDefinition.Model.EntitiesByPluralName.SafeGet(pathValue.Key);

            if (targetEntityDef == null)
            {
                throw new NotFoundException($"Unable to find an entity definition named {pathValue.Key}");
            }

            var targetPropertyDefinition = entityDefinition.Properties.SafeGet(targetEntityDef.SingleName);

            if (targetPropertyDefinition != null)
            {
                var subQuery = query.GetOrCreateSubQuery(targetPropertyDefinition.Name);
                RecursiveFillQuery(subQuery, null, MetaConstants.IdProperty, pathValue.Value);
            }

            return(query);
        }