#106 Mis en place du nécessaire pour Postgresql et configuration "UseDatabase" et "IsSQLite" opérationnel.
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.5" />
|
||||
<PackageReference Include="NLog" Version="6.1.1" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.1" />
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@@ -43,6 +43,8 @@ namespace Webzine.EntitiesContext
|
||||
|
||||
modelBuilder.Entity<Artiste>(entity =>
|
||||
{
|
||||
entity.ToTable("Artistes");
|
||||
|
||||
entity.HasKey(a => a.IdArtiste)
|
||||
.HasName("PK_Artiste");
|
||||
|
||||
@@ -54,12 +56,16 @@ namespace Webzine.EntitiesContext
|
||||
|
||||
modelBuilder.Entity<Style>(entity =>
|
||||
{
|
||||
entity.ToTable("Styles");
|
||||
|
||||
entity.HasKey(s => s.IdStyle)
|
||||
.HasName("PK_Style");
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Commentaire>(entity =>
|
||||
{
|
||||
entity.ToTable("Commentaires");
|
||||
|
||||
entity.HasKey(c => c.IdCommentaire)
|
||||
.HasName("PK_Commentaire");
|
||||
|
||||
@@ -71,6 +77,8 @@ namespace Webzine.EntitiesContext
|
||||
|
||||
modelBuilder.Entity<Titre>(entity =>
|
||||
{
|
||||
entity.ToTable("Titres");
|
||||
|
||||
entity.HasKey(t => t.IdTitre)
|
||||
.HasName("PK_Titre");
|
||||
|
||||
|
||||
@@ -44,8 +44,8 @@ namespace Webzine.Entity.Fixtures
|
||||
Faker<Titre> faker = new Faker<Titre>("fr")
|
||||
.RuleFor(t => t.Libelle, f => f.Lorem.Sentence(3).Replace(".", string.Empty))
|
||||
.RuleFor(t => t.Chronique, f => f.Lorem.Paragraphs(3))
|
||||
.RuleFor(t => t.DateCreation, f => f.Date.Recent(120))
|
||||
.RuleFor(t => t.DateSortie, (f, t) => f.Date.Past(10, t.DateCreation))
|
||||
.RuleFor(t => t.DateCreation, f => DateTime.SpecifyKind(f.Date.Recent(120), DateTimeKind.Utc))
|
||||
.RuleFor(t => t.DateSortie, (f, t) => DateTime.SpecifyKind(f.Date.Past(10, t.DateCreation), DateTimeKind.Utc))
|
||||
.RuleFor(t => t.Duree, f => f.Random.Int(120, 420))
|
||||
.RuleFor(t => t.UrlJaquette, f => $"https://picsum.photos/seed/{Guid.NewGuid():N}/640/640")
|
||||
.RuleFor(t => t.UrlEcoute, f => $"https://example.com/listen/{Guid.NewGuid():N}")
|
||||
@@ -129,7 +129,7 @@ namespace Webzine.Entity.Fixtures
|
||||
Faker<Commentaire> faker = new Faker<Commentaire>("fr")
|
||||
.RuleFor(c => c.Auteur, f => f.Internet.UserName())
|
||||
.RuleFor(c => c.Contenu, f => f.Lorem.Sentences(2))
|
||||
.RuleFor(c => c.DateCreation, f => f.Date.Recent(60))
|
||||
.RuleFor(c => c.DateCreation, f => DateTime.SpecifyKind(f.Date.Recent(60), DateTimeKind.Utc))
|
||||
.RuleFor(c => c.Titre, _ => titre)
|
||||
.RuleFor(c => c.IdTitre, _ => titre.IdTitre);
|
||||
|
||||
|
||||
@@ -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
|
||||
{
|
||||
@@ -56,6 +64,8 @@ try
|
||||
var app = builder.Build();
|
||||
|
||||
if (useDatabase)
|
||||
{
|
||||
if (isSQLite)
|
||||
{
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
@@ -67,6 +77,19 @@ try
|
||||
}
|
||||
}
|
||||
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())
|
||||
{
|
||||
|
||||
@@ -6,13 +6,14 @@
|
||||
}
|
||||
},
|
||||
"Webzine": {
|
||||
// TODO : préciser les modes environnement et repo
|
||||
"NombreDerniereChronique": 3,
|
||||
"NombreDeTopTitres": 3
|
||||
},
|
||||
"UseDatabase": false,
|
||||
"UseDatabase": true,
|
||||
"IsSQLite": true,
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data Source=Data/webzine.sqlite"
|
||||
"SqliteConnection": "Data Source=Data/webzine.sqlite",
|
||||
"PostGreSQLConnection" : "Host=localhost;Port=5432;Username=admin;Password=admin123;Database=webzine_db"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user