391 lines
12 KiB
C#
391 lines
12 KiB
C#
using Hutopy.Web.Common;
|
|
using Hutopy.Web.Features.Contents.Data;
|
|
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<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, 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
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",
|
|
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 Creator 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 Creator 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 Creator 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 Creator 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 Creator 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 Creator[] _creators =
|
|
[
|
|
HutopyCreator,
|
|
ArpsCreator,
|
|
ChloeBeaugrandCreator,
|
|
GuillaumeMCreator,
|
|
LeffetCreator,
|
|
MathieuCaron
|
|
];
|
|
}
|