#87 Modification des factories pour pouvoir seeder la base de données et paramétrage de la fonction pour choisir le nombre d'éléments souhaité

This commit is contained in:
Loic Masi
2026-03-25 16:41:50 +01:00
parent 849f294418
commit 9801eb555f
8 changed files with 224 additions and 39 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Bogus;
using Faker;
using System;
using System.Collections.Generic;
using Webzine.Entity;
namespace Webzine.Repository.Fake
@@ -73,4 +74,50 @@ namespace Webzine.Repository.Fake
return titres;
}
}
public class TitreFactory
{
/// <summary>
/// Initializes a new instance of the <see cref="TitreFactory"/> class.
/// </summary>
public TitreFactory()
{
}
public static List<Titre> CreerListeTitre(
int count,
List<Artiste> artistes,
List<Style> styles)
{
Random random = new Random();
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.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}")
.RuleFor(t => t.NbLectures, f => f.Random.Int(0, 5000))
.RuleFor(t => t.NbLikes, f => f.Random.Int(0, 1000))
.RuleFor(t => t.Album, f => f.Company.CatchPhrase())
.RuleFor(t => t.Artiste, f => f.PickRandom(artistes));
List<Titre> titres = faker.Generate(count);
foreach (Titre titre in titres)
{
int nbStyles = random.Next(1, 4);
titre.Styles = styles
.OrderBy(_ => Guid.NewGuid())
.Take(nbStyles)
.ToList();
titre.IdArtiste = titre.Artiste.IdArtiste;
}
return titres;
}
}
}