private IEnumerable <CodeGenClass> GenerateModels(ApiDefinition definition) { foreach (ApiModel model in definition.Models) { if (string.IsNullOrWhiteSpace(model.Name)) { throw new ArgumentException("Invalid model name"); } var modelClass = new CodeGenClass( model.Name, Scope.Public, ClassType.Normal); modelClass.Comment = new CodeGenComment(model.Description); foreach (ApiType prop in model.Props) { if (string.IsNullOrWhiteSpace(prop.Name)) { throw new ArgumentException("Invalid property name"); } if (string.IsNullOrWhiteSpace(prop.Type)) { throw new ArgumentException("Invalid property type"); } var propProp = new CodeGenProperty( LowerToUpperCamel(prop.Name), ResolveModelType(prop.Type), Scope.Public, true); propProp.Comment = new CodeGenComment(prop.Description); // TODO required modelClass.Properties.Add(propProp); } yield return(modelClass); } }
private CodeGenClass GenerateController(ApiDefinition definition) { if (definition is null) { throw new ArgumentNullException(nameof(definition)); } var controllerClass = new CodeGenClass( $"{definition.Name}Controller", Scope.Public, ClassType.Normal, derivedFrom: new[] { "ControllerBase" }); // class comment if (!string.IsNullOrWhiteSpace(definition.Description)) { controllerClass.Comment = new CodeGenComment(definition.Description); } // ApiController attribute controllerClass.Attributes.Add(new CodeGenAttribute("ApiController")); // Route attribute string controllerRoute = !string.IsNullOrWhiteSpace(definition.ControllerRoute) ? $"\"{definition.ControllerRoute}\"" : @"""[controller]"""; controllerClass.Attributes.Add(new CodeGenAttribute("Route", controllerRoute)); // Constructor and DI injected variables. var constructorStatements = new List <string>(); var constructorParam = new List <string>(); var constructorParamComments = new Dictionary <string, string>(); constructorParam.Add("IServiceProvider serviceProvider"); constructorParamComments.Add("serviceProvider", "The service provider to access validators and handlers."); constructorStatements.Add($"_ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider))"); controllerClass.Variables.Add(new CodeGenVariable( "_ServiceProvider", "IServiceProvider", Scope.Private, readOnly: true)); //foreach (ApiMethod method in definition.Methods) //{ // if (string.IsNullOrWhiteSpace(method.Name)) throw new ArgumentException("Invalid dependency name"); // string methodValidatorName = $"{method.Name}Validator"; // string methodHandlerName = $"{method.Name}Handler"; // constructorParam.Add($"{methodValidatorName} {methodValidatorName}"); // constructorParam.Add($"{methodHandlerName} {methodHandlerName}"); // constructorStatements.Add($"this.{methodValidatorName} = {methodValidatorName};"); // constructorStatements.Add($"this.{methodHandlerName} = {methodHandlerName};"); // constructorParamComments.Add(methodValidatorName, $"Validator for the {method.Name} method."); // constructorParamComments.Add(methodHandlerName, $"Handler for the {method.Name} method."); // controllerClass.Variables.Add(new CodeGenVariable( // dependency.Name, // dependency.Type, // Scope.Private)); //} foreach (ApiMethod apiMethod in definition.Methods) { controllerClass.Methods.Add(GenerateMethod(apiMethod)); } var constructor = new CodeGenConstructor( controllerClass.Name, Scope.Public, constructorParam, string.Join(Environment.NewLine, constructorStatements)); constructor.Comment = new CodeGenComment( "Constructor for this controller.", paramComments: constructorParamComments); controllerClass.Constructors.Add(constructor); return(controllerClass); }