Example #1
0
        public object Any(AutoQueryMetadata request)
        {
            if (NativeTypesMetadata == null)
            {
                throw new NotSupportedException("AutoQueryViewer requries NativeTypesFeature");
            }

            var feature = HostContext.GetPlugin <AutoQueryMetadataFeature>();
            var config  = feature.AutoQueryViewerConfig;

            if (config == null)
            {
                throw new NotSupportedException("AutoQueryViewerConfig is missing");
            }

            if (config.ServiceBaseUrl == null)
            {
                config.ServiceBaseUrl = base.Request.GetBaseUrl();
            }

            if (config.ServiceName == null)
            {
                config.ServiceName = HostContext.AppHost.ServiceName;
            }

            var userSession = Request.GetSession();

            var typesConfig = NativeTypesMetadata.GetConfig(new TypesMetadata {
                BaseUrl = Request.GetBaseUrl()
            });

            foreach (var type in feature.ExportTypes)
            {
                typesConfig.ExportTypes.Add(type);
            }

            var metadataTypes = NativeTypesMetadata.GetMetadataTypes(Request, typesConfig,
                                                                     op => HostContext.Metadata.IsAuthorized(op, Request, userSession));

            var response = new AutoQueryMetadataResponse {
                Config   = config,
                UserInfo = new AutoQueryViewerUserInfo {
                    IsAuthenticated = userSession.IsAuthenticated,
                },
                Operations = new List <AutoQueryOperation>(),
                Types      = new List <MetadataType>(),
            };

            var includeTypeNames = new HashSet <string>();

            foreach (var op in metadataTypes.Operations)
            {
                if (op.Request.Inherits != null &&
                    (op.Request.Inherits.Name.StartsWith("QueryDb`") ||
                     op.Request.Inherits.Name.StartsWith("QueryData`"))
                    )
                {
                    if (config.OnlyShowAnnotatedServices)
                    {
                        var serviceAttrs = op.Request.Attributes.Safe();
                        var attr         = serviceAttrs.FirstOrDefault(x => x.Name + "Attribute" == nameof(AutoQueryViewerAttribute));
                        if (attr == null)
                        {
                            continue;
                        }
                    }

                    var inheritArgs = op.Request.Inherits.GenericArgs.Safe().ToArray();
                    response.Operations.Add(new AutoQueryOperation {
                        Request = op.Request.Name,
                        From    = inheritArgs.First(),
                        To      = inheritArgs.Last(),
                    });

                    response.Types.Add(op.Request);
                    op.Request.GetReferencedTypeNames().Each(x => includeTypeNames.Add(x));
                }
            }

            var allTypes = metadataTypes.GetAllTypes();
            var types    = allTypes.Where(x => includeTypeNames.Contains(x.Name)).ToList();

            //Add referenced types to type name search
            types.SelectMany(x => x.GetReferencedTypeNames()).Each(x => includeTypeNames.Add(x));

            //Only need to seek 1-level deep in AutoQuery's (db.LoadSelect)
            types = allTypes.Where(x => includeTypeNames.Contains(x.Name)).ToList();

            response.Types.AddRange(types);

            response.UserInfo.QueryCount = response.Operations.Count;

            feature.MetadataFilter?.Invoke(response);

            return(response);
        }
Example #2
0
        public object Any(AutoQueryMetadata request)
        {
            if (NativeTypesMetadata == null)
                throw new NotSupportedException("AutoQueryViewer requries NativeTypesFeature");

            var feature = HostContext.GetPlugin<AutoQueryFeature>();
            var config = feature.AutoQueryViewerConfig;

            if (config == null)
                throw new NotSupportedException("AutoQueryViewerConfig is missing");

            if (config.ServiceBaseUrl == null)
                config.ServiceBaseUrl = base.Request.ResolveBaseUrl();

            if (config.ServiceName == null)
                config.ServiceName = HostContext.ServiceName;

            var typesConfig = NativeTypesMetadata.GetConfig(new TypesMetadata { BaseUrl = Request.GetBaseUrl() });
            var metadataTypes = NativeTypesMetadata.GetMetadataTypes(Request, typesConfig);

            var response = new AutoQueryMetadataResponse {
                Config = feature.AutoQueryViewerConfig,
                Operations = new List<AutoQueryOperation>(),
                Types = new List<MetadataType>(),
            };

            var includeTypeNames = new HashSet<string>();

            foreach (var op in metadataTypes.Operations)
            {
                if (op.Request.Inherits != null && op.Request.Inherits.Name.StartsWith("QueryBase`"))
                {
                    if (config.OnlyShowAnnotatedServices)
                    {
                        var serviceAttrs = op.Request.Attributes.Safe();
                        var attr = serviceAttrs.FirstOrDefault(x => x.Name + "Attribute" == typeof(AutoQueryViewerAttribute).Name);
                        if (attr == null)
                            continue;
                    }

                    var inheritArgs = op.Request.Inherits.GenericArgs.Safe().ToArray();
                    response.Operations.Add(new AutoQueryOperation {
                        Request = op.Request.Name,
                        From = inheritArgs.First(),
                        To = inheritArgs.Last(),
                    });

                    response.Types.Add(op.Request);
                    op.Request.GetReferencedTypeNames().Each(x => includeTypeNames.Add(x));
                }
            }

            var types = metadataTypes.Types.Where(x => includeTypeNames.Contains(x.Name));

            //Add referenced types to type name search
            types.SelectMany(x => x.GetReferencedTypeNames()).Each(x => includeTypeNames.Add(x));

            //Only need to seek 1-level deep in AutoQuery's (db.LoadSelect)
            types = metadataTypes.Types.Where(x => includeTypeNames.Contains(x.Name));

            response.Types.AddRange(types);

            return response;
        }