Remove TestDataSeeder
This commit is contained in:
@@ -118,7 +118,6 @@ await app.InitialiseIdentityDatabaseAsync();
|
||||
await app.InitialiseContentDbContextAsync();
|
||||
await app.InitialiseMessagingDbContextAsync();
|
||||
await app.InitialiseMembershipDbContextAsync();
|
||||
await app.SeedDatabaseWithTestDataOnlyIfNoDataIsPresentAsync();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (!app.Environment.IsDevelopment())
|
||||
|
||||
@@ -1,407 +0,0 @@
|
||||
using Hutopy.Web.Common;
|
||||
using Hutopy.Web.Features.Contents.Data;
|
||||
using ContentCreator = Hutopy.Web.Features.Contents.Data.Creator;
|
||||
using Hutopy.Web.Features.Memberships.Data;
|
||||
using MembershipCreator = Hutopy.Web.Features.Memberships.Data.Creator;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
using Hutopy.Web.Features.Users;
|
||||
|
||||
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<IdentityUserManager>(),
|
||||
scope.ServiceProvider.GetRequiredService<ContentDbContext>(),
|
||||
scope.ServiceProvider.GetRequiredService<MessagingDbContext>(),
|
||||
scope.ServiceProvider.GetRequiredService<MembershipDbContext>());
|
||||
|
||||
await seeder.SeedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
internal class TestDataSeeder(
|
||||
IdentityUserManager userManager,
|
||||
ContentDbContext contentContext,
|
||||
MessagingDbContext messagingContext,
|
||||
MembershipDbContext membershipContext)
|
||||
{
|
||||
private const string DefaultPassword = "Test123#";
|
||||
|
||||
public async Task SeedAsync()
|
||||
{
|
||||
if (contentContext.Contents.Any()) return;
|
||||
|
||||
_users.Add(await CreateUserAsync("admin", null, KnownRoles.Administrator));
|
||||
var userA = await CreateUserAsync("userA", null);
|
||||
_users.Add(userA);
|
||||
_users.Add(await CreateUserAsync("userB", null));
|
||||
|
||||
foreach (var creator in _creators)
|
||||
{
|
||||
var creatorUser = await CreateUserAsync(
|
||||
creator.Name,
|
||||
creator.Images.Logo,
|
||||
KnownRoles.Creator);
|
||||
|
||||
creator.Id = creatorUser.Id;
|
||||
creator.CreatedBy = creator.Id;
|
||||
|
||||
await contentContext.Creators.AddAsync(creator);
|
||||
|
||||
var contents = GenerateContent(creator, 10);
|
||||
foreach (var content in contents)
|
||||
{
|
||||
var messages = GenerateMessages(content, 10);
|
||||
|
||||
var parentMessages = messages.Where((
|
||||
_,
|
||||
index) => index % 2 == 0).ToList();
|
||||
foreach (var parentMessage in parentMessages)
|
||||
{
|
||||
_ = GenerateReplies(content, parentMessage, 10);
|
||||
}
|
||||
|
||||
await messagingContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// convert to MembershipCreator
|
||||
var membershipCreator = new MembershipCreator
|
||||
{
|
||||
Id = creator.Id,
|
||||
Name = creator.Name,
|
||||
StripeAccountId = Guid.NewGuid().ToString(),
|
||||
PortraitUrl = creator.Images.Logo,
|
||||
};
|
||||
|
||||
await membershipContext.Creators.AddAsync(membershipCreator);
|
||||
await membershipContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Content> GenerateContent(
|
||||
ContentCreator creator,
|
||||
int contentCount)
|
||||
{
|
||||
var currentDate = DateTimeOffset.UtcNow;
|
||||
|
||||
var contents = new List<Content>();
|
||||
|
||||
for (var c = contentCount; c > 0; c--)
|
||||
{
|
||||
var content = new Content
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
CreatedBy = creator.Id,
|
||||
CreatedAt = currentDate,
|
||||
Title = $"Title {creator.Name}-{c}",
|
||||
Description = $"Description {creator.Name}-{c}"
|
||||
};
|
||||
|
||||
contentContext.Contents.Add(content);
|
||||
|
||||
contents.Add(content);
|
||||
|
||||
currentDate = currentDate.AddSeconds(-Random.Shared.Next(100, 100_000));
|
||||
}
|
||||
|
||||
contentContext.SaveChanges();
|
||||
|
||||
return contents;
|
||||
}
|
||||
|
||||
private List<Message> GenerateMessages(
|
||||
Content content,
|
||||
int messageCount)
|
||||
{
|
||||
var currentDate = content.CreatedAt;
|
||||
var messages = new List<Message>();
|
||||
|
||||
for (var m = messageCount; m > 0; m--)
|
||||
{
|
||||
currentDate = currentDate.AddSeconds(-Random.Shared.Next(100, 100_000));
|
||||
var author = Random.Shared.GetItems(_users.ToArray(), 1).First();
|
||||
|
||||
var message = new Message
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
SubjectId = content.Id,
|
||||
CreatedAt = currentDate,
|
||||
CreatedBy = author.Id,
|
||||
CreatedByName = author.Alias ?? $"{author.Firstname} {author.Lastname}",
|
||||
CreatedByPortraitUrl = author.PortraitUrl,
|
||||
Value = $"Message #{m} on {content.Title}"
|
||||
};
|
||||
|
||||
messagingContext.Messages.Add(message);
|
||||
|
||||
messages.Add(message);
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
private List<Message> GenerateReplies(
|
||||
Content content,
|
||||
Message parent,
|
||||
int replyCount)
|
||||
{
|
||||
var currentDate = parent.CreatedAt;
|
||||
var replies = new List<Message>();
|
||||
|
||||
for (var r = replyCount; r > 0; r--)
|
||||
{
|
||||
currentDate = currentDate.AddSeconds(-Random.Shared.Next(100, 100_000));
|
||||
|
||||
var author = Random.Shared.GetItems(_users.ToArray(), 1).First();
|
||||
|
||||
var message = new Message
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
SubjectId = content.Id,
|
||||
ParentId = parent.Id,
|
||||
CreatedBy = author.Id,
|
||||
CreatedByName = author.Alias ?? $"{author.Firstname} {author.Lastname}",
|
||||
CreatedByPortraitUrl = author.PortraitUrl,
|
||||
CreatedAt = currentDate,
|
||||
Value = $"Reply {r} to {parent.Value} on {content.Title}"
|
||||
};
|
||||
|
||||
messagingContext.Messages.Add(message);
|
||||
|
||||
replies.Add(message);
|
||||
}
|
||||
|
||||
return replies;
|
||||
}
|
||||
|
||||
private async Task<IdentityUser> CreateUserAsync(
|
||||
string name,
|
||||
string? portraitUrl,
|
||||
params string[] roles)
|
||||
{
|
||||
var user = new IdentityUser
|
||||
{
|
||||
UserName = $"{name}@test",
|
||||
Email = $"{name}@test",
|
||||
EmailConfirmed = true,
|
||||
Alias = name,
|
||||
Firstname = $"FirstName of {name}",
|
||||
Lastname = $"LastName of {name}",
|
||||
PortraitUrl = portraitUrl
|
||||
};
|
||||
|
||||
await userManager.CreateAsync(user, DefaultPassword);
|
||||
|
||||
if (roles.Length > 0) await userManager.AddToRolesAsync(user, roles);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private readonly List<IdentityUser> _users =
|
||||
[
|
||||
];
|
||||
|
||||
private readonly static ContentCreator HutopyCreator = new()
|
||||
{
|
||||
Name = "hutopy",
|
||||
Title = "Page officielle",
|
||||
Colors = new Colors
|
||||
{
|
||||
Primary = "#A30E79",
|
||||
Secondary = "#6B0065",
|
||||
Background = "#53B93B",
|
||||
Surface = "#23393B",
|
||||
Error = "#B00020",
|
||||
OnPrimary = "#ffffff",
|
||||
OnSecondary = "#000000",
|
||||
OnBackground = "#000000",
|
||||
OnSurface = "#000000",
|
||||
OnError = "#ffffff",
|
||||
},
|
||||
Socials =
|
||||
new Socials
|
||||
{
|
||||
XUrl = "https://twitter.com/Hutopyinc",
|
||||
FacebookUrl = "https://www.facebook.com/Hutopy",
|
||||
InstagramUrl = "https://www.instagram.com/hutopy.inc/"
|
||||
},
|
||||
Images = new Images
|
||||
{
|
||||
Banner = "/images/usersmedia/HutopyProfile/banners/banner01.png",
|
||||
Logo = "/images/usersmedia/HutopyProfile/profilepictures/profileHutopyProfile01.png"
|
||||
}
|
||||
};
|
||||
|
||||
private readonly static ContentCreator ArpsCreator = new()
|
||||
{
|
||||
Name = "arps",
|
||||
Title = "Créateur de contenu",
|
||||
Colors = new Colors
|
||||
{
|
||||
Primary = "#A30E79",
|
||||
Secondary = "#6B0065",
|
||||
Background = "#53B93B",
|
||||
Surface = "#23393B",
|
||||
Error = "#B00020",
|
||||
OnPrimary = "#ffffff",
|
||||
OnSecondary = "#000000",
|
||||
OnBackground = "#000000",
|
||||
OnSurface = "#000000",
|
||||
OnError = "#ffffff",
|
||||
},
|
||||
Socials = new Socials
|
||||
{
|
||||
FacebookUrl = "https://www.facebook.com/arps.company",
|
||||
InstagramUrl = "https://www.instagram.com/arps.co/",
|
||||
YoutubeUrl = "https://www.youtube.com/channel/UCgnT_psydUXohYm5Yz_wFUg",
|
||||
TikTokUrl = "https://www.tiktok.com/@arps.co",
|
||||
LinkedInUrl = "https://www.linkedin.com/in/mickael-simard-96079a90/",
|
||||
WebsiteUrl = "https://www.arps.ca/"
|
||||
},
|
||||
Images = new Images
|
||||
{
|
||||
Banner = "/images/usersmedia/ARPS/banners/bannerARPS01.png",
|
||||
Logo = "/images/usersmedia/ARPS/profilepictures/profileARPS.png"
|
||||
}
|
||||
};
|
||||
|
||||
private readonly static ContentCreator ChloeBeaugrandCreator = new()
|
||||
{
|
||||
Name = "chloebeaugrand",
|
||||
Title = "Page officielle",
|
||||
Colors = new Colors
|
||||
{
|
||||
Primary = "#A30E79",
|
||||
Secondary = "#6B0065",
|
||||
Background = "#53B93B",
|
||||
Surface = "#23393B",
|
||||
Error = "#B00020",
|
||||
OnPrimary = "#ffffff",
|
||||
OnSecondary = "#000000",
|
||||
OnBackground = "#000000",
|
||||
OnSurface = "#000000",
|
||||
OnError = "#ffffff",
|
||||
},
|
||||
Socials =
|
||||
new Socials
|
||||
{
|
||||
FacebookUrl = "https://www.facebook.com/chloegestionmedias",
|
||||
InstagramUrl = "https://www.instagram.com/chloe.photo_gms",
|
||||
},
|
||||
Images = new Images
|
||||
{
|
||||
Banner = "/images/usersmedia/chloebeaugrand/banners/bannerChloeBeaugrand01.png",
|
||||
Logo = "/images/usersmedia/chloebeaugrand/profilepictures/profileChloeBeaugrand01.png"
|
||||
}
|
||||
};
|
||||
|
||||
private readonly static ContentCreator GuillaumeMCreator = new()
|
||||
{
|
||||
Name = "guillaumem",
|
||||
Title = "Page officielle",
|
||||
Colors = new Colors
|
||||
{
|
||||
Primary = "#A30E79",
|
||||
Secondary = "#6B0065",
|
||||
Background = "#53B93B",
|
||||
Surface = "#23393B",
|
||||
Error = "#B00020",
|
||||
OnPrimary = "#ffffff",
|
||||
OnSecondary = "#000000",
|
||||
OnBackground = "#000000",
|
||||
OnSurface = "#000000",
|
||||
OnError = "#ffffff",
|
||||
},
|
||||
Socials =
|
||||
new Socials
|
||||
{
|
||||
FacebookUrl = "https://www.facebook.com/GuillaumeMousseau222",
|
||||
InstagramUrl = "https://www.instagram.com/guillaumeaime/",
|
||||
TikTokUrl = "https://www.tiktok.com/@guillaumeaime"
|
||||
},
|
||||
Images = new Images
|
||||
{
|
||||
Banner = "/images/usersmedia/guillaumeMousseau/banners/bannerGuillaumeMousseau01.png",
|
||||
Logo =
|
||||
"/images/usersmedia/guillaumeMousseau/profilepictures/profileGuillaumeMousseau01.png"
|
||||
}
|
||||
};
|
||||
|
||||
private readonly static ContentCreator LeffetCreator = new()
|
||||
{
|
||||
Name = "leffet",
|
||||
Title = "Page officielle",
|
||||
Colors = new Colors
|
||||
{
|
||||
Primary = "#A30E79",
|
||||
Secondary = "#6B0065",
|
||||
Background = "#53B93B",
|
||||
Surface = "#23393B",
|
||||
Error = "#B00020",
|
||||
OnPrimary = "#ffffff",
|
||||
OnSecondary = "#000000",
|
||||
OnBackground = "#000000",
|
||||
OnSurface = "#000000",
|
||||
OnError = "#ffffff",
|
||||
},
|
||||
Socials =
|
||||
new Socials
|
||||
{
|
||||
FacebookUrl = "https://www.facebook.com/Hutopy",
|
||||
InstagramUrl = "https://www.instagram.com/guillaumeaime/",
|
||||
WebsiteUrl = "https://fondationleffet.ca/"
|
||||
},
|
||||
Images = new Images
|
||||
{
|
||||
Banner = "/images/usersmedia/leffet/banners/banner02.png",
|
||||
Logo = "/images/usersmedia/leffet/profilepictures/leffetProfile01.png"
|
||||
}
|
||||
};
|
||||
|
||||
private readonly static ContentCreator MathieuCaron = new()
|
||||
{
|
||||
Name = "mathieucaron",
|
||||
Title = "Page officielle",
|
||||
Colors = new Colors
|
||||
{
|
||||
Primary = "#A30E79",
|
||||
Secondary = "#6B0065",
|
||||
Background = "#53B93B",
|
||||
Surface = "#23393B",
|
||||
Error = "#B00020",
|
||||
OnPrimary = "#ffffff",
|
||||
OnSecondary = "#000000",
|
||||
OnBackground = "#000000",
|
||||
OnSurface = "#000000",
|
||||
OnError = "#ffffff",
|
||||
},
|
||||
Socials =
|
||||
new Socials
|
||||
{
|
||||
FacebookUrl = "https://www.facebook.com/MathieuCaronPro/",
|
||||
YoutubeUrl = "https://www.youtube.com/@lesinterviewsatypiquesdema4692",
|
||||
},
|
||||
Images = new Images
|
||||
{
|
||||
Banner = "/images/usersmedia/mathieuCaron/banners/bannerMathieuCaron01.png",
|
||||
Logo = "/images/usersmedia/mathieuCaron/profilepictures/profileMathieuCaron01.png"
|
||||
}
|
||||
};
|
||||
|
||||
private readonly ContentCreator[] _creators =
|
||||
[
|
||||
HutopyCreator,
|
||||
ArpsCreator,
|
||||
ChloeBeaugrandCreator,
|
||||
GuillaumeMCreator,
|
||||
LeffetCreator,
|
||||
MathieuCaron
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user