initial commit

This commit is contained in:
Yui
2025-11-26 16:50:06 -03:00
commit 5644aa0ebf
47 changed files with 800 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
using Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Database;
public class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Kill> Kills { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options){}
}

View 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();
}
}