Skip to content

Commit

Permalink
Merge pull request ALCOpenSource#201 from Honeyloveet/backend/add-dum…
Browse files Browse the repository at this point in the history
…my-data

added dummy user data generator
  • Loading branch information
Honeyloveet authored Jun 21, 2023
2 parents f1bcf7d + fb53b4f commit 4fceb3d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
36 changes: 36 additions & 0 deletions backend/src/mms.Infrastructure/Seeder/DataGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Bogus;
using mms.Domain.Entities;

namespace mms.Infrastructure.Seeder
{
public static class DataGenerator
{
public static readonly List<AppUser> MentorsAndManagers = new();

public const int NumberOfMentorsAndManagers = 60;

public static void InitMentorsAndManagersData()
{
var mentorsAndManagersGenerator = GetMentorsAndManagersGenerator();
var generatedMentorsAndManagers = mentorsAndManagersGenerator.Generate(NumberOfMentorsAndManagers);
MentorsAndManagers.AddRange(generatedMentorsAndManagers);
}

private static Faker<AppUser> GetMentorsAndManagersGenerator()
{
return new Faker<AppUser>()
.RuleFor(e => e.Id, _ => Guid.NewGuid().ToString())
.RuleFor(e => e.About, f => f.Lorem.Paragraph(1))
.RuleFor(e => e.FirstName, f => f.Name.FirstName())
.RuleFor(e => e.LastName, f => f.Name.LastName())
.RuleFor(e => e.IsActive, f => true)
.RuleFor(e => e.DateCreated, f => DateTime.Now)
.RuleFor(e => e.Email, (f, e) => f.Internet.Email(e.FirstName, e.LastName))
.RuleFor(e => e.EmailConfirmed, f => true)
.RuleFor(e => e.UserName, (f, e) => e.FirstName)
.RuleFor(e => e.Country, f => f.Address.Country())
.RuleFor(e => e.City, f => f.Address.City())
.RuleFor(e => e.State, (f, e) => e.City);
}
}
}
49 changes: 34 additions & 15 deletions backend/src/mms.Infrastructure/Seeder/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using mms.Domain.Entities;
using mms.Domain.Enums;
using mms.Infrastructure.Context;
using mms.Infrastructure.Policy;

Expand Down Expand Up @@ -39,6 +38,9 @@ private static async Task SeederRun(UserManager<AppUser> userManager, Applicatio
List<UserNotification> usersNotifications = new();
List<UserPrivacy> userPrivacy = new();

DataGenerator.InitMentorsAndManagersData();

var mentorsAndManagers = DataGenerator.MentorsAndManagers;

var user = new AppUser
{
Expand Down Expand Up @@ -129,25 +131,42 @@ private static async Task SeederRun(UserManager<AppUser> userManager, Applicatio
AppUserId = user2.Id,
});

for (var index = 0; index < users.Count; index++)
foreach (AppUser appUser in users)
{
switch (index)
await userManager.CreateAsync(appUser, Password);
await userManager.AddToRoleAsync(appUser, Policies.Admin);
}

foreach (AppUser appUser in mentorsAndManagers)
{
usersNotifications.Add(new UserNotification()
{
Id = Guid.NewGuid().ToString(),
AppUserId = appUser.Id,
});

userPrivacy.Add(new UserPrivacy()
{
case 0:
await userManager.CreateAsync(users[index], Password);
await userManager.AddToRoleAsync(users[index], Policies.Admin);
break;
case 1:
await userManager.CreateAsync(users[index], Password);
await userManager.AddToRoleAsync(users[index], Policies.Mentor);
break;
default:
await userManager.CreateAsync(users[index], Password);
await userManager.AddToRoleAsync(users[index], Policies.Manager);
break;
Id = Guid.NewGuid().ToString(),
AppUserId = appUser.Id,
});
}

for (int i = 0; i < mentorsAndManagers.Count; i++)
{
if (i <= 30) {
await userManager.CreateAsync(mentorsAndManagers[i], Password);
await userManager.AddToRoleAsync(mentorsAndManagers[i], Policies.Mentor);
} else if (i > 30 && i <= 50) {
await userManager.CreateAsync(mentorsAndManagers[i], Password);
await userManager.AddToRoleAsync(mentorsAndManagers[i], Policies.Manager);
} else {
await userManager.CreateAsync(mentorsAndManagers[i], Password);
await userManager.AddToRoleAsync(mentorsAndManagers[i], Policies.Admin);
}
}


await context.UserNotifications.AddRangeAsync(usersNotifications);
await context.UserPrivacy.AddRangeAsync(userPrivacy);
await context.SaveChangesAsync();
Expand Down
1 change: 1 addition & 0 deletions backend/src/mms.Infrastructure/mms.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.5" />
Expand Down

0 comments on commit 4fceb3d

Please sign in to comment.