├── .dockerignore ├── .gitignore ├── LICENSE ├── README.md ├── miniservice.sln └── src └── Company.Application.TodoWebApi ├── .dockerignore ├── .template.config └── template.json ├── Company.Application.TodoWebApi.csproj ├── Dockerfile ├── Domain └── Todo.cs ├── Dtos └── TodoDto.cs ├── Extensions └── TodoExtensions.cs ├── Infrastructure └── Db │ └── TodoDbContext.cs ├── Program.cs ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── v1 ├── HealthController.cs ├── HomeController.cs ├── TodoController.cs └── UseCases ├── AddTodo ├── Payloads.cs └── RequestHandler.cs ├── ClearTodos ├── Payloads.cs └── RequestHandler.cs ├── DeleteTodo ├── Payloads.cs └── RequestHandler.cs ├── GetTodo ├── Payloads.cs └── RequestHandler.cs ├── GetTodos ├── Payloads.cs └── RequestHandler.cs └── UpdateTodo ├── Payloads.cs └── RequestHandler.cs /.dockerignore: -------------------------------------------------------------------------------- 1 | .dockerignore 2 | .env 3 | .git 4 | .gitignore 5 | .vs 6 | .vscode 7 | */bin 8 | */obj 9 | **/.toolstarget -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Cloud Native .NET Core 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dotnet-new-miniservice 2 | Miniservice template using Cloud Native .NET Core Kit 3 | 4 | # Features 5 | - Basic Clean Architecture skeleton 6 | - Basic CRUD actions with SQL Server Database 7 | - Versioning for API 8 | - OpenAPI enabled 9 | - Relicense and healthcheck 10 | - Dockerfile is out of the box 11 | - Kubernetes natively 12 | 13 | ### Install and run 14 | 15 | 1. Check out this repository 16 | 17 | 2. Run commands below 18 | 19 | ``` 20 | > dotnet new -i \dotnet-new-miniservice\src\TodoWebApi 21 | > dotnet new miniservice 22 | ``` 23 | 24 | Happy hacking!!! -------------------------------------------------------------------------------- /miniservice.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27906.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Company.Application.TodoWebApi", "src\Company.Application.TodoWebApi\Company.Application.TodoWebApi.csproj", "{03CD3BE6-BE0A-4A12-8268-0D4BA3ECCFCD}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {03CD3BE6-BE0A-4A12-8268-0D4BA3ECCFCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {03CD3BE6-BE0A-4A12-8268-0D4BA3ECCFCD}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {03CD3BE6-BE0A-4A12-8268-0D4BA3ECCFCD}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {03CD3BE6-BE0A-4A12-8268-0D4BA3ECCFCD}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {D0D5E0BE-6B36-47EC-8C74-4B8AC2A11E4B} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/.dockerignore: -------------------------------------------------------------------------------- 1 | * 2 | !src 3 | !*.sln 4 | **/bin 5 | **/obj -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/.template.config/template.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Thang Chung", 3 | "classifications": [ 4 | "web" 5 | ], 6 | "name": "Miniservice template using Cloud Native .NET Core Kit", 7 | "identity": "netcorekit.miniservice", 8 | "groupIdentity": "netcorekit.miniservice", 9 | "shortName": "miniservice", 10 | "tags": { 11 | "language": "C#", 12 | "type": "project" 13 | }, 14 | 15 | "sourceName": "TodoWebApi", 16 | "preferNameDirectory": "true", 17 | "primaryOutputs": [ { "path": "TodoWebApi.csproj" } ] 18 | 19 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Company.Application.TodoWebApi.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM microsoft/dotnet:2.1.2-aspnetcore-runtime-alpine AS base 2 | WORKDIR /app 3 | 4 | ARG service_version 5 | ENV SERVICE_VERSION ${service_version:-0.0.1} 6 | 7 | ARG api_version 8 | ENV API_VERSION ${api_version:-1.0} 9 | 10 | ENV ASPNETCORE_URLS http://+:5001 11 | EXPOSE 5001 12 | 13 | FROM microsoft/dotnet:2.1.302-sdk-alpine AS build 14 | WORKDIR /src 15 | COPY . . 16 | 17 | WORKDIR /src/src/TodoWebApi 18 | 19 | RUN dotnet restore -nowarn:msb3202,nu1503 20 | RUN dotnet build --no-restore -c Release -o /app 21 | 22 | FROM build AS publish 23 | RUN dotnet publish --no-restore -c Release -o /app 24 | 25 | FROM base AS final 26 | WORKDIR /app 27 | COPY --from=publish /app . 28 | ENTRYPOINT ["dotnet", "Company.Application.TodoWebApi.dll"] -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Domain/Todo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using NetCoreKit.Domain; 4 | 5 | namespace Company.Application.TodoWebApi.Domain 6 | { 7 | public sealed class Todo : EntityBase 8 | { 9 | internal Todo(string title, string url) 10 | : this(Guid.NewGuid(), title, url) 11 | { 12 | } 13 | 14 | public Todo(Guid id, string title, string url) : base(id) 15 | { 16 | Title = title; 17 | Url = url; 18 | } 19 | 20 | public static Todo Load(string title, string url) 21 | { 22 | return new Todo(title, url); 23 | } 24 | 25 | public static Todo Load(Guid id, string title, string url) 26 | { 27 | return new Todo(id, title, url); 28 | } 29 | 30 | public int? Order { get; private set; } = 1; 31 | [Required] public string Title { get; private set; } 32 | [Required] public string Url { get; private set; } 33 | public bool? Completed { get; private set; } = false; 34 | 35 | public Todo SetOrder(int order) 36 | { 37 | if (order <= 0) 38 | { 39 | throw new DomainException("Order could be greater than zero."); 40 | } 41 | 42 | Order = order; 43 | return this; 44 | } 45 | 46 | public Todo SetCompleted(bool completed) 47 | { 48 | if (Completed.HasValue && !Completed.Value) 49 | Completed = completed; 50 | return this; 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Dtos/TodoDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Company.Application.TodoWebApi.Dtos 4 | { 5 | public class TodoDto 6 | { 7 | public Guid Id { get; set; } 8 | public string Title { get; set; } 9 | public string Url { get; set; } 10 | public int Order { get; set; } 11 | public bool Completed { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Extensions/TodoExtensions.cs: -------------------------------------------------------------------------------- 1 | using Company.Application.TodoWebApi.Domain; 2 | using Company.Application.TodoWebApi.Dtos; 3 | 4 | namespace Company.Application.TodoWebApi.Extensions 5 | { 6 | public static class TodoExtensions 7 | { 8 | public static TodoDto ToDto(this Todo todo) 9 | { 10 | return new TodoDto 11 | { 12 | Id = todo.Id, 13 | Title = todo.Title, 14 | Url = todo.Url, 15 | Completed = todo.Completed ?? false, 16 | Order = todo.Order ?? 1 17 | }; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Infrastructure/Db/TodoDbContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using NetCoreKit.Infrastructure.EfCore.Db; 3 | 4 | namespace Company.Application.TodoWebApi.Infrastructure.Db 5 | { 6 | public class TodoDbContext : ApplicationDbContext 7 | { 8 | public TodoDbContext(DbContextOptions options) : base(options) 9 | { 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace Company.Application.TodoWebApi 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | var webHost = CreateWebHostBuilder(args).Build(); 11 | if ((webHost.Services.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment).IsDevelopment()) 12 | { 13 | // webHost = webHost.RegisterDbContext(); 14 | } 15 | 16 | webHost.Run(); 17 | } 18 | 19 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 20 | WebHost.CreateDefaultBuilder(args) 21 | .UseStartup(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/Startup.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Company.Application.TodoWebApi.Infrastructure.Db; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using NetCoreKit.Infrastructure.AspNetCore.Miniservice; 8 | using NetCoreKit.Infrastructure.EfCore.SqlServer; 9 | 10 | namespace Company.Application.TodoWebApi 11 | { 12 | public class Startup 13 | { 14 | public void ConfigureServices(IServiceCollection services) 15 | { 16 | var assemblies = new HashSet 17 | { 18 | typeof(Startup).GetTypeInfo().Assembly, 19 | typeof(MiniServiceExtensions).GetTypeInfo().Assembly 20 | }; 21 | 22 | var serviceParams = new ServiceParams 23 | { 24 | {"assemblies", assemblies} 25 | }; 26 | 27 | services.AddScoped(sp => serviceParams); 28 | services.AddEfCoreSqlServer(); 29 | services.AddMiniService(); 30 | } 31 | 32 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 33 | { 34 | app.UseMiniService(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "ConnectionStrings": { 3 | "mssqldb": "Server=tcp:127.0.0.1,1433;Database=maindb;User Id=letmein;Password=P@ssw0rd;" 4 | }, 5 | "Hosts": { 6 | "Externals": { 7 | "CurrentUri": "http://localhost:5001" 8 | } 9 | }, 10 | "Logging": { 11 | "IncludeScopes": false, 12 | "LogLevel": { 13 | "Default": "Debug", 14 | "System": "Information", 15 | "Microsoft": "Information" 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "API_VERSION": "1.0", 3 | "SERVICE_VERSION": "0.0.1", 4 | "EnableAuthN": false, 5 | "ConnectionStrings": { 6 | "mssqldb": "Server=tcp:{0},{1};Database={2};User Id={3};Password={4};" 7 | }, 8 | "EfCore": { 9 | "FullyQualifiedPrefix": "TodoWebApi.*", 10 | "ShortyQualifiedPrefix": "TodoWebApi" 11 | }, 12 | "k8s": { 13 | "mssqldb": { 14 | "Host": "TODO_DB_SERVICE_HOST", 15 | "Port": "TODO_DB_SERVICE_PORT", 16 | "Database": "maindb", 17 | "UserName": "letmein", 18 | "Password": "P@ssw0rd" 19 | } 20 | }, 21 | "Hosts": { 22 | "BasePath": "/todo/", 23 | "Externals": { 24 | "CurrentUri": "http://api.dotnetkit.local/todo" 25 | 26 | } 27 | }, 28 | "OpenApi": { 29 | "Enabled": true, 30 | "EnabledUI": true 31 | }, 32 | "Logging": { 33 | "IncludeScopes": false, 34 | "Debug": { 35 | "LogLevel": { 36 | "Default": "Warning" 37 | } 38 | }, 39 | "Console": { 40 | "LogLevel": { 41 | "Default": "Warning" 42 | } 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/HealthController.cs: -------------------------------------------------------------------------------- 1 | using Company.Application.TodoWebApi.Infrastructure.Db; 2 | using Microsoft.AspNetCore.Mvc; 3 | using NetCoreKit.Infrastructure.EfCore.Extensions; 4 | using System; 5 | 6 | namespace Company.Application.TodoWebApi.v1 7 | { 8 | [Route("")] 9 | [ApiVersionNeutral] 10 | [ApiExplorerSettings(IgnoreApi = true)] 11 | public class HealthController : Controller 12 | { 13 | private readonly IServiceProvider _serviceProvider; 14 | 15 | public HealthController(IServiceProvider serviceProvider) 16 | { 17 | _serviceProvider = serviceProvider; 18 | } 19 | 20 | [HttpGet("/healthz")] 21 | public ActionResult Get() 22 | { 23 | try 24 | { 25 | _serviceProvider.MigrateDbContext(); 26 | } 27 | catch (Exception) 28 | { 29 | 30 | return new BadRequestResult(); 31 | } 32 | 33 | return Ok(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/HomeController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using Microsoft.Extensions.Configuration; 3 | using NetCoreKit.Infrastructure.AspNetCore.Extensions; 4 | 5 | namespace Company.Application.TodoWebApi.v1 6 | { 7 | [Route("")] 8 | [ApiVersionNeutral] 9 | [ApiExplorerSettings(IgnoreApi = true)] 10 | public class HomeController : Controller 11 | { 12 | private readonly string _basePath; 13 | 14 | public HomeController(IConfiguration config) 15 | { 16 | _basePath = config.GetBasePath() ?? "/"; 17 | } 18 | 19 | [HttpGet] 20 | public IActionResult Index() 21 | { 22 | return Redirect($"~{_basePath}swagger"); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/TodoController.cs: -------------------------------------------------------------------------------- 1 | using Company.Application.TodoWebApi.v1.UseCases.AddTodo; 2 | using Company.Application.TodoWebApi.v1.UseCases.ClearTodos; 3 | using Company.Application.TodoWebApi.v1.UseCases.DeleteTodo; 4 | using Company.Application.TodoWebApi.v1.UseCases.GetTodo; 5 | using Company.Application.TodoWebApi.v1.UseCases.GetTodos; 6 | using Company.Application.TodoWebApi.v1.UseCases.UpdateTodo; 7 | using MediatR; 8 | using Microsoft.AspNetCore.Mvc; 9 | using NetCoreKit.Infrastructure.AspNetCore; 10 | using NetCoreKit.Infrastructure.AspNetCore.CleanArch; 11 | using System; 12 | using System.Reactive.Linq; 13 | using System.Threading.Tasks; 14 | 15 | namespace Company.Application.TodoWebApi.v1 16 | { 17 | [ApiVersion("1.0")] 18 | [Route("api/todos")] 19 | public class TodoController : EvtControllerBase 20 | { 21 | public TodoController(IMediator eventor) : base(eventor) 22 | { 23 | } 24 | 25 | [HttpGet] 26 | public async Task Get() 27 | { 28 | return await Eventor.SendStream( 29 | new GetTodosRequest(), 30 | x => x.Result); 31 | } 32 | 33 | [HttpGet("{todoId:guid}")] 34 | public async Task Get(Guid todoId) 35 | { 36 | return await Eventor.SendStream( 37 | new GetTodoRequest {Id = todoId}, 38 | x => x.Result); 39 | } 40 | 41 | [HttpPost] 42 | public async Task Post(AddTodoRequest request) 43 | { 44 | return await Eventor.SendStream( 45 | request, 46 | x => x.Result); 47 | } 48 | 49 | [HttpDelete("{todoId:guid}")] 50 | public async Task Delete(Guid todoId) 51 | { 52 | return await Eventor.SendStream( 53 | new DeleteTodoRequest {Id = todoId}, 54 | x => x.Result); 55 | } 56 | 57 | [HttpDelete] 58 | public async Task Clear() 59 | { 60 | return await Eventor.SendStream( 61 | new ClearTodosRequest(), 62 | x => x.Result); 63 | } 64 | 65 | [HttpPut("{todoId:guid}")] 66 | public async Task Clear(Guid todoId, UpdateTodoRequest request) 67 | { 68 | request.Id = todoId; 69 | return await Eventor.SendStream( 70 | request, 71 | x => x.Result); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/AddTodo/Payloads.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | using Company.Application.TodoWebApi.Dtos; 3 | using MediatR; 4 | 5 | namespace Company.Application.TodoWebApi.v1.UseCases.AddTodo 6 | { 7 | public class AddTodoRequest : IRequest 8 | { 9 | public int? Order { get; set; } = 1; 10 | [Required] public string Title { get; set; } 11 | [Required] public string Url { get; set; } 12 | public bool? Completed { get; set; } = false; 13 | } 14 | 15 | public class AddTodoResponse 16 | { 17 | public TodoDto Result { get; set; } 18 | } 19 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/AddTodo/RequestHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | using Company.Application.TodoWebApi.Domain; 4 | using Company.Application.TodoWebApi.Extensions; 5 | using NetCoreKit.Domain; 6 | using NetCoreKit.Infrastructure.AspNetCore.CleanArch; 7 | 8 | namespace Company.Application.TodoWebApi.v1.UseCases.AddTodo 9 | { 10 | public class RequestHandler : TxRequestHandlerBase 11 | { 12 | public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory queryRepositoryFactory) 13 | : base(uow, queryRepositoryFactory) 14 | { 15 | } 16 | 17 | public override async Task TxHandle(AddTodoRequest request, CancellationToken cancellationToken) 18 | { 19 | var todoRepository = UnitOfWork.Repository(); 20 | 21 | var todo = Todo.Load(request.Title, request.Url); 22 | 23 | var result = await todoRepository.AddAsync(todo); 24 | return new AddTodoResponse {Result = result.ToDto()}; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/ClearTodos/Payloads.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | 3 | namespace Company.Application.TodoWebApi.v1.UseCases.ClearTodos 4 | { 5 | public class ClearTodosRequest : IRequest 6 | { 7 | } 8 | 9 | public class ClearTodosResponse 10 | { 11 | public bool Result { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/ClearTodos/RequestHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | using NetCoreKit.Domain; 4 | using NetCoreKit.Infrastructure.AspNetCore.CleanArch; 5 | using NetCoreKit.Infrastructure.EfCore.Extensions; 6 | 7 | namespace Company.Application.TodoWebApi.v1.UseCases.ClearTodos 8 | { 9 | public class RequestHandler : TxRequestHandlerBase 10 | { 11 | public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory queryRepositoryFactory) 12 | : base(uow, queryRepositoryFactory) 13 | { 14 | } 15 | 16 | public override async Task TxHandle(ClearTodosRequest request, 17 | CancellationToken cancellationToken) 18 | { 19 | var commandRepository = UnitOfWork.Repository(); 20 | var queryRepository = QueryRepositoryFactory.QueryEfRepository(); 21 | 22 | var todos = await queryRepository.ListAsync(); 23 | if (todos == null || todos.Count <= 0) return new ClearTodosResponse(); 24 | 25 | //TODO: need to have a ClearAll method in CommandRepository 26 | foreach (var todo in todos) 27 | { 28 | await commandRepository.DeleteAsync(todo); 29 | } 30 | 31 | return new ClearTodosResponse(); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/DeleteTodo/Payloads.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using MediatR; 3 | 4 | namespace Company.Application.TodoWebApi.v1.UseCases.DeleteTodo 5 | { 6 | public class DeleteTodoRequest : IRequest 7 | { 8 | public Guid Id { get; set; } 9 | } 10 | 11 | public class DeleteTodoResponse 12 | { 13 | public Guid Result { get; set; } 14 | } 15 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/DeleteTodo/RequestHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using NetCoreKit.Domain; 5 | using NetCoreKit.Infrastructure.AspNetCore.CleanArch; 6 | using NetCoreKit.Infrastructure.EfCore.Extensions; 7 | 8 | namespace Company.Application.TodoWebApi.v1.UseCases.DeleteTodo 9 | { 10 | public class RequestHandler : TxRequestHandlerBase 11 | { 12 | public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory queryRepositoryFactory) 13 | : base(uow, queryRepositoryFactory) 14 | { 15 | } 16 | 17 | public override async Task TxHandle(DeleteTodoRequest request, 18 | CancellationToken cancellationToken) 19 | { 20 | var commandRepository = UnitOfWork.Repository(); 21 | var queryRepository = QueryRepositoryFactory.QueryEfRepository(); 22 | 23 | var todo = await queryRepository.GetByIdAsync(request.Id); 24 | if (todo == null) 25 | { 26 | throw new Exception($"Could not find item #{request.Id}."); 27 | } 28 | 29 | await commandRepository.DeleteAsync(todo); 30 | 31 | return new DeleteTodoResponse {Result = todo.Id}; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/GetTodo/Payloads.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Company.Application.TodoWebApi.Dtos; 3 | using MediatR; 4 | 5 | namespace Company.Application.TodoWebApi.v1.UseCases.GetTodo 6 | { 7 | public class GetTodoRequest : IRequest 8 | { 9 | public Guid Id { get; set; } 10 | } 11 | 12 | public class GetTodoResponse 13 | { 14 | public TodoDto Result { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/GetTodo/RequestHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | using Company.Application.TodoWebApi.Extensions; 4 | using NetCoreKit.Domain; 5 | using NetCoreKit.Infrastructure.AspNetCore.CleanArch; 6 | using NetCoreKit.Infrastructure.EfCore.Extensions; 7 | 8 | namespace Company.Application.TodoWebApi.v1.UseCases.GetTodo 9 | { 10 | public class RequestHandler : RequestHandlerBase 11 | { 12 | public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory queryRepositoryFactory) 13 | : base(uow, queryRepositoryFactory) 14 | { 15 | } 16 | 17 | public override async Task Handle(GetTodoRequest request, CancellationToken cancellationToken) 18 | { 19 | var queryRepository = QueryRepositoryFactory.QueryEfRepository(); 20 | var result = await queryRepository.GetByIdAsync(request.Id); 21 | return new GetTodoResponse 22 | { 23 | Result = result.ToDto() 24 | }; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/GetTodos/Payloads.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Company.Application.TodoWebApi.Dtos; 3 | using MediatR; 4 | 5 | namespace Company.Application.TodoWebApi.v1.UseCases.GetTodos 6 | { 7 | public class GetTodosRequest : IRequest 8 | { 9 | } 10 | 11 | public class GetTodosResponse 12 | { 13 | public List Result { get; set; } 14 | } 15 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/GetTodos/RequestHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using Company.Application.TodoWebApi.Extensions; 5 | using NetCoreKit.Domain; 6 | using NetCoreKit.Infrastructure.AspNetCore.CleanArch; 7 | using NetCoreKit.Infrastructure.EfCore.Extensions; 8 | 9 | namespace Company.Application.TodoWebApi.v1.UseCases.GetTodos 10 | { 11 | public class RequestHandler : RequestHandlerBase 12 | { 13 | public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory queryRepositoryFactory) 14 | : base(uow, queryRepositoryFactory) 15 | { 16 | } 17 | 18 | public override async Task Handle(GetTodosRequest request, CancellationToken cancellationToken) 19 | { 20 | var queryRepository = QueryRepositoryFactory.QueryEfRepository(); 21 | var result = await queryRepository.ListAsync(); 22 | return new GetTodosResponse 23 | { 24 | Result = result.Select(x => x.ToDto()).ToList() 25 | }; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/UpdateTodo/Payloads.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using Company.Application.TodoWebApi.Dtos; 4 | using MediatR; 5 | 6 | namespace Company.Application.TodoWebApi.v1.UseCases.UpdateTodo 7 | { 8 | public class UpdateTodoRequest : IRequest 9 | { 10 | public Guid Id { get; set; } 11 | public int? Order { get; set; } = 1; 12 | [Required] public string Title { get; set; } 13 | [Required] public string Url { get; set; } 14 | public bool? Completed { get; set; } = false; 15 | } 16 | 17 | public class UpdateTodoResponse 18 | { 19 | public TodoDto Result { get; set; } 20 | } 21 | } -------------------------------------------------------------------------------- /src/Company.Application.TodoWebApi/v1/UseCases/UpdateTodo/RequestHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using Company.Application.TodoWebApi.Extensions; 5 | using NetCoreKit.Domain; 6 | using NetCoreKit.Infrastructure.AspNetCore.CleanArch; 7 | using NetCoreKit.Infrastructure.EfCore.Extensions; 8 | 9 | namespace Company.Application.TodoWebApi.v1.UseCases.UpdateTodo 10 | { 11 | public class RequestHandler : TxRequestHandlerBase 12 | { 13 | public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory queryRepositoryFactory) 14 | : base(uow, queryRepositoryFactory) 15 | { 16 | } 17 | 18 | public override async Task TxHandle(UpdateTodoRequest request, 19 | CancellationToken cancellationToken) 20 | { 21 | var commandRepository = UnitOfWork.Repository(); 22 | var queryRepository = QueryRepositoryFactory.QueryEfRepository(); 23 | 24 | var todo = await queryRepository.GetByIdAsync(request.Id); 25 | if (todo == null) 26 | { 27 | throw new Exception($"Could not find item #{request.Id}."); 28 | } 29 | 30 | var updated = await commandRepository.UpdateAsync(todo); 31 | return new UpdateTodoResponse {Result = updated.ToDto()}; 32 | } 33 | } 34 | } --------------------------------------------------------------------------------