Changed the way the test-data is generated
This commit is contained in:
201
src/Web/TestDataSeeder.cs
Normal file
201
src/Web/TestDataSeeder.cs
Normal file
@@ -0,0 +1,201 @@
|
||||
using Hutopy.Domain.Constants;
|
||||
using Hutopy.Infrastructure.Identity;
|
||||
using Hutopy.Infrastructure.Identity.OwnedEntities;
|
||||
using Hutopy.Web.Common;
|
||||
using Hutopy.Web.Contents.Data;
|
||||
using Hutopy.Web.Messages.Data;
|
||||
|
||||
namespace Hutopy.Web;
|
||||
|
||||
public static class WebApplicationExtensions
|
||||
{
|
||||
public static async Task SeedDatabaseWithTestDataOnlyIfNoDataIsPresentAsync(
|
||||
this WebApplication app,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
using var scope = app.Services.CreateScope();
|
||||
|
||||
var seeder = new TestDataSeeder(
|
||||
scope.ServiceProvider.GetRequiredService<ApplicationUserManager>(),
|
||||
scope.ServiceProvider.GetRequiredService<ContentDbContext>(),
|
||||
scope.ServiceProvider.GetRequiredService<MessagingDbContext>());
|
||||
|
||||
await seeder.SeedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
internal class TestDataSeeder(
|
||||
ApplicationUserManager userManager,
|
||||
ContentDbContext contentContext,
|
||||
MessagingDbContext messagingContext)
|
||||
{
|
||||
private const string DefaultPassword = "Test123#";
|
||||
|
||||
public async Task SeedAsync()
|
||||
{
|
||||
if (contentContext.Contents.Any()) return;
|
||||
|
||||
_ = await CreateAdministratorAsync("admin");
|
||||
_ = await CreateUserAsync("userA");
|
||||
_ = await CreateUserAsync("userB");
|
||||
|
||||
foreach (var creator in _creators)
|
||||
{
|
||||
_ = await CreateCreatorAsync(creator);
|
||||
|
||||
var contents = GenerateContent(creator, 100);
|
||||
foreach (var content in contents)
|
||||
{
|
||||
var messages = GenerateMessages(content, 100);
|
||||
var parentMessages = messages.Where((_, index) => index % 2 == 0).ToList();
|
||||
|
||||
foreach (var parentMessage in parentMessages)
|
||||
{
|
||||
_ = GenerateReplies(content, parentMessage, 10);
|
||||
}
|
||||
|
||||
await messagingContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Content> GenerateContent(ApplicationUser user, int contentCount)
|
||||
{
|
||||
var contents = new List<Content>();
|
||||
|
||||
for (var c = 0; c < contentCount; c++)
|
||||
{
|
||||
var content = new Content
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
CreatedBy = Guid.Parse(user.Id),
|
||||
Title = $"Title {user.UserName}-{c}",
|
||||
Description = $"Description {user.UserName}-{c}"
|
||||
};
|
||||
|
||||
contentContext.Contents.Add(content);
|
||||
|
||||
contents.Add(content);
|
||||
}
|
||||
|
||||
contentContext.SaveChanges();
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
private List<Message> GenerateMessages(Content content, int messageCount)
|
||||
{
|
||||
var messages = new List<Message>();
|
||||
|
||||
for (var m = 0; m < messageCount; m++)
|
||||
{
|
||||
var author = Random.Shared.GetItems(_creators, 1)[0];
|
||||
|
||||
var message = new Message
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
SubjectId = content.Id,
|
||||
CreatedBy = Guid.Parse(author.Id),
|
||||
Value = $"Message #{m} from {author.UserName} on {content.Title}"
|
||||
};
|
||||
|
||||
messagingContext.Messages.Add(message);
|
||||
|
||||
messages.Add(message);
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
private List<Message> GenerateReplies(Content content, Message parent, int replyCount)
|
||||
{
|
||||
var replies = new List<Message>();
|
||||
|
||||
for (var r = 0; r < replyCount; r++)
|
||||
{
|
||||
var author = Random.Shared.GetItems(_creators, 1)[0];
|
||||
|
||||
var message = new Message
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
SubjectId = content.Id,
|
||||
ParentId = parent.Id,
|
||||
CreatedBy = Guid.Parse(author.Id),
|
||||
Value = $"Reply {r} to {parent.Value}"
|
||||
};
|
||||
|
||||
messagingContext.Messages.Add(message);
|
||||
|
||||
replies.Add(message);
|
||||
}
|
||||
|
||||
return replies;
|
||||
}
|
||||
|
||||
private async Task<ApplicationUser> CreateAdministratorAsync(string name)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(name);
|
||||
|
||||
var administrator = new ApplicationUser { UserName = $"{name}@test", Email = $"{name}@test" };
|
||||
|
||||
await userManager.CreateAsync(administrator, DefaultPassword);
|
||||
await userManager.AddToRolesAsync(administrator, new[] { Roles.Administrator });
|
||||
|
||||
return administrator;
|
||||
}
|
||||
|
||||
private async Task<ApplicationUser> CreateUserAsync(string name)
|
||||
{
|
||||
var user = new ApplicationUser
|
||||
{
|
||||
UserName = $"{name}@test",
|
||||
Email = $"{name}@test",
|
||||
EmailConfirmed = true,
|
||||
FirstName = $"FirstName of {name}",
|
||||
LastName = $"LastName of {name}"
|
||||
};
|
||||
|
||||
await userManager.CreateAsync(user, DefaultPassword);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private async Task<ApplicationUser> CreateCreatorAsync(ApplicationUser creator)
|
||||
{
|
||||
await userManager.CreateAsync(creator, DefaultPassword);
|
||||
await userManager.AddToRolesAsync(creator, new[] { Roles.Creator });
|
||||
return creator;
|
||||
}
|
||||
|
||||
|
||||
private readonly static ApplicationUser Hutopy = new()
|
||||
{
|
||||
UserName = "hutopy@test",
|
||||
Email = "hutopy@test",
|
||||
EmailConfirmed = true,
|
||||
CreatorAlias = "hutopy",
|
||||
About = "Page officielle",
|
||||
Description = "Site officiel pour Hutopy. Venez-nous-y retrouver avec tous vos fans!",
|
||||
ProfileColors = new ProfileColors
|
||||
{
|
||||
BannerTop = "A30E79", BannerBottom = "6B0065", Accent = "23393B", Menu = "53B93B",
|
||||
},
|
||||
SocialNetworks =
|
||||
new SocialNetworks
|
||||
{
|
||||
XUrl = "https://twitter.com/Hutopyinc",
|
||||
FacebookUrl = "https://www.facebook.com/Hutopy",
|
||||
InstagramUrl = "https://www.instagram.com/hutopy.inc/"
|
||||
},
|
||||
StoredDataUrls = new StoredDataUrls
|
||||
{
|
||||
BannerPictureUrl = "/images/usersmedia/HutopyProfile/banners/banner01.png",
|
||||
ProfilePictureUrl = "/images/usersmedia/HutopyProfile/profilepictures/profileHutopyProfile01.png"
|
||||
}
|
||||
};
|
||||
|
||||
private readonly ApplicationUser[] _creators =
|
||||
[
|
||||
Hutopy
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user