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

View File

@@ -0,0 +1,23 @@
using Application.Interfaces.Repositories;
using Infrastructure.Database;
using Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
return services.AddDbContextFactory<AppDbContext>(options =>
options
.UseInMemoryDatabase("Skibibase")
.UseSeeding((c, _) => DbSeeder.Seed(c))
.UseAsyncSeeding((async (context, _, token) => await DbSeeder.SeedAsync(context, token)))
.EnableSensitiveDataLogging()
)
.AddScoped<IUserRepository, UserRepository>()
.AddScoped<IKillRepository, KillRepository>();
}
}

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Bogus" Version="35.6.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,34 @@
using Application.Interfaces.Repositories;
using Domain.Entities;
using Infrastructure.Database;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Repositories;
public class KillRepository(AppDbContext context) : IKillRepository
{
public IEnumerable<Kill> GetAllKills()
{
context.Database.EnsureCreated();
return context.Kills.AsNoTracking();
}
public bool TryGetKillById(int id, out Kill? kill)
{
kill = context.Kills.FirstOrDefault(k => k.Id == id);
return kill != null;
}
public void AddKill(Kill kill)
{
context.Kills.Add(kill);
context.SaveChanges();
}
public bool RemoveKill(Kill kill)
{
context.Kills.Remove(kill);
return context.SaveChanges() > 0;
}
}

View File

@@ -0,0 +1,33 @@
using Application.Interfaces.Repositories;
using Domain.Entities;
using Infrastructure.Database;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Repositories;
public class UserRepository(AppDbContext context) : IUserRepository
{
public IEnumerable<User> GetAllUsers()
{
context.Database.EnsureCreated();
return context.Users.AsNoTracking();
}
public bool TryGetUserById(int id, out User? user)
{
user = context.Users.FirstOrDefault(u => u.Id == id);
return user != null;
}
public bool AddUser(User user)
{
context.Users.Add(user);
return context.SaveChanges() != 0;
}
public bool UpdateUser(User user)
{
context.Users.Update(user);
return context.SaveChanges() != 0;
}
}