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

173 lines
6.5 KiB
C#

// <copyright file="SeedDataLocal.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace Webzine.Entity.Fixtures
{
using Bogus;
public class SeedDataLocal
{
public SeedDataLocal()
{
}
/// <summary>
/// Generer une liste d'artiste.
/// </summary>
/// <param name="nombre">Nombre d'artiste.</param>
/// <returns>Liste d'artiste.</returns>
public static List<Artiste> GenererListeArtiste(int nombre)
{
int idStart = 1;
Faker<Artiste> artistes = new Faker<Artiste>("fr")
.RuleFor(a => a.IdArtiste, f => f.IndexFaker + idStart) // Créé les id des artistes de manière incrémentale
.RuleFor(a => a.Nom, f => f.Person.FullName)
.RuleFor(a => a.Biographie, f => f.Lorem.Paragraph(2));
return artistes.Generate(nombre);
}
/// <summary>
/// Generer une liste de titres.
/// </summary>
/// <param name="count">Nombre de titres à créer.</param>
/// <param name="artistes">Liste d'artistes.</param>
/// <param name="styles">Liste de styles.</param>
/// <param name="albums">Liste d'albums.</param>
/// <param name="commentaires">Liste de commentaires.</param>
/// <returns>Liste de titres.</returns>
public static List<Titre> GenererListeTitre(
int count,
List<Artiste> artistes,
List<Style> styles,
List<string> albums,
List<Commentaire> commentaires
)
{
Random random = new Random();
int idStart = 1;
Faker<Titre> faker = new Faker<Titre>("fr")
.RuleFor(a => a.IdTitre, f => f.IndexFaker + idStart)
.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 => DateTime.SpecifyKind(f.Date.Recent(120), DateTimeKind.Utc))
.RuleFor(t => t.DateSortie, (f, t) => DateTime.SpecifyKind(f.Date.Past(10, t.DateCreation), DateTimeKind.Utc))
.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.PickRandom(albums))
.RuleFor(t => t.Artiste, f => f.PickRandom(artistes))
.RuleFor(t => t.Commentaires, f => commentaires ?? new List<Commentaire>());
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;
}
/// <summary>
/// Générer une liste de styles pour seeder la base
/// de données.
/// </summary>
/// <param name="minCount">Le nombre minimum de styles pouvant être créés.</params>
/// <param name="maxCount">Le nombre maximun de styles pouvant être créés.</params>
/// <returns>Liste de styles.</returns>
public static List<Style> GenererListeStyle(int minCount = 15, int maxCount = 20)
{
List<string> libelles = new List<string>
{
"Pop",
"Rock",
"Jazz",
"Blues",
"Hip-Hop",
"Rap",
"Electro",
"Techno",
"House",
"Metal",
"Funk",
"Soul",
"R&B",
"Classique",
"Reggae",
"Punk",
"Folk",
"Disco",
"Ambient",
"Indie",
};
Random random = new Random();
int count = random.Next(minCount, maxCount + 1);
return libelles
.Take(count)
.Select((libelle, index) => new Style
{
IdStyle = index + 1,
Libelle = libelle,
})
.ToList();
}
/// <summary>
/// Générer une liste de commentaires pour seeder la base
/// de données.
/// </summary>
/// <param name="titre">Titre.</param>
/// <param name="min">Le nombre minimum de commentaires pouvant être créés. La valeur par défaut est 0.</params>
/// <param name="max">Le nombre maximun de commentaires pouvant être créés. La valeur par défaut est 5.</params>
/// <returns>Liste de commentaire.</returns>
public static List<Commentaire> GenererListeCommentaire(Titre titre, int min = 0, int max = 5)
{
Random random = new Random();
int count = random.Next(min, max + 1);
int idStart = 1;
Faker<Commentaire> faker = new Faker<Commentaire>("fr")
.RuleFor(a => a.IdCommentaire, f => f.IndexFaker + idStart)
.RuleFor(c => c.Auteur, f => f.Internet.UserName())
.RuleFor(c => c.Contenu, f => f.Lorem.Sentences(2))
.RuleFor(c => c.DateCreation, f => DateTime.SpecifyKind(f.Date.Recent(60), DateTimeKind.Utc))
.RuleFor(c => c.Titre, _ => titre)
.RuleFor(c => c.IdTitre, _ => titre.IdTitre);
return faker.Generate(count);
}
/// <summary>
/// Générer une liste d'albums pour seeder la base
/// de données.
/// </summary>
/// <param name="nombre">Le nombre d'albums à générer.</param>
/// <returns>Liste d'albums.</returns>
public static List<string> GenererListeAlbums(int nombre)
{
Faker faker = new Faker("fr");
HashSet<string> albums = new HashSet<string>();
while (albums.Count < nombre)
{
albums.Add(faker.Company.CatchPhrase());
}
return albums.ToList();
}
}
}