refactor: optimiser la logique de génération de commentaires dans SeedDataLocal et les fichiers associés

This commit is contained in:
mirage
2026-03-27 15:07:40 +01:00
parent 30631676f8
commit 1eb8e93bfc
3 changed files with 17 additions and 11 deletions

View File

@@ -41,8 +41,7 @@ namespace Webzine.Entity.Fixtures
int count, int count,
List<Artiste> artistes, List<Artiste> artistes,
List<Style> styles, List<Style> styles,
List<string> albums, List<string> albums
List<Commentaire> commentaires
) )
{ {
Random random = new Random(); Random random = new Random();
@@ -61,7 +60,7 @@ namespace Webzine.Entity.Fixtures
.RuleFor(t => t.NbLikes, f => f.Random.Int(0, 1000)) .RuleFor(t => t.NbLikes, f => f.Random.Int(0, 1000))
.RuleFor(t => t.Album, f => f.PickRandom(albums)) .RuleFor(t => t.Album, f => f.PickRandom(albums))
.RuleFor(t => t.Artiste, f => f.PickRandom(artistes)) .RuleFor(t => t.Artiste, f => f.PickRandom(artistes))
.RuleFor(t => t.Commentaires, f => commentaires ?? new List<Commentaire>()); .RuleFor(t => t.Commentaires, f => new List<Commentaire>());
List<Titre> titres = faker.Generate(count); List<Titre> titres = faker.Generate(count);
foreach (Titre titre in titres) foreach (Titre titre in titres)
@@ -133,11 +132,10 @@ namespace Webzine.Entity.Fixtures
/// <param name="min">Le nombre minimum de commentaires pouvant être créés. La valeur par défaut est 0.</params> /// <param name="min">Le nombre minimum de commentaires pouvant être créés. La valeur par défaut est 0.</params>
/// <param name="max">Le nombre maximun de commentaires pouvant être créés. La valeur par défaut est 5.</params> /// <param name="max">Le nombre maximun de commentaires pouvant être créés. La valeur par défaut est 5.</params>
/// <returns>Liste de commentaire.</returns> /// <returns>Liste de commentaire.</returns>
public static List<Commentaire> GenererListeCommentaire(Titre titre, int min = 0, int max = 5) public static List<Commentaire> GenererListeCommentaire(Titre titre, int min = 0, int max = 5, int idStart = 1)
{ {
Random random = new Random(); Random random = new Random();
int count = random.Next(min, max + 1); int count = random.Next(min, max + 1);
int idStart = 1;
Faker<Commentaire> faker = new Faker<Commentaire>("fr") Faker<Commentaire> faker = new Faker<Commentaire>("fr")
.RuleFor(a => a.IdCommentaire, f => f.IndexFaker + idStart) .RuleFor(a => a.IdCommentaire, f => f.IndexFaker + idStart)

View File

@@ -46,11 +46,17 @@ namespace Webzine.Repository
this.context.SaveChanges(); this.context.SaveChanges();
List<string> albums = SeedDataLocal.GenererListeAlbums(3); List<string> albums = SeedDataLocal.GenererListeAlbums(3);
List<Commentaire> commentaires = new List<Commentaire>(); List<Titre> titres = SeedDataLocal.GenererListeTitre(nbTitres, artistes, styles, albums);
List<Titre> titres = SeedDataLocal.GenererListeTitre(nbTitres, artistes, styles, albums, commentaires);
this.context.Titres.AddRange(titres);
this.context.SaveChanges();
int commentaireIdStart = 1;
foreach (var titre in titres)
{
var commentairesDuTitre = SeedDataLocal.GenererListeCommentaire(titre, 0, 5, commentaireIdStart);
titre.Commentaires.AddRange(commentairesDuTitre);
commentaireIdStart += commentairesDuTitre.Count;
}
this.context.Titres.AddRange(titres);
this.context.SaveChanges(); this.context.SaveChanges();
} }
} }

View File

@@ -99,11 +99,13 @@ try
var styles = SeedDataLocal.GenererListeStyle(15, 20); var styles = SeedDataLocal.GenererListeStyle(15, 20);
var albums = SeedDataLocal.GenererListeAlbums(50); var albums = SeedDataLocal.GenererListeAlbums(50);
var commentaires = new List<Commentaire>(); var commentaires = new List<Commentaire>();
var titres = SeedDataLocal.GenererListeTitre(500, artistes, styles, albums, commentaires); var titres = SeedDataLocal.GenererListeTitre(500, artistes, styles, albums);
foreach (var titre in titres) foreach (var titre in titres)
{ {
commentaires.AddRange(SeedDataLocal.GenererListeCommentaire(titre, 0, 5)); var commentairesForTitre = SeedDataLocal.GenererListeCommentaire(titre, 0, 5);
titre.Commentaires.AddRange(commentairesForTitre);
commentaires.AddRange(commentairesForTitre);
} }
store.Artistes.AddRange(artistes); store.Artistes.AddRange(artistes);