public IEnumerable<ControllerMethod> GetMethods(Resource resource, string url, ControllerObject parent, string objectName)
		{
			var methodsNames = new List<string>();
			if (parent != null && parent.Methods != null)
				methodsNames = parent.Methods.Select(m => m.Name).ToList();

			var generatorMethods = new Collection<ControllerMethod>();
			if (resource.Methods == null)
				return generatorMethods;

			foreach (var method in resource.Methods)
			{
				var generatedMethod = BuildControllerMethod(url, method, resource, parent);

				if (IsVerbForMethod(method))
				{
					if (methodsNames.Contains(generatedMethod.Name))
						generatedMethod.Name = GetUniqueName(methodsNames, generatedMethod.Name, resource.RelativeUri);

					if (method.QueryParameters != null && method.QueryParameters.Any())
					{
						var queryParameters = QueryParametersParser.ParseParameters(method);
						generatedMethod.QueryParameters = queryParameters;
					}

					generatorMethods.Add(generatedMethod);
					methodsNames.Add(generatedMethod.Name);
				}
			}

			return generatorMethods;
		}
Beispiel #2
0
        private void GetControllers(IList <ControllerObject> classes,
                                    ICollection <string> classesNames, IDictionary <string, ControllerObject> classesObjectsRegistry, IDictionary <string, Parameter> parentUriParameters)
        {
            var rootController = new ControllerObject {
                Name = "Home", PrefixUri = "/"
            };

            foreach (var resource in raml.Resources)
            {
                if (resource == null)
                {
                    continue;
                }

                var fullUrl = GetUrl(string.Empty, resource.RelativeUri);

                // when the resource is a parameter dont generate a class but add it's methods and children to the parent
                if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}"))
                {
                    AddMethodsToRootController(classes, classesNames, classesObjectsRegistry, resource, fullUrl, rootController, parentUriParameters);

                    GetMethodsFromChildResources(resource.Resources, fullUrl, rootController, resource.UriParameters);
                }
                else
                {
                    var controller = CreateControllerAndAddMethods(classes, classesNames, classesObjectsRegistry, resource, fullUrl, parentUriParameters);

                    GetMethodsFromChildResources(resource.Resources, fullUrl, controller, resource.UriParameters);
                }
            }
        }
        private void GetControllers(IList <ControllerObject> classes,
                                    ICollection <string> classesNames, IDictionary <string, ControllerObject> classesObjectsRegistry)
        {
            var rootController = new ControllerObject {
                Name = "Home", PrefixUri = "/"
            };

            foreach (var resource in raml.Resources)
            {
                if (resource == null)
                {
                    continue;
                }

                var fullUrl = GetUrl(string.Empty, resource.RelativeUri);

                // when the resource is a parameter dont generate a class but add it's methods and children to the parent
                if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}"))
                {
                    var generatedMethods = webApiMethodsGenerator.GetMethods(resource, fullUrl, rootController, rootController.Name, schemaResponseObjects, schemaRequestObjects);
                    foreach (var method in generatedMethods)
                    {
                        rootController.Methods.Add(method);
                    }

                    GetMethodsFromChildResources(resource.Resources, fullUrl, rootController);

                    classesNames.Add(rootController.Name);
                    classesObjectsRegistry.Add(CalculateClassKey("/"), rootController);
                    classes.Add(rootController);
                }
                else
                {
                    var controller = new ControllerObject
                    {
                        Name        = GetUniqueObjectName(resource, null),
                        PrefixUri   = UrlGeneratorHelper.FixControllerRoutePrefix(fullUrl),
                        Description = resource.Description,
                    };

                    var methods = webApiMethodsGenerator.GetMethods(resource, fullUrl, controller, controller.Name, schemaResponseObjects, schemaRequestObjects);
                    foreach (var method in methods)
                    {
                        controller.Methods.Add(method);
                    }

                    classesNames.Add(controller.Name);
                    classes.Add(controller);
                    classesObjectsRegistry.Add(CalculateClassKey(fullUrl), controller);

                    GetMethodsFromChildResources(resource.Resources, fullUrl, controller);
                }
            }
        }
        private void AddMethodsToRootController(IList<ControllerObject> classes, ICollection<string> classesNames, IDictionary<string, ControllerObject> classesObjectsRegistry,
            Resource resource, string fullUrl, ControllerObject rootController, IDictionary<string, Parameter> parentUriParameters)
        {
            var generatedMethods = webApiMethodsGenerator.GetMethods(resource, fullUrl, rootController, rootController.Name, parentUriParameters);
            foreach (var method in generatedMethods)
            {
                rootController.Methods.Add(method);
            }

            classesNames.Add(rootController.Name);
            classesObjectsRegistry.Add(CalculateClassKey("/"), rootController);
            classes.Add(rootController);
        }
