genericdatabaseapiservice/regular.cs.tmpl

164 lines
5.1 KiB
Cheetah

$GENERATED_CS_COMMENT
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Microsoft.AspNetCore.Authorization;
using Swashbuckle.AspNetCore.SwaggerGen;
using Newtonsoft.Json;
using ${env['packagename']}.Attributes;
using ${env['packagename']}.Models;
using ${env['packagename']}.Controllers;
using ${env['packagename']}.Services;
namespace ${env['packagename']}.Implementations
{
#for $operation in $operations
#if $operation['paramInputTypes']
[DataContract]
public class ${operation['func']}InputType
{
#for $inputType in $operation['paramInputTypes']
#if $inputType['required']
[Required]
#end if
[DataMember(Name="$inputType['name']")]
public $inputType['type'] $inputType['csName'] { get; set; }
#end for
}
#end if
#end for
public class RegularApiImplementation : RegularApiController
{
private readonly IDbService _dbService;
public RegularApiImplementation(IDbService dbService) {
_dbService = dbService;
}
#for $operation in $operations
#if $operation['method'] == 'get'
public override async Task<IActionResult> ${operation['func']}(#slurp
#if $operation['bodyInputType']
[FromBody]$operation['bodyInputType']['csName'] $operation['bodyInputType']['apiName'] #slurp
#end if
#if $operation['paramInputTypes']
#set sep = ''
#for $inputType in $operation['paramInputTypes']
$sep#slurp
#if $inputType['in'] == 'query'
[FromQuery #slurp
#elif $inputType['in'] == 'path'
[FromRoute #slurp
#else
#raise Exception('Invalid in for paramInput')
#end if
(Name = "$inputType['name']")]#slurp
#if $inputType['required']
[Required()]#slurp
#end if
$inputType['type'] $inputType['name']#slurp
#set sep = ', '
#end for
#end if
) {
#if $operation['paramInputTypes']
${operation['func']}InputType paramInput = new ${operation['func']}InputType();
#for $inputType in $operation['paramInputTypes']
paramInput.$inputType['csName'] = $inputType['name'];
#end for
#end if
try {
List<$operation['resultType']['csName']> res = await _dbService.ReadBySelect<#slurp
#if $operation['bodyInputType']
$operation['bodyInputType']['csName'], #slurp
#elif $operation['paramInputTypes']
${operation['func']}InputType, #slurp
#else
Object, #slurp
#end if
$operation['resultType']['csName']>(#slurp
"$operation['databaseTag']", "#slurp
#if not $operation['statement']
SELECT #slurp
#set $sep = ""
#for $property in $types[$operation['resultType']['apiName']]['properties']
$sep$property['sqlName'] #slurp
#set $sep = ","
#end for
FROM $types[$operation['resultType']['apiName']]['sqlName']#slurp
#else
$operation['statement']#slurp
#end if
", #slurp
#if $operation['isList']
false, #slurp
#else
true, #slurp
#end if
#if $operation['bodyInputType']
$operation['bodyInputType']['apiName']#slurp
#elif $operation['paramInputTypes']
paramInput#slurp
#else
null#slurp
#end if
);
return new ObjectResult(res#slurp
#if not $operation['isList']
[0]#slurp
#end if
);
\#pragma warning disable CS0168
#for $errDef in $Exceptions
} catch ($errDef['Exception'] ex) {
var err = new ErrorResultObject();
err.ErrorCode = $errDef['ErrorCode'];
err.ServiceErrorCode = $errDef['ServiceErrorCode'];
err.ErrorMessage = #slurp
#if $errDef['ErrorMessage'] == 'INSERT_EXCEPTION_MESSAGE'
ex.ToString();
#else
"$errDef['ErrorMessage']";
#end if
err.ErrorInfoURL = "$errDef['ErrorInfoURL']";
err.OffensiveData = #slurp
#if $operation['bodyInputType']
Newtonsoft.Json.JsonConvert.SerializeObject($operation['bodyInputType']['apiName'], Newtonsoft.Json.Formatting.Indented);
#elif $operation['paramInputTypes']
Newtonsoft.Json.JsonConvert.SerializeObject(paramInput, Newtonsoft.Json.Formatting.Indented);
#else
null;
#end if
return StatusCode($errDef['ErrorCode'], err);
#end for
}
\#pragma warning restore CS0168
}
#elif $operation['method'] == 'post'
// INSERT
#elif $operation['method'] == 'put'
// UPDATE
#elif $operation['method'] == 'delete'
// DELETE
#else
#raise Exception('invalid method')
#end if
#end for
}
}