#106 Mis en place du nécessaire pour Postgresql et configuration "UseDatabase" et "IsSQLite" opérationnel.

This commit is contained in:
Loic Masi
2026-03-27 11:28:10 +01:00
parent 8c724da7ae
commit db822e3ac5
5 changed files with 49 additions and 16 deletions

View File

@@ -23,22 +23,30 @@ try
// Necessite le package Nuget Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
.AddRazorRuntimeCompilation();
builder.Services.AddDbContext<WebzineDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
// NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders();
builder.Host.UseNLog();
// En fonction de la configuration, utilise soit les repositories basés sur une base de données, soit les repositories basés sur des listes locales.
bool useDatabase = builder.Configuration.GetValue<bool>("UseDatabase");
bool isSQLite = builder.Configuration.GetValue<bool>("IsSQLite");
if (useDatabase)
{
if (isSQLite)
{
builder.Services.AddDbContext<WebzineDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection")));
}
else
{
builder.Services.AddDbContext<WebzineDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("PostGreSQLConnection")));
}
builder.Services.AddScoped<DbEntityRepository>();
builder.Services.AddScoped<ITitreRepository, DbTitreRepository>();
builder.Services.AddScoped<IStyleRepository, DbStyleRepository>();
builder.Services.AddScoped<IArtisteRepository, DbArtisteRepository>();
//builder.Services.AddScoped<ICommentaireRepository, DbCommentaireRepository>();
builder.Services.AddScoped<DbEntityRepository>();
}
else
{
@@ -57,13 +65,28 @@ try
if (useDatabase)
{
using (var scope = app.Services.CreateScope())
if (isSQLite)
{
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
var repo = scope.ServiceProvider.GetRequiredService<DbEntityRepository>();
repo.SeedBaseDeDonnees();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
var repo = scope.ServiceProvider.GetRequiredService<DbEntityRepository>();
repo.SeedBaseDeDonnees();
}
}
else
{
using (var scope = app.Services.CreateScope())
{
// TODO : A modifier pour ne pas supprimer la base de donnée en prod
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
var repo = scope.ServiceProvider.GetRequiredService<DbEntityRepository>();
repo.SeedBaseDeDonnees();
}
}
}
else