Beispiel #5
0
        private void AddMethodsToRootController(IList <ControllerObject> classes, ICollection <string> classesNames, IDictionary <string, ControllerObject> classesObjectsRegistry,
                                                Resource resource, string fullUrl, ControllerObject rootController, IDictionary <string, Parameter> parentUriParameters)
        {
            var generatedMethods = webApiMethodsGenerator.GetMethods(resource, fullUrl, rootController, rootController.Name, parentUriParameters);

            foreach (var method in generatedMethods)
            {
                rootController.Methods.Add(method);
            }

            classesNames.Add(rootController.Name);
            classesObjectsRegistry.Add(CalculateClassKey("/"), rootController);
            classes.Add(rootController);
        }
		private void GetControllers(IList<ControllerObject> classes, 
			ICollection<string> classesNames, IDictionary<string, ControllerObject> classesObjectsRegistry)
		{
			var rootController = new ControllerObject { Name = "Home", PrefixUri = "/" };

			foreach (var resource in raml.Resources)
			{
				if (resource == null)
					continue;

				var fullUrl = GetUrl(string.Empty, resource.RelativeUri);

				// when the resource is a parameter dont generate a class but add it's methods and children to the parent
				if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}"))
				{
					var generatedMethods = webApiMethodsGenerator.GetMethods(resource, fullUrl, rootController, rootController.Name);
					foreach (var method in generatedMethods)
					{
						rootController.Methods.Add(method);
					}

					GetMethodsFromChildResources(resource.Resources, fullUrl, rootController);

					classesNames.Add(rootController.Name);
					classesObjectsRegistry.Add(CalculateClassKey("/"), rootController);
					classes.Add(rootController);
				}
				else
				{
					var controller = new ControllerObject
					{
						Name = GetUniqueObjectName(resource, null),
						PrefixUri = UrlGeneratorHelper.FixControllerRoutePrefix(fullUrl),
						Description = resource.Description,
					};

					var methods = webApiMethodsGenerator.GetMethods(resource, fullUrl, controller, controller.Name);
					foreach (var method in methods)
					{
						controller.Methods.Add(method);
					}

					classesNames.Add(controller.Name);
					classes.Add(controller);
					classesObjectsRegistry.Add(CalculateClassKey(fullUrl), controller);

					GetMethodsFromChildResources(resource.Resources, fullUrl, controller);
				}
			}
		}
		private ControllerMethod BuildControllerMethod(string url, Method method, Resource resource, ControllerObject parent)
		{
			var relativeUri = UrlGeneratorHelper.GetRelativeUri(url, parent.PrefixUri);

			return new ControllerMethod
			{
				Name = NetNamingMapper.GetMethodName(method.Verb ?? "Get" + resource.RelativeUri),
				Parameter = GetParameter(GeneratorServiceHelper.GetKeyForResource(method, resource), method, resource, url),
				UriParameters = uriParametersGenerator.GetUriParameters(resource, url),
				ReturnType = GetReturnType(GeneratorServiceHelper.GetKeyForResource(method, resource), method, resource, url),
				Comment = GetComment(resource, method),
				Url = relativeUri,
				Verb = NetNamingMapper.Capitalize(method.Verb),
				Parent = null,
				UseSecurity =
					raml.SecuredBy != null && raml.SecuredBy.Any() ||
					resource.Methods.Any(m => m.Verb == method.Verb && m.SecuredBy != null && m.SecuredBy.Any()),
				SecurityParameters = GetSecurityParameters(raml, method)
			};
		}
Beispiel #8
0
        private ControllerObject CreateControllerAndAddMethods(IList <ControllerObject> classes, ICollection <string> classesNames,
                                                               IDictionary <string, ControllerObject> classesObjectsRegistry, Resource resource, string fullUrl, IDictionary <string, Parameter> parentUriParameters)
        {
            var controller = new ControllerObject
            {
                Name        = GetUniqueObjectName(resource, null),
                PrefixUri   = UrlGeneratorHelper.FixControllerRoutePrefix(fullUrl),
                Description = resource.Description,
            };

            var methods = webApiMethodsGenerator.GetMethods(resource, fullUrl, controller, controller.Name, parentUriParameters);

            foreach (var method in methods)
            {
                controller.Methods.Add(method);
            }

            classesNames.Add(controller.Name);
            classes.Add(controller);
            classesObjectsRegistry.Add(CalculateClassKey(fullUrl), controller);
            return(controller);
        }
