Introducing service interface and some validation.

This commit is contained in:
Marcin Stefaniuk 2016-05-16 16:25:17 +02:00
parent 37e76f4c68
commit 874509a592
8 changed files with 39 additions and 49 deletions

View File

@ -122,6 +122,6 @@ public class NancyFXServerCodegen extends AbstractCSharpCodegen {
}
// Converts, for example, PUT to HttpPut for controller attributes
operation.httpMethod = "Http" + operation.httpMethod.substring(0, 1) + operation.httpMethod.substring(1).toLowerCase();
operation.httpMethod = operation.httpMethod.substring(0, 1) + operation.httpMethod.substring(1).toLowerCase();
}
}

View File

@ -1,45 +1,44 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Newtonsoft.Json;
using Swashbuckle.SwaggerGen.Annotations;
using Nancy;
using {{packageName}}.Models;
namespace {{packageName}}.Controllers
namespace {{packageName}}.Api
{ {{#operations}}
/// <summary>
/// {{description}}
/// </summary>{{#description}}{{#basePath}}
[Route("{{basePath}}")]
{{/basePath}}[Description("{{description}}")]{{/description}}
public class {{classname}}Controller : Controller
{ {{#operation}}
/// <summary>
/// {{#summary}}{{summary}}{{/summary}}
/// </summary>
{{#notes}}/// <remarks>{{notes}}</remarks>{{/notes}}{{#allParams}}
/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}{{#responses}}
/// <response code="{{code}}">{{message}}</response>{{/responses}}
[{{httpMethod}}]
[Route("{{path}}")]
[SwaggerOperation("{{operationId}}")]{{#returnType}}
[SwaggerResponse(200, type: typeof({{&returnType}}))]{{/returnType}}
public {{#returnType}}IActionResult{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{ {{#returnType}}
string exampleJson = null;
{{#isListCollection}}{{>listReturn}}{{/isListCollection}}{{^isListCollection}}{{#isMapContainer}}{{>mapReturn}}{{/isMapContainer}}{{^isMapContainer}}{{>objectReturn}}{{/isMapContainer}}{{/isListCollection}}
{{!TODO: defaultResponse, examples, auth, consumes, produces, nickname, externalDocs, imports, security}}
return new ObjectResult(example);{{/returnType}}{{^returnType}}
throw new NotImplementedException();{{/returnType}}
public sealed class {{classname}}Module : NancyModule
{
public {{classname}}Module({{classname}}Service service) : base("")
{ {{#operation}}
{{httpMethod}}["{{path}}"] = parameters =>
{
// existence validation of obligatory parameters
{{#allParams}}{{#required}}
if (parameters.{{paramName}} == null) {
throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}");
}
{{/required}}{{/allParams}}
{{#allParams}}{{#isBodyParam}}
{{&dataType}} {{paramName}} = Bind<{{&dataType}}>();
{{/isBodyParam}}{{#isPathParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}};
{{/isPathParam}}{{#isHeaderParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}};
{{/isHeaderParam}}{{#isQueryParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}};
{{/isQueryParam}}{{/allParams}}
return service.{{operationId}}(
{{#allParams}}
{{paramName}}{{#hasMore}},{{/hasMore}}
{{/allParams}}
);
};
{{/operation}}
}
}
interface {{classname}}Service
{ {{#operation}}
public {{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{/operation}}
}
{{/operations}}
}

View File

@ -1 +1 @@
{{#isBodyParam}}[FromBody]{{&dataType}} {{paramName}}{{/isBodyParam}}
{{#isBodyParam}}{{&dataType}} {{paramName}}{{/isBodyParam}}

View File

@ -1 +1 @@
{{#isFormParam}}[FromForm]{{&dataType}} {{paramName}}{{/isFormParam}}
{{#isFormParam}}{{&dataType}} {{paramName}}{{/isFormParam}}

View File

@ -1 +1 @@
{{#isHeaderParam}}[FromHeader]{{&dataType}} {{paramName}}{{/isHeaderParam}}
{{#isHeaderParam}}{{&dataType}} {{paramName}}{{/isHeaderParam}}

View File

@ -69,15 +69,6 @@ namespace {{packageName}}.Models
return sb.ToString();
}
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public {{#parent}} new {{/parent}}string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
/// <summary>
/// Returns true if objects are equal
/// </summary>

View File

@ -1 +1 @@
{{#isPathParam}}[FromRoute]{{&dataType}} {{paramName}}{{/isPathParam}}
{{#isPathParam}}{{&dataType}} {{paramName}}{{/isPathParam}}

View File

@ -1 +1 @@
{{#isQueryParam}}[FromQuery]{{&dataType}} {{paramName}}{{/isQueryParam}}
{{#isQueryParam}}{{&dataType}} {{paramName}}{{/isQueryParam}}