#21 Création d'un ArtisteFactory qui génère des titres et deux albums par artiste. Le viewmodèle est passé dans la vue par le controleur.
This commit is contained in:
54
Webzine.Entity/Fixtures/ArtisteFactory.cs
Normal file
54
Webzine.Entity/Fixtures/ArtisteFactory.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Bogus" Version="35.6.5" />
|
||||
<PackageReference Include="Faker.Net" Version="2.0.163" />
|
||||
<PackageReference Include="NLog" Version="6.1.1" />
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
|
||||
|
||||
Reference in New Issue
Block a user