namespace Webzine.Entity.Fixtures
{
using Bogus;
///
/// Factory pour générer des artistes avec des titres associés, à l'aide de la bibliothèque Bogus.
///
public class ArtisteFactory
{
///
/// Récupère un artiste par son nom, en générant des données fictives pour ses titres associés.
///
/// Le nom de l'artiste à générer.
/// Un objet Artiste avec des titres associés générés de manière aléatoire.
public static Artiste SeedArtisteByName(string nom)
{
// On définit nos albums "bouchonnés"
var albumsData = new[]
{
new { Nom = "Bohemian Rhapsody", Image = "https://upload.wikimedia.org/wikipedia/en/9/9f/Bohemian_Rhapsody.png" },
new { Nom = "Born This Way", Image = "https://static.wikia.nocookie.net/ladygaga/images/2/2d/BornThisWay-DeluxeEdition.jpg/revision/latest/scale-to-width-down/3500?cb=20111120030308" },
};
var faker = new Bogus.Faker("fr");
var tousLesTitres = new List();
// Pour chaque album, on génère un paquet de titres
foreach (var album in albumsData)
{
var nombreDeTitres = faker.Random.Number(3, 6);
var titresDeLalbum = new Faker("fr")
.RuleFor(t => t.IdTitre, f => f.IndexFaker + 1 + tousLesTitres.Count)
.RuleFor(t => t.UrlJaquette, _ => album.Image)
.RuleFor(t => t.Album, _ => album.Nom)
.RuleFor(t => t.Duree, f => f.Random.Number(90, 180))
.RuleFor(t => t.Libelle, f => f.Music.Genre() + " - " + f.Commerce.ProductName())
.Generate(nombreDeTitres);
tousLesTitres.AddRange(titresDeLalbum);
}
// On crée l'artiste final
var artisteFaker = new Faker("fr")
.RuleFor(a => a.IdArtiste, f => f.IndexFaker + 1)
.RuleFor(a => a.Nom, _ => nom)
.RuleFor(a => a.Biographie, f => f.Lorem.Paragraphs(2))
.RuleFor(a => a.Titres, _ => tousLesTitres);
return artisteFaker.Generate();
}
}
}