Fix db seeding, migration, repository services

This commit is contained in:
danial23 2025-05-19 22:22:33 -04:00
parent 872dc1e263
commit 6b87902ca7
Signed by: danial23
SSH key fingerprint: SHA256:IJ8VP0j2WMUVweTYnzUUnEjNgPnGx+mAt+RhqWZ01bU
22 changed files with 606 additions and 64 deletions

View file

@ -5,6 +5,10 @@
<ProjectReference Include="..\CSR.Infrastructure\CSR.Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.5" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

View file

@ -1,6 +1,10 @@
using CSR.Infrastructure.Persistence;
using CSR.Infrastructure.Persistence.Repositories;
using CSR.Application.Services;
using CSR.Application.Interfaces;
using Microsoft.EntityFrameworkCore;
using Csr.Infrastructure.Persistence.Repositories;
using CSR.Infrastructure.Data;
var builder = WebApplication.CreateBuilder(args);
@ -8,6 +12,7 @@ var builder = WebApplication.CreateBuilder(args);
// Get configuration from appsettings.json, environment variables, and Docker secrets in that order
builder.Configuration
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", optional: true)
.AddEnvironmentVariables()
.AddKeyPerFile("/run/secrets", optional: true);
@ -28,22 +33,31 @@ builder.Services.AddDbContext<CSRDbContext>(options =>
options.UseSqlite($"Data Source={dbPath}");
});
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<Microsoft.AspNetCore.Identity.IPasswordHasher<CSR.Domain.Entities.User>, Microsoft.AspNetCore.Identity.PasswordHasher<CSR.Domain.Entities.User>>();
builder.Services.AddScoped<CSR.Domain.Interfaces.IPasswordHasher, CSR.Infrastructure.Services.PasswordHasherService>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IRoleRepository, RoleRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
var app = builder.Build();
// apply migrations and seed the database
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var config = services.GetRequiredService<IConfiguration>();
var context = services.GetRequiredService<CSRDbContext>();
var userService = services.GetRequiredService<UserService>();
var adminUsername = config["Admin:Username"] ?? "admin";
var adminEmail = config["Admin:Email"] ?? "";
var adminPassword = config["Admin:Password"] ?? "password";
userService.CreateUser(adminUsername, adminEmail, adminPassword, true);
try
{
var context = services.GetRequiredService<CSRDbContext>();
context.Database.Migrate();
await DbInitializer.SeedDatabase(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database");
}
}
// Configure the HTTP request pipeline.

View file

@ -5,5 +5,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Database": {
"Path": "/home/danial23/dl/csr.db"
}
}