Files
social-media/src/Web/TestDataSeeder.cs
2024-08-04 04:45:53 -04:00

267 lines
8.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Hutopy.Domain.Constants;
using Hutopy.Infrastructure.Identity;
using Hutopy.Web.Common;
using Hutopy.Web.Features.Contents.Data;
using Hutopy.Web.Features.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;
_users.Add(await CreateUserAsync("admin", null, Roles.Administrator));
_users.Add(await CreateUserAsync("userA", null));
_users.Add(await CreateUserAsync("userB", null));
foreach (var creator in _creators)
{
var creatorUser = await CreateUserAsync(
creator.Name,
creator.StoredDataUrls.ProfilePictureUrl,
Roles.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();
}
}
}
private List<Content> GenerateContent(Creator 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<ApplicationUser> CreateUserAsync(string name, string? portraitUrl, params string[] roles)
{
var user = new ApplicationUser
{
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<ApplicationUser> _users =
[
];
private readonly static Creator HutopyCreator = new()
{
Name = "hutopy",
About = new()
{
Title = "Page officielle",
Description = "Site officiel pour Hutopy. Venez-nous-y retrouver avec tous vos fans!",
},
ProfileColors = new()
{
BannerTop = "#A30E79", BannerBottom = "#6B0065", Accent = "#23393B", Menu = "#53B93B",
},
SocialNetworks =
new()
{
XUrl = "https://twitter.com/Hutopyinc",
FacebookUrl = "https://www.facebook.com/Hutopy",
InstagramUrl = "https://www.instagram.com/hutopy.inc/"
},
StoredDataUrls = new()
{
BannerPictureUrl = "/images/usersmedia/HutopyProfile/banners/banner01.png",
ProfilePictureUrl = "/images/usersmedia/HutopyProfile/profilepictures/profileHutopyProfile01.png"
}
};
private readonly static Creator ArpsCreator = new()
{
Name = "arps",
About =
new()
{
Title = "Page officielle",
Description = "Site officiel pour Arps. Venez-nous-y retrouver avec tous vos fans!",
},
ProfileColors =
new() { BannerTop = "#231F20", BannerBottom = "#231F20", Accent = "#272526", Menu = "#FFFFFF" },
SocialNetworks = new()
{
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/"
},
StoredDataUrls = new()
{
BannerPictureUrl = "/images/usersmedia/ARPS/banners/bannerARPS01.png",
ProfilePictureUrl = "/images/usersmedia/ARPS/profilepictures/profileARPS.png"
}
};
private readonly static Creator ChloeCreator = new()
{
Name = "chloe",
About = new()
{
Title = "Page officielle",
Description = "𝐿𝑎 𝑐𝑟𝑒́𝑎𝑡𝑖𝑣𝑖𝑡𝑒́ 𝑐𝑒𝑠𝑡 𝑙𝑖𝑛𝑡𝑒𝑙𝑙𝑖𝑔𝑒𝑛𝑐𝑒 𝑞𝑢𝑖 𝑠’𝑎𝑚𝑢𝑠𝑒!",
},
ProfileColors = new()
{
BannerTop = "#231F20", BannerBottom = "#272526", Accent = "#231F20", Menu = "#231F20",
},
SocialNetworks =
new()
{
FacebookUrl = "https://www.facebook.com/chloegestionmedias",
InstagramUrl = "https://www.instagram.com/chloe.photo_gms",
},
StoredDataUrls = new()
{
BannerPictureUrl = "/images/usersmedia/chloebeaugrand/banners/bannerChloeBeaugrand01.png",
ProfilePictureUrl = "/images/usersmedia/chloebeaugrand/profilepictures/profileChloeBeaugrand01.png"
}
};
private readonly Creator[] _creators =
[
HutopyCreator,
ArpsCreator,
ChloeCreator
];
}