initial commit
This commit is contained in:
42
Infrastructure/Database/DbSeeder.cs
Normal file
42
Infrastructure/Database/DbSeeder.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Bogus;
|
||||
using Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Database;
|
||||
|
||||
public class DbSeeder
|
||||
{
|
||||
public static async Task SeedAsync(DbContext dbContext, CancellationToken cancellationToken)
|
||||
{
|
||||
var exampleUser = await dbContext.Set<User>().FirstOrDefaultAsync(u => u.Email.EndsWith("@example.com"), cancellationToken);
|
||||
if (exampleUser == null)
|
||||
{
|
||||
dbContext.Set<User>().AddRange([
|
||||
new User(){Id = 1, Email = "sunpy@example.com", Password = "iAmJaneEldenRing21", Username = "sunpy"},
|
||||
new User(){Id = 2, Email = "yui@example.com", Password = "ILoveHalfLife98", Username = "yui"},
|
||||
new User(){Id = 3, Email = "johnny@example.com", Password = "iAmCyberPunk77", Username = "johnny"},
|
||||
new User(){Id = 4, Email = "skibidi@example.com", Password = "iHateToilets67", Username = "skibidi_hater"},
|
||||
]);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
var exampleKill = dbContext.Set<Kill>().FirstOrDefault(k => k.UserId == 1);
|
||||
if (exampleKill == null)
|
||||
{
|
||||
var killSet = dbContext.Set<Kill>();
|
||||
var fake = new Faker<Kill>()
|
||||
.RuleFor(k => k.Id, f => ++f.IndexVariable)
|
||||
.RuleFor(k => k.User, f => f.PickRandom(dbContext.Set<User>().ToList()))
|
||||
.RuleFor(k => k.UserId, (_, k) => k.User.Id)
|
||||
.RuleFor(k => k.Damage, f => f.Random.Double(1, 100))
|
||||
.RuleFor(k => k.Victim, f => f.Name.FirstName());
|
||||
var fakeKills = fake.Generate(500).ToList();
|
||||
await killSet.AddRangeAsync(fakeKills, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Seed(DbContext dbContext)
|
||||
{
|
||||
Task.Run(() => SeedAsync(dbContext, CancellationToken.None)).Wait();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user