#87 Modification des factories pour pouvoir seeder la base de données et paramétrage de la fonction pour choisir le nombre d'éléments souhaité

This commit is contained in:
Loic Masi
2026-03-25 16:41:50 +01:00
parent 849f294418
commit 9801eb555f
8 changed files with 224 additions and 39 deletions

View File

@@ -49,5 +49,20 @@ namespace Webzine.Entity.Fixtures
return artisteFaker.Generate();
}
/// <summary>
/// Permet de retourner une liste d'artiste pour seeder la base
/// de données.
/// </summary>
/// <param name="nombre"></param>
/// <returns>Liste d'artiste.</returns>
public static List<Artiste> CreerListeArtiste(int nombre)
{
Faker<Artiste> faker = new Faker<Artiste>("fr")
.RuleFor(a => a.Nom, f => f.Person.FullName)
.RuleFor(a => a.Biographie, f => f.Lorem.Paragraph(2));
return faker.Generate(nombre);
}
}
}

View File

@@ -0,0 +1,33 @@
// <copyright file="CommentaireFactory.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
using Bogus;
namespace Webzine.Entity.Fixtures
{
public class CommentaireFactory
{
/// <summary>
/// Seeder pour la base de données.
/// </summary>
/// <param name="titre">Titre.</param>
/// <param name="min">Min.</param>
/// <param name="max">Max.</param>
/// <returns>Liste de commentaire.</returns>
public static List<Commentaire> CreerListeCommentaire(Titre titre, int min = 0, int max = 5)
{
Random random = new Random();
int count = random.Next(min, max + 1);
Faker<Commentaire> faker = new Faker<Commentaire>("fr")
.RuleFor(c => c.Auteur, f => f.Internet.UserName())
.RuleFor(c => c.Contenu, f => f.Lorem.Sentences(2))
.RuleFor(c => c.DateCreation, f => f.Date.Recent(60))
.RuleFor(c => c.Titre, _ => titre)
.RuleFor(c => c.IdTitre, _ => titre.IdTitre);
return faker.Generate(count);
}
}
}

View File

@@ -0,0 +1,53 @@
// <copyright file="StyleFactory.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace Webzine.Entity.Fixtures
{
using Webzine.Entity;
public class StyleFactory
{
/// <summary>
/// Générer une liste de style pour seeder la base
/// de données.
/// </summary>
/// <returns>Liste de style.</returns>
public static List<Style> CreerListeStyle(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 => new Style
{
Libelle = libelle,
})
.ToList();
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Bogus;
using Faker;
using System;
using System.Collections.Generic;
using Webzine.Entity;
namespace Webzine.Repository.Fake
@@ -73,4 +74,50 @@ namespace Webzine.Repository.Fake
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;
}
}
}