Files
webzine/Webzine.Entity/Fixtures/ArtisteFactory.cs

54 lines
2.2 KiB
C#

using Bogus;
namespace Webzine.Entity.Fixtures
{
/// <summary>
/// Factory pour générer des artistes avec des titres associés, à l'aide de la bibliothèque Bogus.
///
/// </summary>
public class ArtisteFactory
{
/// <summary>
/// Récupère un artiste par son nom, en générant des données fictives pour ses titres associés.
/// </summary>
/// <param name="nom"></param>
/// <returns></returns>
public static Artiste FindByName(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<Titre>();
// 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<Titre>("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<Artiste>("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();
}
}
}