private static HttpActionDescriptor GetActionDescriptorByCurrentHttpVerb(HttpControllerContext controllerContext, DynamicApiControllerInfo controllerInfo) { //Check if there is only one action with the current http verb var actionsByVerb = controllerInfo.Actions.Values .Where(action => action.Verb.IsEqualTo(controllerContext.Request.Method)) .ToArray(); if (actionsByVerb.Length == 0) { throw new OwException( "There is no action" + " defined for api controller " + controllerInfo.ServiceName + " with an http verb: " + controllerContext.Request.Method ); } if (actionsByVerb.Length > 1) { throw new OwException( "There are more than one action" + " defined for api controller " + controllerInfo.ServiceName + " with an http verb: " + controllerContext.Request.Method ); } //Return the single action by the current http verb return new DynamicHttpActionDescriptor(controllerContext.ControllerDescriptor, actionsByVerb[0].Method, actionsByVerb[0].Filters); }
public string GenerateFor(DynamicApiControllerInfo controllerInfo) { var script = new StringBuilder(); //Module dependencies and start script.AppendLine("define(['jquery'], function ($) {"); //all methods foreach (var methodInfo in controllerInfo.Actions.Values) { script.AppendLine(); AppendMethod(script, controllerInfo, methodInfo); } //Return value of the module script.AppendLine(); script.AppendLine(" return {"); var methodNo = 0; foreach (var methodInfo in controllerInfo.Actions.Values) { script.AppendLine(" " + methodInfo.ActionName.ToCamelCase() + ": " + methodInfo.ActionName.ToCamelCase() + ((methodNo++) < (controllerInfo.Actions.Count - 1) ? "," : "")); } script.AppendLine(" };"); script.AppendLine(); //Module end script.AppendLine("});"); return script.ToString(); }
public static string GenerateUrlWithParameters(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo actionInfo) { var baseUrl = "api/services/" + controllerInfo.ServiceName + "/" + actionInfo.ActionName; var primitiveParameters = actionInfo.Method .GetParameters() .Where(p => TypeHelper.IsPrimitiveIncludingNullable(p.ParameterType)) .ToArray(); if (!primitiveParameters.Any()) { return baseUrl; } var urlBuilder = new StringBuilder(baseUrl); if (!baseUrl.Contains("?")) { urlBuilder.Append("?"); } for (var i = 0; i < primitiveParameters.Length; i++) { var parameterInfo = primitiveParameters[i]; if (i > 0) { urlBuilder.Append("&"); } urlBuilder.Append(parameterInfo.Name.ToCamelCase() + "=' + escape(" + parameterInfo.Name.ToCamelCase() + ") + '"); } return urlBuilder.ToString(); }
/// <summary> /// 得到 HttpActionDescriptor /// </summary> /// <param name="controllerContext"></param> /// <param name="controllerInfo"></param> /// <param name="actionName"></param> /// <returns></returns> private HttpActionDescriptor GetActionByActionName(HttpControllerContext controllerContext, DynamicApiControllerInfo controllerInfo, string actionName) { DynamicApiActionInfo actionInfo; if (!controllerInfo.Actions.TryGetValue(actionName, out actionInfo)) { return base.SelectAction(controllerContext); } return new DynamicHttpActionDescriptor(controllerContext.ControllerDescriptor, actionInfo.Method, actionInfo.Filters); }
private static IScriptProxyGenerator CreateProxyGenerator(ProxyScriptType type, DynamicApiControllerInfo controllerInfo, bool amdModule) { switch (type) { case ProxyScriptType.JQuery: return new JQueryProxyGenerator(controllerInfo, amdModule); case ProxyScriptType.Angular: return new AngularProxyGenerator(controllerInfo); //TODO: Angular! default: throw new AbpException("Unknown ProxyScriptType: " + type); } }
private static ActionScriptWriter CreateActionScriptWriter(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { switch (methodInfo.Verb) { case HttpVerb.Get: return new GetActionScriptWriter(controllerInfo, methodInfo); case HttpVerb.Post: return new PostActionScriptWriter(controllerInfo, methodInfo); case HttpVerb.Put: return new PostActionScriptWriter(controllerInfo, methodInfo); case HttpVerb.Delete: return new DeleteActionScriptWriter(controllerInfo, methodInfo); default: throw new AbpException("This Http verb is not implemented: " + methodInfo.Verb); } }
public static ActionScriptProxyGenerator CreateActionScriptProxyGenerator(this HttpVerb verb, DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { switch (verb) { case HttpVerb.Get: return new HttpGetActionScriptProxyGenerator(controllerInfo, methodInfo); case HttpVerb.Post: return new HttpPostActionScriptProxyGenerator(controllerInfo, methodInfo); case HttpVerb.Put: return new HttpPutActionScriptProxyGenerator(controllerInfo, methodInfo); case HttpVerb.Delete: return new HttpDeleteActionScriptProxyGenerator(controllerInfo, methodInfo); default: throw new AbpException("This Http verb is not implemented: " + verb); } }
private HttpActionDescriptor GetActionDescriptorByActionName(HttpControllerContext controllerContext, DynamicApiControllerInfo controllerInfo, string actionName) { //Get action information by action name DynamicApiActionInfo actionInfo; if (!controllerInfo.Actions.TryGetValue(actionName, out actionInfo)) { throw new AbpException("There is no action " + actionName + " defined for api controller " + controllerInfo.ServiceName); } if (!actionInfo.Verb.IsEqualTo(controllerContext.Request.Method)) { throw new AbpException( "There is an action " + actionName + " defined for api controller " + controllerInfo.ServiceName + " but with a different HTTP Verb. Request verb is " + controllerContext.Request.Method + ". It should be " + actionInfo.Verb); } return new DynamicHttpActionDescriptor(_configuration, controllerContext.ControllerDescriptor, actionInfo.Method, actionInfo.Filters); }
public string Generate(DynamicApiControllerInfo controllerInfo, string servicePrefix) { if (_servicePrefix != servicePrefix) { //if there is a change in servicePrefix, we need to generate the types again _servicePrefix = servicePrefix; _typesToBeDone = new HashSet<Type>(); _doneTypes = new HashSet<Type>(); } _controllerInfo = controllerInfo; var script = new StringBuilder(); script.AppendLine(" interface " + _controllerInfo.ServiceName.Substring(_controllerInfo.ServiceName.IndexOf('/') + 1)); script.AppendLine(" {"); foreach (var methodInfo in _controllerInfo.Actions.Values) { PrepareInputParameterTypes(methodInfo.Method); List<Type> newTypes = new List<Type>(); var returnType = TypeScriptHelper.GetTypeContractName(methodInfo.Method.ReturnType, newTypes); this.AddNewTypesIfRequired(newTypes.ToArray()); if (returnType == "void") script.AppendLine(string.Format(" {0} ({1}): abp.IPromise; ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method))); else script.AppendLine(string.Format(" {0} ({1}): abp.IGenericPromise<{2}>; ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method), returnType)); } script.AppendLine(" }"); while (_typesToBeDone != null && _typesToBeDone.Count > 0) { Type type = _typesToBeDone.First(); script.AppendLine(GenerateTypeScript(type)); _doneTypes.Add(type); _typesToBeDone.RemoveWhere(x => x == type); } return script.ToString(); }
public string GenerateFor(DynamicApiControllerInfo controllerInfo, bool defineAmdModule = true) { var script = new StringBuilder(); script.AppendLine("(function(){"); script.AppendLine(); script.AppendLine(" var serviceNamespace = abp.utils.createNamespace(abp, 'services." + controllerInfo.ServiceName.Replace("/", ".") + "');"); script.AppendLine(); //generate all methods foreach (var methodInfo in controllerInfo.Actions.Values) { AppendMethod(script, controllerInfo, methodInfo); script.AppendLine(); } //generate amd module definition if (defineAmdModule) { script.AppendLine(" if(define && typeof define === 'function' && define.amd){"); script.AppendLine(" define(function (require, exports, module) {"); script.AppendLine(" return {"); var methodNo = 0; foreach (var methodInfo in controllerInfo.Actions.Values) { script.AppendLine(" " + methodInfo.ActionName.ToCamelCase() + ": serviceNamespace." + methodInfo.ActionName.ToCamelCase() + ((methodNo++) < (controllerInfo.Actions.Count - 1) ? "," : "")); } script.AppendLine(" };"); script.AppendLine(" });"); script.AppendLine(" }"); } script.AppendLine(); script.AppendLine("})();"); return script.ToString(); }
public JQueryProxyGenerator(DynamicApiControllerInfo controllerInfo, bool defineAmdModule = true) { _controllerInfo = controllerInfo; _defineAmdModule = defineAmdModule; }
public PostActionScriptWriter(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) : base(controllerInfo, methodInfo) { }
protected ActionScriptWriter(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { ControllerInfo = controllerInfo; MethodInfo = methodInfo; }
public string Generate(DynamicApiControllerInfo controllerInfo, string servicePrefix) { if (_servicePrefix != servicePrefix) { //if there is a change in servicePrefix, we need to generate the types again _servicePrefix = servicePrefix; _typesToBeDone = new HashSet<Type>(); _doneTypes = new HashSet<Type>(); } _controllerInfo = controllerInfo; var script = new StringBuilder(); script.AppendLine(" export class " + _controllerInfo.ServiceName.Substring(_controllerInfo.ServiceName.IndexOf('/') + 1)); script.AppendLine(" {"); script.AppendLine(" static $inject = ['$http'];"); script.AppendLine(" constructor(private $http: ng.IHttpService){"); script.AppendLine(" }"); foreach (var methodInfo in _controllerInfo.Actions.Values) { PrepareInputParameterTypes(methodInfo.Method); List<Type> newTypes = new List<Type>(); var returnType = TypeScriptHelper.GetTypeContractName(methodInfo.Method.ReturnType, newTypes); this.AddNewTypesIfRequired(newTypes.ToArray()); if (returnType == "void") { script.AppendLine(string.Format(" public {0} = function ({1}): abp.IPromise ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method))); script.AppendLine("{"); script.AppendLine(" var self = this;"); script.AppendLine(" return self.$http(angular.extend({"); script.AppendLine(" abp: true,"); script.AppendLine(" url: abp.appPath + '" + ActionScriptingHelper.GenerateUrlWithParameters(_controllerInfo, methodInfo) + "',"); script.AppendLine(" method: '" + methodInfo.Verb.ToString().ToUpper(CultureInfo.InvariantCulture) + "',"); if (methodInfo.Verb == HttpVerb.Get) { script.AppendLine(" params: " + ActionScriptingHelper.GenerateBody(methodInfo)); } else { script.AppendLine(" data: JSON.stringify(" + ActionScriptingHelper.GenerateBody(methodInfo) + ")"); } script.AppendLine(" }, httpParams));"); script.AppendLine("}"); } else { script.AppendLine(string.Format(" public {0} = function ({1}): abp.IGenericPromise<{2}> ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method), returnType)); script.AppendLine("{"); script.AppendLine(" var self = this;"); script.AppendLine(" return self.$http(angular.extend({"); script.AppendLine(" abp: true,"); script.AppendLine(" url: abp.appPath + '" + ActionScriptingHelper.GenerateUrlWithParameters(_controllerInfo, methodInfo) + "',"); script.AppendLine(" method: '" + methodInfo.Verb.ToString().ToUpper(CultureInfo.InvariantCulture) + "',"); if (methodInfo.Verb == HttpVerb.Get) { script.AppendLine(" params: " + ActionScriptingHelper.GenerateBody(methodInfo)); } else { script.AppendLine(" data: JSON.stringify(" + ActionScriptingHelper.GenerateBody(methodInfo) + ")"); } script.AppendLine(" }, httpParams));"); script.AppendLine("}"); } } script.AppendLine(" }"); script.AppendLine("angular.module('abp').service('abp.services." + _controllerInfo.ServiceName.Replace("/", ".") + "', abp.services." + _controllerInfo.ServiceName.Replace("/", ".")+");"); while (_typesToBeDone != null && _typesToBeDone.Count > 0) { Type type = _typesToBeDone.First(); script.AppendLine(GenerateTypeScript(type)); _doneTypes.Add(type); _typesToBeDone.RemoveWhere(x => x == type); } return script.ToString(); }
private static void AppendMethod(StringBuilder script, DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { var generator = new JQueryActionScriptGenerator(controllerInfo, methodInfo); script.AppendLine(generator.GenerateMethod()); }
public AngularProxyGenerator(DynamicApiControllerInfo controllerInfo) { _controllerInfo = controllerInfo; }
public HttpPutActionScriptProxyGenerator(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) : base(controllerInfo, methodInfo) { }
public JQueryProxyGenerator(DynamicApiControllerInfo controllerInfo, bool defineAmdModule = true) { _controllerInfo = controllerInfo; _defineAmdModule = defineAmdModule; }
private static void AppendMethod(StringBuilder script, DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { var generator = methodInfo.Verb.CreateActionScriptProxyGenerator(controllerInfo, methodInfo); script.AppendLine(generator.GenerateMethod()); }
private HttpActionDescriptor GetActionDescriptorByActionName(HttpControllerContext controllerContext, DynamicApiControllerInfo controllerInfo, string actionName) { //Get action information by action name DynamicApiActionInfo actionInfo; if (!controllerInfo.Actions.TryGetValue(actionName, out actionInfo)) { throw new AbpException("There is no action " + actionName + " defined for api controller " + controllerInfo.ServiceName); } if (actionInfo.Verb != controllerContext.Request.Method.ToHttpVerb()) { throw new HttpException( (int)HttpStatusCode.BadRequest, "There is an action " + actionName + " defined for api controller " + controllerInfo.ServiceName + " but with a different HTTP Verb. Request verb is " + controllerContext.Request.Method + ". It should be " + actionInfo.Verb ); } return(new DynamicHttpActionDescriptor(_configuration, controllerContext.ControllerDescriptor, actionInfo)); }
protected ActionScriptProxyGenerator(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { ControllerInfo = controllerInfo; ActionInfo = methodInfo; }
private static HttpActionDescriptor GetActionByActionName(HttpControllerContext controllerContext, DynamicApiControllerInfo controllerInfo, string actionName) { //Get action information by action name DynamicApiActionInfo actionInfo; if (!controllerInfo.Actions.TryGetValue(actionName, out actionInfo)) { throw new AbpException("There is no action " + actionName + " defined for api controller " + controllerInfo.ServiceName); } if (!actionInfo.Verb.IsEqualTo(controllerContext.Request.Method)) { throw new AbpException( "There is an action " + actionName + " defined for api controller " + controllerInfo.ServiceName + " but with a different HTTP Verb. Request verb is " + controllerContext.Request.Method + ". It should be " + actionInfo.Verb); } return(new DynamicHttpActionDescriptor(controllerContext.ControllerDescriptor, actionInfo.Method, actionInfo.Filters)); }
private static HttpActionDescriptor GetActionByCurrentHttpVerb(HttpControllerContext controllerContext, DynamicApiControllerInfo controllerInfo) { //Check if there is only one action with the current http verb var actionsByVerb = controllerInfo.Actions.Values .Where(action => action.Verb.IsEqualTo(controllerContext.Request.Method)) .ToArray(); if (actionsByVerb.Length == 0) { throw new AbpException( "There is no action" + " defined for api controller " + controllerInfo.ServiceName + " with an http verb: " + controllerContext.Request.Method ); } if (actionsByVerb.Length > 1) { throw new AbpException( "There are more than one action" + " defined for api controller " + controllerInfo.ServiceName + " with an http verb: " + controllerContext.Request.Method ); } //Return the single action by the current http verb return(new DynamicHttpActionDescriptor(controllerContext.ControllerDescriptor, actionsByVerb[0].Method, actionsByVerb[0].Filters)); }
public JQueryActionScriptGenerator(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo actionInfo) { _controllerInfo = controllerInfo; _actionInfo = actionInfo; }
private static void AppendMethod(StringBuilder script, DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { var generator = methodInfo.Verb.CreateActionScriptProxyGenerator(controllerInfo, methodInfo); script.AppendLine(generator.GenerateMethod()); }
public AngularProxyGenerator(DynamicApiControllerInfo controllerInfo) { _controllerInfo = controllerInfo; }
private HttpActionDescriptor GetActionDescriptorByCurrentHttpVerb(HttpControllerContext controllerContext, DynamicApiControllerInfo controllerInfo) { //Check if there is only one action with the current http verb var actionsByVerb = controllerInfo.Actions.Values .Where(action => action.Verb == controllerContext.Request.Method.ToHttpVerb()) .ToArray(); if (actionsByVerb.Length == 0) { throw new HttpException( (int)HttpStatusCode.NotFound, "There is no action" + " defined for api controller " + controllerInfo.ServiceName + " with an http verb: " + controllerContext.Request.Method ); } if (actionsByVerb.Length > 1) { throw new HttpException( (int)HttpStatusCode.InternalServerError, "There are more than one action" + " defined for api controller " + controllerInfo.ServiceName + " with an http verb: " + controllerContext.Request.Method ); } //Return the single action by the current http verb return(new DynamicHttpActionDescriptor(_configuration, controllerContext.ControllerDescriptor, actionsByVerb[0])); }
public AngularActionScriptWriter(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { _controllerInfo = controllerInfo; _actionInfo = methodInfo; }
public PostActionScriptWriter(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) : base(controllerInfo, methodInfo) { }
private static IScriptProxyGenerator CreateProxyGenerator(ProxyScriptType type, DynamicApiControllerInfo controllerInfo, bool amdModule) { switch (type) { case ProxyScriptType.JQuery: return(new JQueryProxyGenerator(controllerInfo, amdModule)); case ProxyScriptType.Angular: return(new AngularProxyGenerator(controllerInfo)); default: throw new AbpException("Unknown ProxyScriptType: " + type); } }
public AngularActionScriptWriter(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { _controllerInfo = controllerInfo; _actionInfo = methodInfo; }
public string Generate(DynamicApiControllerInfo controllerInfo, string servicePrefix) { if (_servicePrefix != servicePrefix) { //if there is a change in servicePrefix, we need to generate the types again _servicePrefix = servicePrefix; _typesToBeDone = new HashSet <Type>(); _doneTypes = new HashSet <Type>(); } _controllerInfo = controllerInfo; var script = new StringBuilder(); script.AppendLine(" export class " + _controllerInfo.ServiceName.Substring(_controllerInfo.ServiceName.IndexOf('/') + 1)); script.AppendLine(" {"); script.AppendLine(" static $inject = ['$http'];"); script.AppendLine(" constructor(private $http: ng.IHttpService){"); script.AppendLine(" }"); foreach (var methodInfo in _controllerInfo.Actions.Values) { PrepareInputParameterTypes(methodInfo.Method); List <Type> newTypes = new List <Type>(); var returnType = TypeScriptHelper.GetTypeContractName(methodInfo.Method.ReturnType, newTypes); this.AddNewTypesIfRequired(newTypes.ToArray()); if (returnType == "void") { script.AppendLine(string.Format(" public {0} = function ({1}): CodeZero.IPromise ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method))); script.AppendLine("{"); script.AppendLine(" var self = this;"); script.AppendLine(" return self.$http(angular.extend({"); script.AppendLine(" CodeZero: true,"); script.AppendLine(" url: CodeZero.appPath + '" + ActionScriptingHelper.GenerateUrlWithParameters(_controllerInfo, methodInfo) + "',"); script.AppendLine(" method: '" + methodInfo.Verb.ToString().ToUpper(CultureInfo.InvariantCulture) + "',"); if (methodInfo.Verb == HttpVerb.Get) { script.AppendLine(" params: " + ActionScriptingHelper.GenerateBody(methodInfo)); } else { script.AppendLine(" data: JSON.stringify(" + ActionScriptingHelper.GenerateBody(methodInfo) + ")"); } script.AppendLine(" }, httpParams));"); script.AppendLine("}"); } else { script.AppendLine(string.Format(" public {0} = function ({1}): CodeZero.IGenericPromise<{2}> ", methodInfo.ActionName.ToCamelCase(), GetMethodInputParameter(methodInfo.Method), returnType)); script.AppendLine("{"); script.AppendLine(" var self = this;"); script.AppendLine(" return self.$http(angular.extend({"); script.AppendLine(" CodeZero: true,"); script.AppendLine(" url: CodeZero.appPath + '" + ActionScriptingHelper.GenerateUrlWithParameters(_controllerInfo, methodInfo) + "',"); script.AppendLine(" method: '" + methodInfo.Verb.ToString().ToUpper(CultureInfo.InvariantCulture) + "',"); if (methodInfo.Verb == HttpVerb.Get) { script.AppendLine(" params: " + ActionScriptingHelper.GenerateBody(methodInfo)); } else { script.AppendLine(" data: JSON.stringify(" + ActionScriptingHelper.GenerateBody(methodInfo) + ")"); } script.AppendLine(" }, httpParams));"); script.AppendLine("}"); } } script.AppendLine(" }"); script.AppendLine("angular.module('CodeZero').service('CodeZero.services." + _controllerInfo.ServiceName.Replace("/", ".") + "', CodeZero.services." + _controllerInfo.ServiceName.Replace("/", ".") + ");"); while (_typesToBeDone != null && _typesToBeDone.Count > 0) { Type type = _typesToBeDone.First(); script.AppendLine(GenerateTypeScript(type)); _doneTypes.Add(type); _typesToBeDone.RemoveWhere(x => x == type); } return(script.ToString()); }
public JQueryActionScriptGenerator(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo actionInfo) { _controllerInfo = controllerInfo; _actionInfo = actionInfo; }
private static void AppendMethod(StringBuilder script, DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { var generator = new JQueryActionScriptGenerator(controllerInfo, methodInfo); script.AppendLine(generator.GenerateMethod()); }
public HttpGetActionScriptProxyGenerator(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) : base(controllerInfo, methodInfo) { }
protected ActionScriptProxyGenerator(DynamicApiControllerInfo controllerInfo, DynamicApiActionInfo methodInfo) { ControllerInfo = controllerInfo; ActionInfo = methodInfo; }