Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database option #5

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .template.config/dotnetcli.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"GitHost": {
"longName": "git-host",
"shortName": "gh"
},
"Database": {
"longName": "database",
"shortName": "db"
}
}
}
10 changes: 10 additions & 0 deletions .template.config/ide.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
"text": "Select the git respository host"
},
"isVisible": true
},
{
"id": "Database",
"name": {
"text": "Database"
},
"description": {
"text": "Select the database"
},
"isVisible": true
}
]
}
68 changes: 67 additions & 1 deletion .template.config/template.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Enis Mulic",
"classifications": ["Api", "Minimal Api", "Vertical Slice Architecture", "CQRS"],
"classifications": [
"Api",
"Minimal Api",
"Vertical Slice Architecture",
"CQRS"
],
"identity": "VerticalSliceMinimalApi",
"name": "Vertical Slice Minimal Api",
"shortName": "vsma",
Expand Down Expand Up @@ -42,6 +47,38 @@
"UseNoGitHost": {
"type": "computed",
"value": "(GitHost == \"None\")"
},
"Database": {
"type": "parameter",
"datatype": "choice",
"choices": [
{
"choice": "MsSql",
"description": "Use Microsoft SQL or Azure SQL"
},
{
"choice": "PostgreSql",
"description": "Use PostgreSql"
},
{
"choice": "None",
"description": ""
}
],
"defaultValue": "None",
"description": "The type of database to use"
},
"UseMsSql": {
"type": "computed",
"value": "(Database == \"MsSql\")"
},
"UsePostgreSql": {
"type": "computed",
"value": "(Database == \"PostgreSql\")"
},
"UseDatabase": {
"type": "computed",
"value": "(Database != \"None\")"
}
},
"sources": [
Expand All @@ -60,6 +97,35 @@
{
"condition": "(UseNoGitHost)",
"exclude": [".azuredevops/**", ".github/**"]
},
{
"condition": "(!UseDatabase)",
"exclude": [
"src/Application/Infrastructure/Persistance/**",
"appsettings.*.json"
]
},
{
"condition": "(UseMsSql)",
"exclude": [
"src/Api/appsettings.PostgreSql.json",
"docker-compose.postgresql.yml"
],
"rename": {
"appsettings.MsSql.json": "appsettings.Development.json",
"docker-compose.mssql.yml": "docker-compose.yml"
}
},
{
"condition": "(UsePostgreSql)",
"exclude": [
"src/Api/appsettings.MsSql.json",
"docker-compose.mssql.yml"
],
"rename": {
"appsettings.PostgreSql.json": "appsettings.Development.json",
"docker-compose.postgresql.yml": "docker-compose.yml"
}
}
]
}
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build](https://github.com/EnisMulic/VerticalSliceMinimalApi/actions/workflows/ci.yml/badge.svg)](https://github.com/EnisMulic/VerticalSliceMinimalApi/actions/workflows/ci.yml)

## Getting Started
## Getting Started

1. Clone the repository

Expand All @@ -19,26 +19,31 @@ dotnet new install ./VerticalSliceMinimalApi
3. Use the installed template to create your API

```sh
dotnet new vsma --name ProjectName
dotnet new vsma --name ProjectName
```

## Synopsis

```
dotnet new vsma --name [ProjectName]
[-gh|--git-host [Github|AzureDevOps|None]]
[-db|--database [MsSql|PostgreSql|None]]
```

### Options

* `--name [ProjectName]`
- `--name [ProjectName]`
Specify the name of the project you are creating, this will replace all occurrences of `ProjectName` in the template with the name you pass in.

* `-gh|--git-host [Github|AzureDevOps|None]`
- `-gh|--git-host [Github|AzureDevOps|None]`
Choose the platform you will host your projects git repository, this will give you a base CI workflow, pull request template, and anything specific to the platform that might be of use. The default value is `None`.
- `-db|--database [MsSql|PostgreSql|None]`
Choose what database to use for your project. The default is `None`

## How to Run

To start the app run:

```sh
dotnet run --project src/Api
```
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions docker-compose.postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
postgres:
image: postgres:14-alpine
restart: unless-stopped
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=QWElkj132!
- POSTGRES_USER=todos_user
- POSTGRES_DB=todos_db
5 changes: 4 additions & 1 deletion src/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Carter" Version="7.0.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.2.2" />
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.2" />

<!-- #if (UseDatabase) -->
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- #endif -->

</ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
{
"ConnectionStrings": {
"Default": "Server=.;Database=TodoDatabase;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True"
"Default": "Server=.;Database=TodoDatabase;MultipleActiveResultSets=true;TrustServerCertificate=True;User Id=sa;Password=QWElkj132!"
},
"Logging": {
"LogLevel": {
Expand All @@ -21,4 +21,4 @@
}
}
}
}
}
24 changes: 24 additions & 0 deletions src/Api/appsettings.PostgreSql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"ConnectionStrings": {
"Default": "Host=localhost;Port=5432;Database=todos_db;Integrated Security=true;Pooling=true;Username=todos_user;Password=QWElkj132!"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Authentication": {
"Schemes": {
"Bearer": {
"ValidAudiences": [
"http://localhost:44986",
"https://localhost:44337",
"http://localhost:5191",
"https://localhost:7079"
],
"ValidIssuer": "dotnet-user-jwts"
}
}
}
}
5 changes: 1 addition & 4 deletions src/Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@
}
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Default": "Server=.;Database=TodoDatabase;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True"
}
"AllowedHosts": "*"
}
28 changes: 19 additions & 9 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -10,10 +10,18 @@
</PropertyGroup>

