private void RenderMethod(MethodElement method) { this.DocumentMethod(method); this.output.Write("service."); this.output.Write(method.Name.Camelize()); this.output.Write(" = function("); this.output.Write(string.Join(", ", method.GetAllParameters().Select(_ => _.Name))); this.output.WriteLine(") {"); this.output.PushIndent(Tab); this.output.Write("return $http."); this.output.Write(method.HttpMethod.ToString().ToLower()); this.output.Write("(utilities.fixUri(apiBaseUri + "); this.output.Write(AngularUtility.GetRouteBuilder(method)); this.output.Write(")"); if (method.BodyParameter != null) { this.output.Write(", "); if (method.BodyParameter.Type == typeof(string)) { this.output.Write("JSON.stringify("); this.output.Write(method.BodyParameter.Name); this.output.Write(")"); } else { this.output.Write(method.BodyParameter.Name); } } this.output.WriteLine(").catch(function(response) {"); this.output.PushIndent(Tab); this.output.WriteLine("return $q.reject(utilities.getHttpError(response));"); this.output.PopIndent(); this.output.WriteLine("});"); this.output.PopIndent(); this.output.WriteLine("};"); this.output.WriteLine(string.Empty); }
private void RenderMethod(MethodElement method) { var parameterNames = method.GetAllParameters().Select(_ => _.Name).ToArray(); this.output.WriteLine(string.Empty); this.output.WriteLine(string.Format("it('should {0}', function() {{", method.Name.Humanize(LetterCasing.LowerCase))); this.output.PushIndent(Tab); for (var i = 0; i < method.UrlParameters.Count; i++) { var parameter = method.UrlParameters[i]; if (parameter.Type.IsPrimitiveEx()) { this.output.WriteLine(string.Format("var {0} = 'value{1}';", parameterNames[i], i)); } else { this.output.WriteLine(string.Format("var {0} = {{", parameterNames[i])); var flattenedComplexType = parameter.Type.GetProperties().Where(_ => _.PropertyType.IsPrimitiveEx() && _.CanWrite); int propertyIndex = 0; foreach (var property in flattenedComplexType) { this.output.WriteLine(string.Format(" {0}: 'value{1}-{2}',", property.Name.Camelize(), i, propertyIndex++)); } this.output.WriteLine("};"); } } if (method.BodyParameter != null) { this.output.WriteLine(string.Format("var {0} = 'value{1}';", method.BodyParameter.Name, "-body")); } this.output.WriteLine(string.Empty); this.output.WriteLine("var responseData = 'response data';"); this.output.Write("$httpBackend.expect"); this.output.Write(method.HttpMethod.ToString().ToUpper()); this.output.Write("(utilities.fixUri(fifthweekConstants.apiBaseUri + "); this.output.Write(AngularUtility.GetRouteBuilder(method)); if (method.BodyParameter != null) { this.output.Write(", "); if (method.BodyParameter.Type == typeof(string)) { this.output.Write("JSON.stringify("); this.output.Write(method.BodyParameter.Name); this.output.Write(")"); } else { this.output.Write(method.BodyParameter.Name); } } this.output.WriteLine(")).respond(200, responseData);"); this.output.WriteLine(string.Empty); var actualParameters = string.Join(", ", parameterNames); this.output.WriteLine("var result = null;"); this.output.WriteLine(string.Format("target.{0}({1}).then(function(response) {{ result = response.data; }});", method.Name.Camelize(), actualParameters)); this.output.WriteLine(string.Empty); this.output.WriteLine("$httpBackend.flush();"); this.output.WriteLine("$rootScope.$apply();"); this.output.WriteLine(string.Empty); this.output.WriteLine("expect(result).toBe(responseData);"); this.output.PopIndent(); this.output.WriteLine("});"); }