@yosr-ai/template-dotnet 0.0.0-stage → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +14 -2
- package/features/auth-api-key/files/src/App/Core/ApiKeyMiddleware.cs +33 -0
- package/features/auth-api-key/files/tests/App.Tests/ApiKeyTests.cs +27 -0
- package/features/data-efcore/files/src/App/App.csproj +16 -0
- package/features/data-efcore/files/src/App/Core/AppDbContext.cs +22 -0
- package/features/data-efcore/files/src/App/Core/AppSettings.cs +30 -0
- package/features/data-efcore/files/src/App/Features/Notes/EfNoteStore.cs +46 -0
- package/features/data-efcore/files/src/App/Features/Notes/NoteEntity.cs +24 -0
- package/features/data-efcore/files/src/App/Program.cs +46 -0
- package/features/data-efcore/files/src/App/packages.lock.json +74 -0
- package/features/data-efcore/files/tests/App.Tests/ApiFactory.cs +70 -0
- package/features/data-efcore/files/tests/App.Tests/packages.lock.json +511 -0
- package/features/data-efcore/snippets/program-usings.txt +1 -0
- package/features/data-efcore/snippets/readme-db.md +3 -0
- package/features/data-efcore/snippets/service-registration.txt +3 -0
- package/features/deploy-docker/files/Dockerfile +19 -0
- package/features/deploy-docker/files/compose.yaml +12 -0
- package/features/deploy-docker/snippets/readme-docker.md +3 -0
- package/features/deploy-docker-postgres/snippets/app-config.yaml +3 -0
- package/features/deploy-docker-postgres/snippets/app-env.yaml +1 -0
- package/features/deploy-docker-postgres/snippets/compose-header.yaml +1 -0
- package/features/deploy-docker-postgres/snippets/compose-volumes.yaml +2 -0
- package/features/deploy-docker-postgres/snippets/postgres-service.yaml +13 -0
- package/features/deploy-docker-postgres/snippets/readme-docker.md +3 -0
- package/features/errors-result/files/src/App/Features/Notes/NotesEndpoints.cs +52 -0
- package/features/errors-result/files/src/App/Features/Notes/NotesService.cs +56 -0
- package/features/errors-result/files/src/App/Shared/Result.cs +8 -0
- package/features/errors-result/files/tests/App.Tests/NotesServiceTests.cs +37 -0
- package/files/.env.example +7 -0
- package/files/App.sln +24 -0
- package/files/Directory.Build.props +9 -0
- package/files/README.md +20 -0
- package/files/gitignore +9 -0
- package/files/global.json +6 -0
- package/files/src/App/App.csproj +13 -0
- package/files/src/App/Core/AppSettings.cs +25 -0
- package/files/src/App/Features/Notes/INoteStore.cs +12 -0
- package/files/src/App/Features/Notes/MemoryNoteStore.cs +54 -0
- package/files/src/App/Features/Notes/Note.cs +24 -0
- package/files/src/App/Features/Notes/NotesEndpoints.cs +63 -0
- package/files/src/App/Features/Notes/NotesService.cs +38 -0
- package/files/src/App/Program.cs +26 -0
- package/files/src/App/Properties/launchSettings.json +12 -0
- package/files/src/App/Shared/NotFoundException.cs +9 -0
- package/files/src/App/appsettings.json +9 -0
- package/files/src/App/packages.lock.json +21 -0
- package/files/tests/App.Tests/ApiFactory.cs +14 -0
- package/files/tests/App.Tests/App.Tests.csproj +28 -0
- package/files/tests/App.Tests/GlobalUsings.cs +1 -0
- package/files/tests/App.Tests/NotesApiTests.cs +62 -0
- package/files/tests/App.Tests/NotesServiceTests.cs +36 -0
- package/files/tests/App.Tests/packages.lock.json +428 -0
- package/package.json +26 -4
- package/rules/architecture.md +5 -0
- package/rules/auth-api-key.md +3 -0
- package/rules/commands.md +7 -0
- package/rules/data-efcore.md +3 -0
- package/rules/data-none.md +3 -0
- package/rules/errors-result.md +3 -0
- package/rules/errors-throw.md +3 -0
- package/rules/layout-feature.md +5 -0
- package/rules/layout-flat.md +3 -0
- package/rules/layout-layer.md +3 -0
- package/rules/project-overview.md +5 -0
- package/seed.template.json +716 -0
- package/style/.gitkeep +0 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @yosr-ai/template-dotnet
|
|
2
|
+
|
|
3
|
+
## 0.2.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 2373b62: Fix Spring create success next-steps (`mvn spring-boot:run` instead of `pnpm dev`) and add download links when the JDK or Maven is missing (same clarity as .NET’s https://dot.net/download).
|
|
8
|
+
|
|
9
|
+
## 0.2.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 63badc9: Add the ASP.NET Core API template (`--template dotnet`, ADR 0026): .NET 10 LTS, minimal API, xUnit + WebApplicationFactory, locked NuGet restore with committed `packages.lock.json` and `global.json`, notes API, database none/Postgres (EF Core + Npgsql), API-key auth, multi-stage non-root Docker. Engine gains `engines.dotnet` / package manager `dotnet` (and the same shape for later `engines.java` / `maven`). Joins the Yosr Seed fixed group.
|
|
14
|
+
- 11f252b: Add Spring Boot template-spring (ADR 0027): Java 25, Maven locked resolve, optional JPA Postgres.
|
|
15
|
+
|
|
16
|
+
## 0.2.4
|
|
17
|
+
|
|
18
|
+
### Minor Changes
|
|
19
|
+
|
|
20
|
+
- First npm release of the ASP.NET Core API template (`--template dotnet`, ADR 0026): .NET 10 LTS, minimal API, xUnit + WebApplicationFactory, locked NuGet restore, `global.json`, notes API, database none/Postgres (EF Core + Npgsql), API-key auth, Docker (multi-stage non-root). Joins the Yosr Seed fixed group.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wael Zohny / Yosr AI
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yosr-ai/template-dotnet
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Yosr Seed template for an API-only ASP.NET Core service on .NET 10 (ADR 0026). Create with:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx create-yosr-seed@latest my-api --template dotnet
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Published MIT. Exact-pinned by the `yosr-seed` CLI.
|
|
10
|
+
|
|
11
|
+
## Bugs and feedback
|
|
12
|
+
|
|
13
|
+
Email **wzohny@gmail.com**. The source repository is private, so there is no public issue tracker.
|
|
14
|
+
|
|
15
|
+
Site: [seed.yosr.ai](https://seed.yosr.ai)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
namespace App.Core;
|
|
2
|
+
|
|
3
|
+
public sealed class ApiKeyMiddleware(RequestDelegate next)
|
|
4
|
+
{
|
|
5
|
+
public const string HeaderName = "X-API-Key";
|
|
6
|
+
|
|
7
|
+
public async Task InvokeAsync(HttpContext context, AppSettings settings)
|
|
8
|
+
{
|
|
9
|
+
if (context.Request.Path.StartsWithSegments("/health") ||
|
|
10
|
+
context.Request.Path.StartsWithSegments("/openapi"))
|
|
11
|
+
{
|
|
12
|
+
await next(context);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (string.IsNullOrEmpty(settings.ApiKey))
|
|
17
|
+
{
|
|
18
|
+
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
|
19
|
+
await context.Response.WriteAsJsonAsync(new { detail = "API_KEY is not configured." });
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!context.Request.Headers.TryGetValue(HeaderName, out var provided) ||
|
|
24
|
+
!string.Equals(provided.ToString(), settings.ApiKey, StringComparison.Ordinal))
|
|
25
|
+
{
|
|
26
|
+
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
|
27
|
+
await context.Response.WriteAsJsonAsync(new { detail = "Invalid or missing API key." });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
await next(context);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
using System.Net;
|
|
2
|
+
|
|
3
|
+
namespace App.Tests;
|
|
4
|
+
|
|
5
|
+
public sealed class ApiKeyTests : IClassFixture<ApiFactory>
|
|
6
|
+
{
|
|
7
|
+
private readonly HttpClient _client;
|
|
8
|
+
|
|
9
|
+
public ApiKeyTests(ApiFactory factory)
|
|
10
|
+
{
|
|
11
|
+
_client = factory.CreateClient();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
[Fact]
|
|
15
|
+
public async Task Missing_api_key_is_unauthorized()
|
|
16
|
+
{
|
|
17
|
+
var response = await _client.GetAsync("/notes");
|
|
18
|
+
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
[Fact]
|
|
22
|
+
public async Task Health_stays_open()
|
|
23
|
+
{
|
|
24
|
+
var response = await _client.GetAsync("/health");
|
|
25
|
+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<Project Sdk="Microsoft.NET.Sdk.Web">
|
|
2
|
+
|
|
3
|
+
<PropertyGroup>
|
|
4
|
+
<TargetFramework>net10.0</TargetFramework>
|
|
5
|
+
<RootNamespace>App</RootNamespace>
|
|
6
|
+
<AssemblyName>App</AssemblyName>
|
|
7
|
+
</PropertyGroup>
|
|
8
|
+
|
|
9
|
+
<ItemGroup>
|
|
10
|
+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.15" />
|
|
11
|
+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
|
12
|
+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.5" />
|
|
13
|
+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
|
|
14
|
+
</ItemGroup>
|
|
15
|
+
|
|
16
|
+
</Project>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
using App.Features.Notes;
|
|
2
|
+
using Microsoft.EntityFrameworkCore;
|
|
3
|
+
|
|
4
|
+
namespace App.Core;
|
|
5
|
+
|
|
6
|
+
public sealed class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
|
|
7
|
+
{
|
|
8
|
+
public DbSet<NoteEntity> Notes => Set<NoteEntity>();
|
|
9
|
+
|
|
10
|
+
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
11
|
+
{
|
|
12
|
+
modelBuilder.Entity<NoteEntity>(entity =>
|
|
13
|
+
{
|
|
14
|
+
entity.ToTable("notes");
|
|
15
|
+
entity.HasKey(note => note.Id);
|
|
16
|
+
entity.Property(note => note.Title).HasMaxLength(200).IsRequired();
|
|
17
|
+
entity.Property(note => note.Body);
|
|
18
|
+
entity.Property(note => note.CreatedAt).IsRequired();
|
|
19
|
+
entity.Property(note => note.UpdatedAt);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
namespace App.Core;
|
|
2
|
+
|
|
3
|
+
public sealed class AppSettings
|
|
4
|
+
{
|
|
5
|
+
public required string AppName { get; init; }
|
|
6
|
+
|
|
7
|
+
public IReadOnlyList<string> CorsOrigins { get; init; } = ["http://localhost:5173"];
|
|
8
|
+
|
|
9
|
+
public required string DatabaseUrl { get; init; }
|
|
10
|
+
|
|
11
|
+
// @seed:anchor settings-fields
|
|
12
|
+
|
|
13
|
+
public static AppSettings FromConfiguration(IConfiguration configuration)
|
|
14
|
+
{
|
|
15
|
+
var cors = configuration["CORS_ORIGINS"]
|
|
16
|
+
?? Environment.GetEnvironmentVariable("CORS_ORIGINS")
|
|
17
|
+
?? "http://localhost:5173";
|
|
18
|
+
return new AppSettings
|
|
19
|
+
{
|
|
20
|
+
AppName = configuration["APP_NAME"]
|
|
21
|
+
?? Environment.GetEnvironmentVariable("APP_NAME")
|
|
22
|
+
?? "__seed_projectName__",
|
|
23
|
+
CorsOrigins = cors.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries),
|
|
24
|
+
DatabaseUrl = configuration["DATABASE_URL"]
|
|
25
|
+
?? Environment.GetEnvironmentVariable("DATABASE_URL")
|
|
26
|
+
?? "__seed_databaseUrl__",
|
|
27
|
+
// @seed:anchor settings-values
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
using App.Core;
|
|
2
|
+
using Microsoft.EntityFrameworkCore;
|
|
3
|
+
|
|
4
|
+
namespace App.Features.Notes;
|
|
5
|
+
|
|
6
|
+
public sealed class EfNoteStore(AppDbContext db) : INoteStore
|
|
7
|
+
{
|
|
8
|
+
public async Task<IReadOnlyList<Note>> ListAsync(CancellationToken cancellationToken = default)
|
|
9
|
+
{
|
|
10
|
+
var rows = await db.Notes.AsNoTracking()
|
|
11
|
+
.OrderByDescending(note => note.Id)
|
|
12
|
+
.ToListAsync(cancellationToken);
|
|
13
|
+
return rows.Select(row => row.ToNote()).ToList();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public async Task<Note?> GetAsync(int id, CancellationToken cancellationToken = default)
|
|
17
|
+
{
|
|
18
|
+
var row = await db.Notes.AsNoTracking()
|
|
19
|
+
.FirstOrDefaultAsync(note => note.Id == id, cancellationToken);
|
|
20
|
+
return row?.ToNote();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async Task<Note> AddAsync(string title, string? body, CancellationToken cancellationToken = default)
|
|
24
|
+
{
|
|
25
|
+
var row = new NoteEntity
|
|
26
|
+
{
|
|
27
|
+
Title = title,
|
|
28
|
+
Body = body,
|
|
29
|
+
CreatedAt = DateTimeOffset.UtcNow,
|
|
30
|
+
};
|
|
31
|
+
db.Notes.Add(row);
|
|
32
|
+
await db.SaveChangesAsync(cancellationToken);
|
|
33
|
+
return row.ToNote();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async Task<Note?> UpdateAsync(int id, string? title, string? body, CancellationToken cancellationToken = default)
|
|
37
|
+
{
|
|
38
|
+
var row = await db.Notes.FirstOrDefaultAsync(note => note.Id == id, cancellationToken);
|
|
39
|
+
if (row is null) return null;
|
|
40
|
+
if (title is not null) row.Title = title;
|
|
41
|
+
if (body is not null) row.Body = body;
|
|
42
|
+
row.UpdatedAt = DateTimeOffset.UtcNow;
|
|
43
|
+
await db.SaveChangesAsync(cancellationToken);
|
|
44
|
+
return row.ToNote();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
namespace App.Features.Notes;
|
|
2
|
+
|
|
3
|
+
public sealed class NoteEntity
|
|
4
|
+
{
|
|
5
|
+
public int Id { get; set; }
|
|
6
|
+
|
|
7
|
+
public required string Title { get; set; }
|
|
8
|
+
|
|
9
|
+
public string? Body { get; set; }
|
|
10
|
+
|
|
11
|
+
public DateTimeOffset CreatedAt { get; set; }
|
|
12
|
+
|
|
13
|
+
public DateTimeOffset? UpdatedAt { get; set; }
|
|
14
|
+
|
|
15
|
+
public Note ToNote() =>
|
|
16
|
+
new()
|
|
17
|
+
{
|
|
18
|
+
Id = Id,
|
|
19
|
+
Title = Title,
|
|
20
|
+
Body = Body,
|
|
21
|
+
CreatedAt = CreatedAt,
|
|
22
|
+
UpdatedAt = UpdatedAt,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
using App.Core;
|
|
2
|
+
using App.Features.Notes;
|
|
3
|
+
using App.Shared;
|
|
4
|
+
using Microsoft.EntityFrameworkCore;
|
|
5
|
+
// @seed:anchor program-usings
|
|
6
|
+
|
|
7
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
8
|
+
|
|
9
|
+
var settings = AppSettings.FromConfiguration(builder.Configuration);
|
|
10
|
+
builder.Services.AddSingleton(settings);
|
|
11
|
+
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
12
|
+
options.UseNpgsql(ToNpgsql(settings.DatabaseUrl)));
|
|
13
|
+
builder.Services.AddScoped<INoteStore, EfNoteStore>();
|
|
14
|
+
builder.Services.AddScoped<NotesService>();
|
|
15
|
+
// @seed:anchor service-registration
|
|
16
|
+
|
|
17
|
+
builder.Services.AddOpenApi();
|
|
18
|
+
|
|
19
|
+
var app = builder.Build();
|
|
20
|
+
|
|
21
|
+
// @seed:anchor middleware
|
|
22
|
+
|
|
23
|
+
app.MapOpenApi();
|
|
24
|
+
app.MapGet("/health", () => Results.Ok(new { status = "ok" }));
|
|
25
|
+
app.MapNotesEndpoints();
|
|
26
|
+
|
|
27
|
+
app.Run();
|
|
28
|
+
|
|
29
|
+
static string ToNpgsql(string url)
|
|
30
|
+
{
|
|
31
|
+
if (url.StartsWith("postgresql://", StringComparison.OrdinalIgnoreCase) ||
|
|
32
|
+
url.StartsWith("postgres://", StringComparison.OrdinalIgnoreCase))
|
|
33
|
+
{
|
|
34
|
+
var uri = new Uri(url);
|
|
35
|
+
var user = Uri.UnescapeDataString(uri.UserInfo.Split(':')[0]);
|
|
36
|
+
var pass = uri.UserInfo.Contains(':')
|
|
37
|
+
? Uri.UnescapeDataString(uri.UserInfo.Split(':')[1])
|
|
38
|
+
: string.Empty;
|
|
39
|
+
var db = uri.AbsolutePath.Trim('/');
|
|
40
|
+
return $"Host={uri.Host};Port={(uri.Port > 0 ? uri.Port : 5432)};Database={db};Username={user};Password={pass}";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return url;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public partial class Program;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"net10.0": {
|
|
5
|
+
"Microsoft.AspNetCore.OpenApi": {
|
|
6
|
+
"type": "Direct",
|
|
7
|
+
"requested": "[10.0.15, )",
|
|
8
|
+
"resolved": "10.0.15",
|
|
9
|
+
"contentHash": "pptFDPMWyPDDtt68s/HsvLYeHJ0WQtntjyve6ztp/5MrLOEzdZvafYIyWSeErtexIGaB8XGYCnnUK8T/Vn4MwQ==",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"Microsoft.OpenApi": "[2.13.0, 3.0.0)"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"Microsoft.EntityFrameworkCore": {
|
|
15
|
+
"type": "Direct",
|
|
16
|
+
"requested": "[10.0.5, )",
|
|
17
|
+
"resolved": "10.0.5",
|
|
18
|
+
"contentHash": "9tNBmK3EpYVGRQLiqP+bqK2m+TD0Gv//4vCzR7ZOgl4FWzCFyOpYdIVka13M4kcBdPdSJcs3wbHr3rmzOqbIMA==",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"Microsoft.EntityFrameworkCore.Abstractions": "10.0.5",
|
|
21
|
+
"Microsoft.EntityFrameworkCore.Analyzers": "10.0.5"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"Microsoft.EntityFrameworkCore.InMemory": {
|
|
25
|
+
"type": "Direct",
|
|
26
|
+
"requested": "[10.0.5, )",
|
|
27
|
+
"resolved": "10.0.5",
|
|
28
|
+
"contentHash": "zz4THzlDOrJ7fKU2YUOzQFs2LJ9DgOSr5xFXcDdoD59el73MTQLtQQOAJxQ94F4XDegyL9+2sePSQGdcU25ZxQ==",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"Microsoft.EntityFrameworkCore": "10.0.5"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"Npgsql.EntityFrameworkCore.PostgreSQL": {
|
|
34
|
+
"type": "Direct",
|
|
35
|
+
"requested": "[10.0.0, )",
|
|
36
|
+
"resolved": "10.0.0",
|
|
37
|
+
"contentHash": "E2+uSWxSB8LdsUVwPaqRWOcGOP92biry2JEwc0KJMdLJF+aZdczeIdEXVwEyv4nSVMQJH0o8tLhyAMiR6VF0lw==",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"Microsoft.EntityFrameworkCore": "[10.0.0, 11.0.0)",
|
|
40
|
+
"Microsoft.EntityFrameworkCore.Relational": "[10.0.0, 11.0.0)",
|
|
41
|
+
"Npgsql": "10.0.0"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"Microsoft.EntityFrameworkCore.Abstractions": {
|
|
45
|
+
"type": "Transitive",
|
|
46
|
+
"resolved": "10.0.5",
|
|
47
|
+
"contentHash": "32c58Rnm47Qvhimawf67KO9PytgPz3QoWye7Abapt0Yocw/JnzMiSNj/pRoIKyn8Jxypkv86zxKD4Q/zNTc0Ag=="
|
|
48
|
+
},
|
|
49
|
+
"Microsoft.EntityFrameworkCore.Analyzers": {
|
|
50
|
+
"type": "Transitive",
|
|
51
|
+
"resolved": "10.0.5",
|
|
52
|
+
"contentHash": "ipC4u1VojgEfoIZhtbS2Sx5IluJTP/Jf1hz3yGsxGBgSukYY/CquI6rAjxn5H58CZgVn36qcuPPtNMwZ0AUzMg=="
|
|
53
|
+
},
|
|
54
|
+
"Microsoft.EntityFrameworkCore.Relational": {
|
|
55
|
+
"type": "Transitive",
|
|
56
|
+
"resolved": "10.0.0",
|
|
57
|
+
"contentHash": "A3MX1ee7RDxWCUdx/KqP+74fbksz0UIhkVZh56YHvbPkEKsffCXgHU3LGkRDwqR/MrBNWLCWC/IVX79tzM30ZA==",
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"Microsoft.EntityFrameworkCore": "10.0.0"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"Microsoft.OpenApi": {
|
|
63
|
+
"type": "Transitive",
|
|
64
|
+
"resolved": "2.13.0",
|
|
65
|
+
"contentHash": "wavlgBYnn7uU/ZGhFHNl0UlDOlwz+6KRduKkqdCCbfxxANaQ1rmBpETVGdwXanAM6twQOxsvI3CBEGF4OScbeQ=="
|
|
66
|
+
},
|
|
67
|
+
"Npgsql": {
|
|
68
|
+
"type": "Transitive",
|
|
69
|
+
"resolved": "10.0.0",
|
|
70
|
+
"contentHash": "xZAYhPOU2rUIFpV48xsqhCx9vXs6Y+0jX2LCoSEfDFYMw9jtAOUk3iQsCnDLrFIv9NT3JGMihn7nnuZsPKqJmA=="
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
using App.Core;
|
|
2
|
+
using App.Features.Notes;
|
|
3
|
+
using Microsoft.AspNetCore.Hosting;
|
|
4
|
+
using Microsoft.AspNetCore.Mvc.Testing;
|
|
5
|
+
using Microsoft.EntityFrameworkCore;
|
|
6
|
+
using Microsoft.Extensions.DependencyInjection;
|
|
7
|
+
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
8
|
+
// @seed:anchor factory-usings
|
|
9
|
+
|
|
10
|
+
namespace App.Tests;
|
|
11
|
+
|
|
12
|
+
/// <summary>
|
|
13
|
+
/// In-process HTTP tests. Uses EnsureCreated against DATABASE_URL when set
|
|
14
|
+
/// (gate throwaway Postgres); otherwise an in-memory EF provider. Gate never
|
|
15
|
+
/// runs <c>dotnet ef database update</c>.
|
|
16
|
+
/// </summary>
|
|
17
|
+
public sealed class ApiFactory : WebApplicationFactory<Program>
|
|
18
|
+
{
|
|
19
|
+
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
20
|
+
{
|
|
21
|
+
builder.UseEnvironment("Development");
|
|
22
|
+
builder.ConfigureServices(services =>
|
|
23
|
+
{
|
|
24
|
+
services.RemoveAll(typeof(DbContextOptions<AppDbContext>));
|
|
25
|
+
services.RemoveAll(typeof(AppDbContext));
|
|
26
|
+
services.RemoveAll(typeof(INoteStore));
|
|
27
|
+
|
|
28
|
+
var databaseUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
|
|
29
|
+
if (!string.IsNullOrWhiteSpace(databaseUrl))
|
|
30
|
+
{
|
|
31
|
+
services.AddDbContext<AppDbContext>(options =>
|
|
32
|
+
options.UseNpgsql(ToNpgsql(databaseUrl)));
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
{
|
|
36
|
+
var name = $"yosr-tests-{Guid.NewGuid():N}";
|
|
37
|
+
services.AddDbContext<AppDbContext>(options =>
|
|
38
|
+
options.UseInMemoryDatabase(name));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
services.AddScoped<INoteStore, EfNoteStore>();
|
|
42
|
+
});
|
|
43
|
+
// @seed:anchor factory-configure
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected override void ConfigureClient(HttpClient client)
|
|
47
|
+
{
|
|
48
|
+
using var scope = Services.CreateScope();
|
|
49
|
+
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
50
|
+
db.Database.EnsureCreated();
|
|
51
|
+
base.ConfigureClient(client);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private static string ToNpgsql(string url)
|
|
55
|
+
{
|
|
56
|
+
if (url.StartsWith("postgresql://", StringComparison.OrdinalIgnoreCase) ||
|
|
57
|
+
url.StartsWith("postgres://", StringComparison.OrdinalIgnoreCase))
|
|
58
|
+
{
|
|
59
|
+
var uri = new Uri(url);
|
|
60
|
+
var user = Uri.UnescapeDataString(uri.UserInfo.Split(':')[0]);
|
|
61
|
+
var pass = uri.UserInfo.Contains(':')
|
|
62
|
+
? Uri.UnescapeDataString(uri.UserInfo.Split(':')[1])
|
|
63
|
+
: string.Empty;
|
|
64
|
+
var db = uri.AbsolutePath.Trim('/');
|
|
65
|
+
return $"Host={uri.Host};Port={(uri.Port > 0 ? uri.Port : 5432)};Database={db};Username={user};Password={pass}";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return url;
|
|
69
|
+
}
|
|
70
|
+
}
|