<ItemGroup>

<PackageReference Include="MediatR" Version="11.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="FluentValidation" Version="11.4.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.4.0" />

<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Carter" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />

<!-- #if (UseDatabase) -->
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -24,15 +32,17 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Carter" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.12" />

<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="7.0.0" />

<!-- #if (UseMsSql) -->
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.12" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="7.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />

<!-- #elif (UsePostgreSql) -->
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="7.1.0" />
<!-- #endif -->
<!-- #endif -->
</ItemGroup>
</Project>
18 changes: 14 additions & 4 deletions src/Application/Infrastructure/ConfigureServices.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Application.Common.Interfaces;
#if UseDatabase
using Application.Infrastructure.Persistance;
using Application.Infrastructure.Persistance.Interceptors;
using Application.Infrastructure.Services;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

#endif
using Application.Infrastructure.Services;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -14,6 +18,9 @@ public static class ConfigureInfrastructureServices
{
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddTransient<IDateTime, DateTimeService>();

#if UseDatabase
services.AddScoped<ISaveChangesInterceptor, AuditableEntityInterceptor>();
services.AddScoped<ISaveChangesInterceptor, DispatchDomainEventsInterceptor>();
services.AddScoped<ISaveChangesInterceptor, SoftDeleteInterceptor>();
Expand All @@ -22,15 +29,18 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());

#if UseMsSql
options.UseSqlServer(configuration.GetConnectionString("Default"),
builder => builder.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
#elif UsePostgreSql
options.UseNpgsql(configuration.GetConnectionString("Default"),
builder => builder.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
#endif
});

services.AddTransient<IDateTime, DateTimeService>();

services.AddHealthChecks()
.AddDbContextCheck<ApplicationDbContext>("Database");

#endif

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="Respawn" Version="6.1.0" />
<PackageReference Include="Testcontainers" Version="3.5.0" />
<!-- #if (UseMsSql) -->
<PackageReference Include="Testcontainers.MsSql" Version="3.5.0" />
<!-- #else -->
<PackageReference Include="Testcontainers.PostgreSql" Version="3.5.0" />
<!-- #endif -->
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
15 changes: 15 additions & 0 deletions tests/Application.IntegrationTests/TestContainersDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,37 @@

using Respawn;

#if UseMsSql
using Testcontainers.MsSql;
#else
using Testcontainers.PostgreSql;
#endif

namespace Application.IntegrationTests;

public class TestContainersDatabase : ITestDatabase
{
#if UseMsSql
private readonly MsSqlContainer _container;
#else
private readonly PostgreSqlContainer _container;
#endif

private DbConnection _connection = null!;
private string _connectionString = null!;
private Respawner _respawner = null!;

public TestContainersDatabase()
{
#if UseMsSql
_container = new MsSqlBuilder()
.WithAutoRemove(true)
.Build();
#else
_container = new PostgreSqlBuilder()
.WithAutoRemove(true)
.Build();
#endif
}

public async Task InitialiseAsync()
Expand Down
Loading