#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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Webzine.Entity/Fixtures/CommentaireFactory.cs
Normal file
33
Webzine.Entity/Fixtures/CommentaireFactory.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Webzine.Entity/Fixtures/StyleFactory.cs
Normal file
53
Webzine.Entity/Fixtures/StyleFactory.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,69 @@
|
||||
namespace Webzine.Repository;
|
||||
// <copyright file="DbEntityRepository.cs" company="PlaceholderCompany">
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
public class DbEntityRepository
|
||||
namespace Webzine.Repository
|
||||
{
|
||||
|
||||
using Webzine.EntitiesContext;
|
||||
using Webzine.Entity;
|
||||
using Webzine.Entity.Fixtures;
|
||||
using Webzine.Repository.Contracts;
|
||||
using Webzine.Repository.Fake;
|
||||
|
||||
public class DbEntityRepository
|
||||
{
|
||||
private readonly WebzineDbContext context;
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DbEntityRepository"/> class.
|
||||
/// </summary>
|
||||
public DbEntityRepository(WebzineDbContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seed de la base de données à partir de données générées.
|
||||
/// </summary>
|
||||
public void SeedBaseDeDonnees(
|
||||
int nbArtistes = 100,
|
||||
int nbTitres = 500,
|
||||
int minStyles = 15,
|
||||
int maxStyles = 20,
|
||||
int minCommentairesParTitre = 0,
|
||||
int maxCommentairesParTitre = 5)
|
||||
{
|
||||
if (this.context.Artistes.Any() ||
|
||||
this.context.Titres.Any() ||
|
||||
this.context.Styles.Any() ||
|
||||
this.context.Commentaires.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
List<Artiste> artistes = ArtisteFactory.CreerListeArtiste(nbArtistes);
|
||||
List<Style> styles = StyleFactory.CreerListeStyle(minStyles, maxStyles);
|
||||
|
||||
this.context.Artistes.AddRange(artistes);
|
||||
this.context.Styles.AddRange(styles);
|
||||
this.context.SaveChanges();
|
||||
|
||||
List<Titre> titres = TitreFactory.CreerListeTitre(nbTitres, artistes, styles);
|
||||
this.context.Titres.AddRange(titres);
|
||||
this.context.SaveChanges();
|
||||
|
||||
List<Commentaire> commentaires = new List<Commentaire>();
|
||||
|
||||
foreach (Titre titre in titres)
|
||||
{
|
||||
commentaires.AddRange(
|
||||
CommentaireFactory.CreerListeCommentaire(
|
||||
titre,
|
||||
minCommentairesParTitre,
|
||||
maxCommentairesParTitre));
|
||||
}
|
||||
|
||||
this.context.Commentaires.AddRange(commentaires);
|
||||
this.context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Webzine.EntitiesContext\Webzine.EntitiesContext.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Repository.Contracts\Webzine.Repository.Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -21,11 +21,15 @@ try
|
||||
// Necessite le package Nuget Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
|
||||
.AddRazorRuntimeCompilation();
|
||||
|
||||
|
||||
builder.Services.AddSingleton<ITitreRepository, LocalEntityRepository>();
|
||||
|
||||
builder.Services.AddDbContext<WebzineDbContext>(options =>
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||
|
||||
// Ajout d'un seeder pour la base de données
|
||||
builder.Services.AddScoped<DbEntityRepository>();
|
||||
|
||||
// NLog: Setup NLog for Dependency injection
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Host.UseNLog();
|
||||
@@ -39,7 +43,10 @@ try
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
|
||||
db.Database.EnsureDeleted();
|
||||
db.Database.EnsureCreated();
|
||||
var repo = scope.ServiceProvider.GetRequiredService<DbEntityRepository>();
|
||||
repo.SeedBaseDeDonnees();
|
||||
}
|
||||
|
||||
// Active le middleware permettant le routage des requetes entrantes.
|
||||
|
||||
@@ -12,40 +12,6 @@
|
||||
<h2>Styles</h2>
|
||||
<ul>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Acid house">Acid house</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Ambient">Ambient</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Deep house">Deep house</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Disco">Disco</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Downtempo">Downtempo</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Drum n bass">Drum n bass</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Dub Techno">Dub Techno</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Electro">Electro</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Electronic">Electronic</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Experimental">Experimental</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Funk">Funk</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Garage">Garage</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Hardcore">Hardcore</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Hardstyle">Hardstyle</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Hip hop">Hip hop</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="House">House</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Indie">Indie</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Industrial">Industrial</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Jazz">Jazz</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Latin">Latin</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Metal">Metal</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Minimal">Minimal</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Pop">Pop</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Progressive">Progressive</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Punk">Punk</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="R&B">R&B</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Rap">Rap</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Reggae">Reggae</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Rock">Rock</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Soul">Soul</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Techno">Techno</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Trance">Trance</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="Trip hop">Trip hop</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="UK garage">UK garage</a></li>
|
||||
<li><a asp-controller="Titre" asp-action="Style" asp-route-id="World">World</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</aside>
|
||||
Reference in New Issue
Block a user