Beispiel #9
0
        private void GetMethodsFromChildResources(IEnumerable <Resource> resources, string url, ControllerObject parentController, IDictionary <string, Parameter> parentUriParameters)
        {
            if (resources == null)
            {
                return;
            }

            foreach (var resource in resources)
            {
                if (resource == null)
                {
                    continue;
                }

                var fullUrl = GetUrl(url, resource.RelativeUri);

                var methods = webApiMethodsGenerator.GetMethods(resource, fullUrl, parentController, parentController.Name, parentUriParameters);
                foreach (var method in methods)
                {
                    parentController.Methods.Add(method);
                }

                GetInheritedUriParams(parentUriParameters, resource);

                GetMethodsFromChildResources(resource.Resources, fullUrl, parentController, parentUriParameters);
            }
        }
		private void GetMethodsFromChildResources(IEnumerable<Resource> resources, string url, ControllerObject parentController)
		{
			if (resources == null)
				return;

			foreach (var resource in resources)
			{
				if (resource == null)
					continue;

				var fullUrl = GetUrl(url, resource.RelativeUri);

				var methods = webApiMethodsGenerator.GetMethods(resource, fullUrl, parentController, parentController.Name);
				foreach (var method in methods)
				{
					parentController.Methods.Add(method);
				}

				GetMethodsFromChildResources(resource.Resources, fullUrl, parentController);
			}
		}
        private void GetMethodsFromChildResources(IEnumerable <Resource> resources, string url, ControllerObject parentController)
        {
            if (resources == null)
            {
                return;
            }

            foreach (var resource in resources)
            {
                if (resource == null)
                {
                    continue;
                }

                var fullUrl = GetUrl(url, resource.RelativeUri);

                var methods = webApiMethodsGenerator.GetMethods(resource, fullUrl, parentController, parentController.Name, schemaResponseObjects, schemaRequestObjects);
                foreach (var method in methods)
                {
                    parentController.Methods.Add(method);
                }

                GetMethodsFromChildResources(resource.Resources, fullUrl, parentController);
            }
        }
        private ControllerObject CreateControllerAndAddMethods(IList<ControllerObject> classes, ICollection<string> classesNames,
            IDictionary<string, ControllerObject> classesObjectsRegistry, Resource resource, string fullUrl, IDictionary<string, Parameter> parentUriParameters)
        {
            var controller = new ControllerObject
            {
                Name = GetUniqueObjectName(resource, null),
                PrefixUri = UrlGeneratorHelper.FixControllerRoutePrefix(fullUrl),
                Description = resource.Description,
            };

            var methods = webApiMethodsGenerator.GetMethods(resource, fullUrl, controller, controller.Name, parentUriParameters);
            foreach (var method in methods)
            {
                controller.Methods.Add(method);
            }

            classesNames.Add(controller.Name);
            classes.Add(controller);
            classesObjectsRegistry.Add(CalculateClassKey(fullUrl), controller);
            return controller;
        }
        private void GetControllers(IList<ControllerObject> classes, 
            ICollection<string> classesNames, IDictionary<string, ControllerObject> classesObjectsRegistry, IDictionary<string, Parameter> parentUriParameters)
        {
            var rootController = new ControllerObject { Name = "Home", PrefixUri = "/" };

            foreach (var resource in raml.Resources)
            {
                if (resource == null)
                    continue;

                var fullUrl = GetUrl(string.Empty, resource.RelativeUri);

                // when the resource is a parameter dont generate a class but add it's methods and children to the parent
                if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}"))
                {
                    AddMethodsToRootController(classes, classesNames, classesObjectsRegistry, resource, fullUrl, rootController, parentUriParameters);

                    GetMethodsFromChildResources(resource.Resources, fullUrl, rootController, resource.UriParameters);
                }
                else
                {
                    var controller = CreateControllerAndAddMethods(classes, classesNames, classesObjectsRegistry, resource, fullUrl, parentUriParameters);

                    GetMethodsFromChildResources(resource.Resources, fullUrl, controller, resource.UriParameters);
                }
            }
        }