Web Api layout

This commit is contained in:
Jöran Malek 2024-02-07 22:16:59 +01:00
parent 5619093f41
commit da6d5576bf
32 changed files with 515 additions and 29 deletions

View file

@ -0,0 +1,23 @@
using InkForge.Api.Data;
using Microsoft.EntityFrameworkCore;
namespace InkForge.Migrations;
public class ApiDbContextFactory : MigratingDbContextFactory<ApiDbcontext>
{
protected override void Configure(
DbContextOptionsBuilder<ApiDbcontext> optionsBuilder,
string connectionString,
string provider
) => _ = provider switch
{
"Sqlite" => optionsBuilder.UseSqlite(connectionString,
m => m.MigrationsAssembly("InkForge.Api.Sqlite")
),
_ => throw new Exception($"Invalid DbProvider: {provider}")
};
protected override ApiDbcontext CreateDbContext(DbContextOptions<ApiDbcontext> options) => new(options);
}

View file

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\migrations\InkForge.Api.Sqlite\InkForge.Api.Sqlite.csproj" />
<ProjectReference Include="..\..\shared\migrations\InkForge.Sqlite\InkForge.Sqlite.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace InkForge.Migrations;
public abstract class MigratingDbContextFactory<T> : IDesignTimeDbContextFactory<T>
where T : DbContext
{
public T CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var options = new DbContextOptionsBuilder<T>();
switch (configuration.GetValue<string>("DbProvider"))
{
case null:
throw new Exception("DbProvider not set.");
case { } provider:
Configure(options, configuration.GetConnectionString("DefaultConnection")!, provider);
break;
}
return CreateDbContext(options.Options);
}
protected abstract void Configure(DbContextOptionsBuilder<T> optionsBuilder, string connectionString, string provider);
protected abstract T CreateDbContext(DbContextOptions<T> options);
}

View file

@ -0,0 +1,23 @@
using InkForge.Data;
using Microsoft.EntityFrameworkCore;
namespace InkForge.Migrations;
public class NoteDbContextFactory : MigratingDbContextFactory<NoteDbContext>
{
protected override void Configure(
DbContextOptionsBuilder<NoteDbContext> optionsBuilder,
string connectionString,
string provider
) => _ = provider switch
{
"Sqlite" => optionsBuilder.UseSqlite(connectionString,
m => m.MigrationsAssembly("InkForge.Sqlite")
),
_ => throw new Exception($"Invalid DbProvider: {provider}")
};
protected override NoteDbContext CreateDbContext(DbContextOptions<NoteDbContext> options) => new(options);
}