124 lines
4.7 KiB
C#
124 lines
4.7 KiB
C#
using Bogus;
|
|
using Faker;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Webzine.Entity;
|
|
|
|
namespace Webzine.Repository.Fake
|
|
{
|
|
/// <summary>
|
|
/// Classe de fabrique pour générer des données factices (fake data) pour les entités Artiste et Titre.
|
|
/// </summary>
|
|
public static class FakeDataFactory
|
|
{
|
|
//https://cdn-images.dzcdn.net/images/cover/311bba0fc112d15f72c8b5a65f0456c1/1900x1900-000000-80-0-0.jpg",
|
|
/// <summary>
|
|
/// Génère une liste d'artistes avec des données factices, incluant des titres associés à chaque artiste.
|
|
/// </summary>
|
|
/// <param name="count">Le nombre d'artistes à générer. Par défaut, 10 artistes seront générés.</param>
|
|
/// <returns>Une liste d'objets Artiste avec des titres associés, générés de manière aléatoire.</returns>
|
|
public static List<Artiste> GetArtistes(int count = 10)
|
|
{
|
|
var artistes = new List<Artiste>();
|
|
|
|
for (int i = 1; i <= count; i++)
|
|
{
|
|
artistes.Add(new Artiste
|
|
{
|
|
IdArtiste = i,
|
|
Nom = Name.FullName(),
|
|
Biographie = Lorem.Paragraph(),
|
|
Titres = new List<Titre>()
|
|
});
|
|
}
|
|
|
|
return artistes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Génère une liste de titres avec des données factices, en associant chaque titre à un artiste existant.
|
|
/// </summary>
|
|
/// <param name="count">Le nombre de titres à générer. Par défaut, 40 titres seront générés.</param>
|
|
/// <returns>Une liste d'objets Titre avec des données factices, associés à des artistes générés de manière aléatoire.</returns>
|
|
public static List<Titre> GetTitres(int count = 40)
|
|
{
|
|
var artistes = GetArtistes();
|
|
var titres = new List<Titre>();
|
|
|
|
for (int i = 1; i <= count; i++)
|
|
{
|
|
var artiste = artistes[RandomNumber.Next(0, artistes.Count - 1)];
|
|
|
|
var titre = new Titre
|
|
{
|
|
IdTitre = i,
|
|
IdArtiste = artiste.IdArtiste,
|
|
Artiste = artiste,
|
|
Libelle = Lorem.Sentence(3),
|
|
Chronique = Lorem.Paragraph(),
|
|
DateCreation = DateTime.Now.AddDays(-RandomNumber.Next(1, 100)),
|
|
DateSortie = DateTime.Now.AddYears(-RandomNumber.Next(1, 20)),
|
|
Duree = RandomNumber.Next(120, 420),
|
|
UrlJaquette = "https://picsum.photos/300",
|
|
UrlEcoute = Internet.Url(),
|
|
NbLectures = RandomNumber.Next(0, 500),
|
|
NbLikes = RandomNumber.Next(0, 200),
|
|
Album = Lorem.Sentence(2),
|
|
Commentaires = new List<Commentaire>()
|
|
};
|
|
|
|
titres.Add(titre);
|
|
artiste.Titres.Add(titre);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|