All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csharp-nancyfx.api.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
using System;
using Nancy;
using Nancy.ModelBinding;
using System.Collections.Generic;
using Sharpility.Base;
using {{packageName}}.{{packageContext}}.Models;
using {{packageName}}.{{packageContext}}.Utils;
using NodaTime;{{#asyncServer}}
using System.Threading.Tasks;{{/asyncServer}}

namespace {{packageName}}.{{packageContext}}.Modules
{ {{#operations}}{{#operation}}{{#allParams}}{{#isEnum}}
    {{>innerApiEnum}}{{/isEnum}}{{/allParams}}{{/operation}}

    /// 
    /// Module processing requests of {{classname}} domain.
    /// 
    public sealed class {{classname}}Module : NancyModule
    {
        /// 
        /// Sets up HTTP methods mappings.
        /// 
        /// Service handling requests
        public {{classname}}Module({{interfacePrefix}}{{classname}}Service service) : base("{{{baseContext}}}")
        { {{#operation}}
            {{httpMethod}}["{{{path}}}"{{#asyncServer}}, true{{/asyncServer}}] = {{#asyncServer}}async (parameters, ct){{/asyncServer}}{{^asyncServer}}parameters{{/asyncServer}} =>
            {
                {{#allParams}}{{#isBodyParam}}var {{paramName}} = this.Bind<{{&dataType}}>();{{/isBodyParam}}{{^isBodyParam}}{{#isEnum}}var {{paramName}} = Parameters.ValueOf<{{>innerApiEnumName}}?>({{>innerParameterValueOfArgs}});{{/isEnum}}{{^isEnum}}var {{paramName}} = Parameters.ValueOf<{{&dataType}}>({{>innerParameterValueOfArgs}});{{/isEnum}}{{^-last}}
                {{/-last}}{{/isBodyParam}}{{/allParams}}{{#allParams}}{{#required}}
                Preconditions.IsNotNull({{paramName}}, "Required parameter: '{{paramName}}' is missing at '{{operationId}}'");
                {{/required}}{{/allParams}}
                {{#returnType}}return {{/returnType}}{{#asyncServer}}await {{/asyncServer}}service.{{operationId}}(Context{{#allParams.0}}, {{/allParams.0}}{{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}){{#returnType}}{{#isArray}}.ToArray(){{/isArray}}{{/returnType}};{{^returnType}}
                return new Response { ContentType = "{{produces.0.mediaType}}"};{{/returnType}}
            };
{{/operation}}
        }
    }

    /// 
    /// Service handling {{classname}} requests.
    /// 
    public interface {{interfacePrefix}}{{classname}}Service
    {
        {{#operation}}/// 
        /// {{notes}}
        /// 
        /// Context of request
        {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}
        {{/allParams}}/// {{#returnType}}{{returnType}}{{/returnType}}
        {{#asyncServer}}{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}}{{/asyncServer}}{{^asyncServer}}{{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}{{/asyncServer}} {{operationId}}(NancyContext context{{#allParams.0}}, {{/allParams.0}}{{>paramsList}});{{^-last}}

        {{/-last}}{{/operation}}
    }

    /// 
    /// Abstraction of {{classname}}Service.
    /// 
    public abstract class Abstract{{classname}}Service: {{interfacePrefix}}{{classname}}Service
    {
        {{#operation}}public virtual {{#asyncServer}}{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}}{{/asyncServer}}{{^asyncServer}}{{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}{{/asyncServer}} {{operationId}}(NancyContext context{{#allParams.0}}, {{/allParams.0}}{{>paramsList}})
        {
            {{^asyncServer}}{{#returnType}}return {{/returnType}}{{/asyncServer}}{{#asyncServer}}return {{/asyncServer}}{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
        }{{^-last}}

        {{/-last}}{{/operation}}

        {{#operation}}protected abstract {{#asyncServer}}{{#returnType}}Task<{{{returnType}}}>{{/returnType}}{{^returnType}}Task{{/returnType}}{{/asyncServer}}{{^asyncServer}}{{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}{{/asyncServer}} {{operationId}}({{>paramsList}});{{^-last}}

        {{/-last}}{{/operation}}
    }

{{/operations}}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy