genericdatabaseapiservice/regular.cs.tmpl

181 lines
5.5 KiB
Cheetah
Raw Normal View History

2021-11-18 19:27:39 +01:00
$GENERATED_CS_COMMENT
2021-11-25 14:49:08 +01:00
2021-11-18 19:27:39 +01:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
2021-11-25 14:49:08 +01:00
using System.Runtime.Serialization;
2021-11-18 19:27:39 +01:00
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;
2021-11-18 19:27:39 +01:00
namespace ${env['packagename']}.Implementations
{
2021-11-25 14:49:08 +01:00
#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 #slurp
#if $inputType['isList']
List<#slurp
#end if
$inputType['type']#slurp
#if $inputType['isList']
>#slurp
#end if
$inputType['csName'] { get; set; }
2021-11-25 14:49:08 +01:00
#end for
}
#end if
#end for
2021-11-18 19:27:39 +01:00
public class RegularApiImplementation : RegularApiController
{
private readonly IDbService _dbService;
public RegularApiImplementation(IDbService dbService) {
_dbService = dbService;
}
2021-11-18 19:27:39 +01:00
#for $operation in $operations
2021-11-19 12:39:46 +01:00
#if $operation['method'] == 'get'
2021-11-25 14:49:08 +01:00
public override async Task<IActionResult> ${operation['func']}(#slurp
#if $operation['bodyInputType']
[FromBody]$operation['bodyInputType']['csName'] $operation['bodyInputType']['apiName'] #slurp
2021-11-19 12:39:46 +01:00
#end if
2021-11-25 14:49:08 +01:00
#if $operation['paramInputTypes']
#set sep = ''
#for $inputType in $operation['paramInputTypes']
$sep#slurp
#if $inputType['in'] == 'query'
[FromQuery #slurp
#elif $inputType['in'] == 'path'
2021-11-25 14:54:18 +01:00
[FromRoute #slurp
2021-11-25 14:49:08 +01:00
#else
#raise Exception('Invalid in for paramInput')
#end if
(Name = "$inputType['name']")]#slurp
#if $inputType['required']
[Required()]#slurp
#end if
#if $inputType['isList']
List<#slurp
#end if
$inputType['type']#slurp
#if $inputType['isList']
>#slurp
#end if
$inputType['name']#slurp
2021-11-25 14:49:08 +01:00
#set sep = ', '
#end for
#end if
) {
2021-11-25 14:49:08 +01:00
#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']
2021-11-25 14:49:08 +01:00
$operation['bodyInputType']['csName'], #slurp
#elif $operation['paramInputTypes']
2021-11-25 14:49:08 +01:00
${operation['func']}InputType, #slurp
#else
2021-11-25 14:49:08 +01:00
Object, #slurp
#end if
2021-12-06 17:46:58 +01:00
$operation['resultType']['csName']>(#slurp
"$operation['databaseTag']", "#slurp
#if not $operation['statement']
2021-11-25 14:49:08 +01:00
SELECT #slurp
#set $sep = ""
#for $property in $types[$operation['resultType']['apiName']]['properties']
2021-11-25 14:49:08 +01:00
$sep$property['sqlName'] #slurp
#set $sep = ","
#end for
2021-11-25 14:49:08 +01:00
FROM $types[$operation['resultType']['apiName']]['sqlName']#slurp
#else
2021-11-25 14:49:08 +01:00
$operation['statement']#slurp
#end if
2021-11-30 19:14:31 +01:00
", #slurp
#if $operation['isList']
false, #slurp
2021-11-30 19:16:32 +01:00
#else
true, #slurp
2021-11-30 19:14:31 +01:00
#end if
#if $operation['bodyInputType']
2021-11-25 14:49:08 +01:00
$operation['bodyInputType']['apiName']#slurp
#elif $operation['paramInputTypes']
2021-11-25 14:49:08 +01:00
paramInput#slurp
#else
2021-11-25 14:49:08 +01:00
null#slurp
#end if
, #slurp
#if $operation['bindingByStringReplacement']
true#slurp
#else
false#slurp
#end if
2021-11-25 14:49:08 +01:00
);
2021-11-30 19:09:22 +01:00
return new ObjectResult(res#slurp
2021-11-30 19:20:24 +01:00
#if not $operation['isList']
2021-11-30 19:09:22 +01:00
[0]#slurp
#end if
);
2021-12-02 11:59:10 +01:00
\#pragma warning disable CS0168
#for $errDef in $Exceptions
} catch ($errDef['Exception'] ex) {
2021-12-02 09:47:48 +01:00
var err = new ErrorResultObject();
2021-12-02 11:59:10 +01:00
err.ErrorCode = $errDef['ErrorCode'];
err.ServiceErrorCode = $errDef['ServiceErrorCode'];
err.ErrorMessage = "$errDef['ErrorMessage']";
2021-12-02 11:59:10 +01:00
err.ErrorInfoURL = "$errDef['ErrorInfoURL']";
err.CaughtException = ex.ToString();
err.CaughtExceptionMessage = ex.Message;
2021-12-02 11:59:10 +01:00
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
2021-11-18 19:27:39 +01:00
}
2021-11-19 12:39:46 +01:00
#elif $operation['method'] == 'post'
// INSERT
#elif $operation['method'] == 'put'
// UPDATE
#elif $operation['method'] == 'delete'
// DELETE
2021-11-18 19:27:39 +01:00
2021-11-19 12:39:46 +01:00
#else
#raise Exception('invalid method')
#end if
#end for
2021-11-18 19:27:39 +01:00
}
}