Ajout du seeder de la base de données

This commit is contained in:
Loic Masi
2026-03-26 15:02:34 +01:00
parent 49bf4a024b
commit 5c7af80f1b
4 changed files with 88 additions and 14 deletions

View File

@@ -1,6 +1,14 @@
namespace Webzine.Entity.Fixtures; // <copyright file="SeedDataLocal.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
public class SeedDataLocal namespace Webzine.Entity.Fixtures
{ {
public class SeedDataLocal
{
public SeedDataLocal()
{
}
}
} }

View File

@@ -0,0 +1,63 @@
// <copyright file="DbEntityRepository.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace Webzine.Repository
{
using Webzine.EntitiesContext;
using Webzine.Entity;
using Webzine.Entity.Fixtures;
using Webzine.Repository.Fake;
public class DbEntityRepository
{
private readonly WebzineDbContext context;
public DbEntityRepository(WebzineDbContext context)
{
this.context = context;
}
public void SeedBaseDeDonnees(
int nbArtistes = 100,
int nbTitres = 500,
int minStyles = 15,
int maxStyles = 20,
int minCommentairesParTitre = 0,
int maxCommentairesParTitre = 5)
{
if (this.context.Artistes.Any() ||
this.context.Titres.Any() ||
this.context.Styles.Any() ||
this.context.Commentaires.Any())
{
return;
}
List<Artiste> artistes = ArtisteFactory.CreerListeArtiste(nbArtistes);
List<Style> styles = StyleFactory.CreerListeStyle(minStyles, maxStyles);
this.context.Artistes.AddRange(artistes);
this.context.Styles.AddRange(styles);
this.context.SaveChanges();
List<Titre> titres = TitreFactory.CreerListeTitre(nbTitres, artistes, styles);
this.context.Titres.AddRange(titres);
this.context.SaveChanges();
List<Commentaire> commentaires = new List<Commentaire>();
foreach (Titre titre in titres)
{
commentaires.AddRange(
CommentaireFactory.CreerListeCommentaire(
titre,
minCommentairesParTitre,
maxCommentairesParTitre));
}
this.context.Commentaires.AddRange(commentaires);
this.context.SaveChanges();
}
}
}

View File

@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;
using NLog; using NLog;
using NLog.Web; using NLog.Web;
using Microsoft.EntityFrameworkCore;
using Webzine.EntitiesContext; using Webzine.EntitiesContext;
using Webzine.Repository; using Webzine.Repository;
using Webzine.Repository.Contracts; using Webzine.Repository.Contracts;
@@ -35,14 +35,15 @@ try
builder.Services.AddScoped<ITitreRepository, DbTitreRepository>(); builder.Services.AddScoped<ITitreRepository, DbTitreRepository>();
builder.Services.AddScoped<IStyleRepository, DbStyleRepository>(); builder.Services.AddScoped<IStyleRepository, DbStyleRepository>();
builder.Services.AddScoped<IArtisteRepository, DbArtisteRepository>(); builder.Services.AddScoped<IArtisteRepository, DbArtisteRepository>();
// builder.Services.AddScoped<ICommentaireRepository, DbCommentaireRepository>(); //builder.Services.AddScoped<ICommentaireRepository, DbCommentaireRepository>();
builder.Services.AddScoped<DbEntityRepository>();
} }
else else
{ {
builder.Services.AddScoped<ITitreRepository, LocalTitreRepository>(); builder.Services.AddScoped<ITitreRepository, LocalTitreRepository>();
builder.Services.AddScoped<IStyleRepository, LocalStyleRepository>(); builder.Services.AddScoped<IStyleRepository, LocalStyleRepository>();
builder.Services.AddScoped<IArtisteRepository, LocalArtisteRepository>(); builder.Services.AddScoped<IArtisteRepository, LocalArtisteRepository>();
// builder.Services.AddScoped<ICommentaireRepository, LocalCommentaireRepository>(); //builder.Services.AddScoped<ICommentaireRepository, LocalCommentaireRepository>();
} }
// https://learn.microsoft.com/fr-fr/aspnet/core/performance/response-compression?view=aspnetcore-10.0#configuration // https://learn.microsoft.com/fr-fr/aspnet/core/performance/response-compression?view=aspnetcore-10.0#configuration
@@ -51,6 +52,15 @@ try
var app = builder.Build(); var app = builder.Build();
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();
}
app.UseResponseCompression(); app.UseResponseCompression();
// Active la possibilite de servir des fichiers statiques presents dans // Active la possibilite de servir des fichiers statiques presents dans
@@ -64,13 +74,6 @@ try
}, },
}); });
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
}
// Active le middleware permettant le routage des requetes entrantes. // Active le middleware permettant le routage des requetes entrantes.
app.UseRouting(); app.UseRouting();

View File

@@ -19,7 +19,7 @@
</main> </main>
@if(ViewContext.RouteData.Values["area"]?.ToString() != "Administration") @if(ViewContext.RouteData.Values["area"]?.ToString() != "Administration")
{ {
@await Component.InvokeAsync("Components.Sidebar.Default") @await Component.InvokeAsync("Sidebar")
} }
</div> </div>
</div> </div>