refactor: #150 mettre à jour appsettings et introduire des enums pour les types Seeder et Repository

This commit is contained in:
mirage
2026-03-31 10:07:47 +02:00
parent c65c0113af
commit 63e755c5c7
7 changed files with 39 additions and 52 deletions

View File

@@ -11,20 +11,10 @@ jobs:
- name: Checkout PR branch - name: Checkout PR branch
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Configure appsettings for CI - name: Configure environment for CI
run: | run: |
# Find the appsettings.json file echo "ASPNETCORE_ENVIRONMENT=Development" >> $ACT_ENV
APPSETTINGS_PATH="Webzine.WebApplication/appsettings.json" echo "Repository=Db" >> $ACT_ENV
# Backup original file
cp $APPSETTINGS_PATH $APPSETTINGS_PATH.bak
# Use jq to modify the JSON
jq '.UseDatabase = true | .IsSQLite = true' $APPSETTINGS_PATH > $APPSETTINGS_PATH.tmp
mv $APPSETTINGS_PATH.tmp $APPSETTINGS_PATH
echo "Updated appsettings.json:"
cat $APPSETTINGS_PATH
- name: Setup .NET 10 - name: Setup .NET 10
uses: actions/setup-dotnet@v4 uses: actions/setup-dotnet@v4

View File

@@ -7,7 +7,7 @@ EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release ARG BUILD_CONFIGURATION=Release
WORKDIR /src WORKDIR /src
COPY ["Webzine.WebApplication/Webzine.WebApplication.csproj", "Webzine.WebApplication/"] COPY ["./Webzine.WebApplication/Webzine.WebApplication.csproj", "Webzine.WebApplication/"]
RUN dotnet restore "Webzine.WebApplication/Webzine.WebApplication.csproj" RUN dotnet restore "Webzine.WebApplication/Webzine.WebApplication.csproj"
COPY . . COPY . .
WORKDIR "/src/Webzine.WebApplication" WORKDIR "/src/Webzine.WebApplication"

View File

@@ -0,0 +1,13 @@
namespace Webzine.WebApplication.Configuration;
public enum SeederType
{
Local,
Spotify
}
public enum RepositoryType
{
Local,
Db
}

View File

@@ -8,6 +8,7 @@ using Webzine.Entity;
using Webzine.Entity.Fixtures; using Webzine.Entity.Fixtures;
using Webzine.Repository; using Webzine.Repository;
using Webzine.Repository.Contracts; using Webzine.Repository.Contracts;
using Webzine.WebApplication.Configuration;
using Webzine.WebApplication.Extensions; using Webzine.WebApplication.Extensions;
// Initiation du logger NLog pour la classe courante afin de pouvoir l'utiliser pour logger des messages d'information, d'erreur, etc avant la construction de l'application. // Initiation du logger NLog pour la classe courante afin de pouvoir l'utiliser pour logger des messages d'information, d'erreur, etc avant la construction de l'application.
@@ -32,19 +33,19 @@ try
builder.Host.UseNLog(); 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. // 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"); var repositoryType = builder.Configuration.GetValue<RepositoryType>("Repository");
bool isSQLite = builder.Configuration.GetValue<bool>("IsSQLite"); var seederType = builder.Configuration.GetValue<SeederType>("Seeder");
if (useDatabase) if (repositoryType == RepositoryType.Db)
{ {
if (isSQLite) if (builder.Environment.IsProduction())
{ {
builder.Services.AddDbContext<WebzineDbContext>(options => builder.Services.AddDbContext<WebzineDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection"))); options.UseNpgsql(builder.Configuration.GetConnectionString("PostGreSQLConnection")));
} }
else else
{ {
builder.Services.AddDbContext<WebzineDbContext>(options => builder.Services.AddDbContext<WebzineDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("PostGreSQLConnection"))); options.UseSqlite(builder.Configuration.GetConnectionString("SqliteConnection")));
} }
builder.Services.AddScoped<DbEntityRepository>(); builder.Services.AddScoped<DbEntityRepository>();
@@ -68,9 +69,7 @@ try
var app = builder.Build(); var app = builder.Build();
if (useDatabase) if (repositoryType == RepositoryType.Db)
{
if (isSQLite)
{ {
using (var scope = app.Services.CreateScope()) using (var scope = app.Services.CreateScope())
{ {
@@ -82,19 +81,6 @@ try
} }
} }
else 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
{ {
using (var scope = app.Services.CreateScope()) using (var scope = app.Services.CreateScope())
{ {

View File

@@ -1,8 +1,4 @@
{ {
"Logging": { "Seeder": "Local",
"LogLevel": { "Repository": "Db"
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
} }

View File

@@ -0,0 +1,4 @@
{
"Seeder": "Local",
"Repository": "Db"
}

View File

@@ -9,11 +9,9 @@
"NombreDerniereChronique": 3, "NombreDerniereChronique": 3,
"NombreDeTopTitres": 3 "NombreDeTopTitres": 3
}, },
"UseDatabase": true,
"IsSQLite": true,
"ConnectionStrings": { "ConnectionStrings": {
"SqliteConnection": "Data Source=Data/webzine.sqlite", "SqliteConnection": "Data Source=Data/webzine.sqlite",
"PostGreSQLConnection" : "Host=localhost;Port=5432;Username=admin;Password=admin123;Database=webzine_db" "PostGreSQLConnection": "Host=192.168.60.50;Port=5432;Username=postgres;Password=BcS4e6DHr2NgVrnPomVI;Database=webzine_prod"
}, },
"AllowedHosts": "*" "AllowedHosts": "*"
} }