continuity/Server/DB/WonderkingContext.cs
Timothy Schenk b584ca55bf
All checks were successful
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 24s
Build, Package and Push Images / sbom-scan (push) Successful in 36s
Build, Package and Push Images / sonarqube (push) Successful in 1m31s
Build, Package and Push Images / container-build (push) Successful in 1m37s
Build, Package and Push Images / container-sbom-scan (push) Successful in 2m16s
chore: deps and indices
2023-11-14 21:06:02 +01:00

53 lines
2.3 KiB
C#

namespace Server.DB;
using Documents;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
public class WonderkingContext : DbContext
{
private readonly IConfiguration _configuration;
private readonly ILoggerFactory _loggerFactory;
public WonderkingContext(ILoggerFactory loggerFactory, IConfiguration configuration)
{
this._loggerFactory = loggerFactory;
this._configuration = configuration;
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Character> Characters { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder
.UseNpgsql(
$"Host={this._configuration["DB:Host"]};Username={this._configuration["DB:Username"]};Password={this._configuration["DB:Password"]};Database={this._configuration["DB:Database"]};Port={this._configuration["DB:Port"]}")
.EnableSensitiveDataLogging().UseLoggerFactory(this._loggerFactory);
protected override void OnModelCreating(ModelBuilder modelBuilder) =>
modelBuilder.Entity<Account>(builder =>
{
builder.Property(b => b.Username).HasColumnType("varchar(20)");
builder.HasIndex(b => b.Username).IsUnique();
builder.Property(b => b.Password).HasColumnType("bytea");
builder.Property(b => b.Salt).HasColumnType("bytea");
builder.HasKey(b => b.Id);
builder.HasMany(e => e.Characters).WithOne(e => e.Account).HasForeignKey(e => e.AccountId)
.IsRequired();
}).Entity<Character>(builder =>
{
builder.HasKey(c => c.Id);
builder.Property(c => c.Name).HasColumnType("varchar(20)");
builder.HasIndex(c => c.Name).IsUnique();
builder.HasMany(e => e.InventoryItems).WithOne(e => e.Character)
.HasForeignKey(e => e.CharacterId).IsRequired();
builder.OwnsOne(p => p.BaseStats);
builder.OwnsOne(p => p.JobData);
}).Entity<InventoryItem>(builder => { builder.HasKey(i => i.Id); }).Entity<Guild>(builder =>
{
builder.HasKey(g => g.Id);
builder.HasMany(g => g.GuildMembers).WithOne(g => g.Guild).HasForeignKey(g => g.GuildId)
.IsRequired();
});
}