From 2be22bbdd15f96fd8be2912c3178c2cc829e53be Mon Sep 17 00:00:00 2001 From: Sergei Korolev Date: Wed, 12 Jan 2022 17:20:42 -0500 Subject: [PATCH] Generate RandomValueController when random value option is set --- .../CSharp/.template.config/template.json | 6 ++++ .../Controllers/RandomValueController.cs | 33 +++++++++++++++++++ .../FSharp/.template.config/template.json | 6 ++++ .../FSharp/Company.WebApplication.FS.fsproj | 3 ++ .../Controllers/RandomValueController.fs | 23 +++++++++++++ .../ConfigurationRandomValueOptionTest.cs | 19 +++++++++++ 6 files changed, 90 insertions(+) create mode 100644 src/Content/NetCoreTool.Template.WebApi/CSharp/Controllers/RandomValueController.cs create mode 100644 src/Content/NetCoreTool.Template.WebApi/FSharp/Controllers/RandomValueController.fs diff --git a/src/Content/NetCoreTool.Template.WebApi/CSharp/.template.config/template.json b/src/Content/NetCoreTool.Template.WebApi/CSharp/.template.config/template.json index 98001ab..0f8518e 100644 --- a/src/Content/NetCoreTool.Template.WebApi/CSharp/.template.config/template.json +++ b/src/Content/NetCoreTool.Template.WebApi/CSharp/.template.config/template.json @@ -43,6 +43,12 @@ "exclude": [ "Dockerfile" ] + }, + { + "condition": "(!ConfigurationRandomValueOption)", + "exclude": [ + "Controllers/RandomValueController.cs" + ] } ] } diff --git a/src/Content/NetCoreTool.Template.WebApi/CSharp/Controllers/RandomValueController.cs b/src/Content/NetCoreTool.Template.WebApi/CSharp/Controllers/RandomValueController.cs new file mode 100644 index 0000000..0850b9f --- /dev/null +++ b/src/Content/NetCoreTool.Template.WebApi/CSharp/Controllers/RandomValueController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; + +namespace Company.WebApplication.CS.Controllers +{ + [ApiController] + [Route("[controller]")] + public class RandomValueController : ControllerBase + { + private IConfiguration _config; + + public RandomValueController(IConfiguration config) + { + _config = config; + } + + [HttpGet] + public dynamic Index() + { + return new + { + intVal = _config["random:int"], + longVal = _config["random:long"], + int10 = _config["random:int(10)"], + long10 = _config["random:long(100)"], + int10_20 = _config["random:int(10,20)"], + long100_200 = _config["random:long(100,200)"], + uuid = _config["random:uuid"], + stringVal = _config["random:string"] + }; + } + } +} diff --git a/src/Content/NetCoreTool.Template.WebApi/FSharp/.template.config/template.json b/src/Content/NetCoreTool.Template.WebApi/FSharp/.template.config/template.json index 3072893..d922a0e 100644 --- a/src/Content/NetCoreTool.Template.WebApi/FSharp/.template.config/template.json +++ b/src/Content/NetCoreTool.Template.WebApi/FSharp/.template.config/template.json @@ -43,6 +43,12 @@ "exclude": [ "Dockerfile" ] + }, + { + "condition": "(!ConfigurationRandomValueOption)", + "exclude": [ + "Controllers/RandomValueController.fs" + ] } ] } diff --git a/src/Content/NetCoreTool.Template.WebApi/FSharp/Company.WebApplication.FS.fsproj b/src/Content/NetCoreTool.Template.WebApi/FSharp/Company.WebApplication.FS.fsproj index 6427b31..e2d2320 100644 --- a/src/Content/NetCoreTool.Template.WebApi/FSharp/Company.WebApplication.FS.fsproj +++ b/src/Content/NetCoreTool.Template.WebApi/FSharp/Company.WebApplication.FS.fsproj @@ -52,6 +52,9 @@ + + + diff --git a/src/Content/NetCoreTool.Template.WebApi/FSharp/Controllers/RandomValueController.fs b/src/Content/NetCoreTool.Template.WebApi/FSharp/Controllers/RandomValueController.fs new file mode 100644 index 0000000..6f5dc40 --- /dev/null +++ b/src/Content/NetCoreTool.Template.WebApi/FSharp/Controllers/RandomValueController.fs @@ -0,0 +1,23 @@ +namespace Company.WebApplication.FS.Controllers + +open Microsoft.AspNetCore.Mvc +open Microsoft.Extensions.Configuration +open Microsoft.Extensions.Logging + +type RandomValue = {intVal : int; longVal: int64; int10: int; long10: int64; int10_20: int; long100_200: int64; uuid: string; stringVal: string} + +[] +[] +type RandomValueController (logger : ILogger, _config: IConfiguration) = + inherit ControllerBase() + + [] + member _.Get() = + { intVal = int _config.["random:int"] + longVal = int64 _config.["random:long"] + int10 = int _config.["random:int(10)"] + long10 = int64 _config.["random:long(100)"] + int10_20 = int _config.["random:int(10,20)"] + long100_200 = int64 _config.["random:long(100,200)"] + uuid = _config.["random:uuid"] + stringVal = _config.["random:string"] } diff --git a/test/NetCoreTool.Template.WebApi.Test/ConfigurationRandomValueOptionTest.cs b/test/NetCoreTool.Template.WebApi.Test/ConfigurationRandomValueOptionTest.cs index f322387..22e5fb4 100644 --- a/test/NetCoreTool.Template.WebApi.Test/ConfigurationRandomValueOptionTest.cs +++ b/test/NetCoreTool.Template.WebApi.Test/ConfigurationRandomValueOptionTest.cs @@ -1,5 +1,8 @@ using System.Collections.Generic; +using System.Threading.Tasks; +using FluentAssertions; using Steeltoe.NetCoreTool.Template.WebApi.Test.Models; +using Xunit; using Xunit.Abstractions; namespace Steeltoe.NetCoreTool.Template.WebApi.Test @@ -21,5 +24,21 @@ protected override void AssertProgramSnippetsHook(ProjectOptions options, List