2023-10-12 07:15:34 +00:00
|
|
|
namespace Server.DB;
|
2023-08-10 08:47:35 +00:00
|
|
|
|
2023-08-12 21:02:59 +00:00
|
|
|
using Documents;
|
2023-08-14 11:49:27 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-08-10 08:47:35 +00:00
|
|
|
|
2023-08-14 11:49:27 +00:00
|
|
|
public class WonderkingContext : DbContext
|
2023-08-10 08:47:35 +00:00
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
private readonly ILoggerFactory _loggerFactory;
|
2023-08-14 11:49:27 +00:00
|
|
|
|
|
|
|
public WonderkingContext(ILoggerFactory loggerFactory, IConfiguration configuration)
|
2023-08-10 08:47:35 +00:00
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
this._loggerFactory = loggerFactory;
|
|
|
|
this._configuration = configuration;
|
2023-08-10 08:47:35 +00:00
|
|
|
}
|
2023-08-12 21:02:59 +00:00
|
|
|
|
2023-08-14 19:30:32 +00:00
|
|
|
public DbSet<Account> Accounts { get; set; }
|
2023-08-14 20:30:35 +00:00
|
|
|
public DbSet<Character> Characters { get; set; }
|
2023-08-14 19:30:32 +00:00
|
|
|
|
2023-08-14 11:49:27 +00:00
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
|
|
|
|
optionsBuilder
|
|
|
|
.UseNpgsql(
|
2023-10-27 17:47:17 +00:00
|
|
|
$"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);
|
2023-08-14 11:49:27 +00:00
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder) =>
|
|
|
|
modelBuilder.Entity<Account>(builder =>
|
|
|
|
{
|
|
|
|
builder.Property(b => b.Username).HasColumnType("varchar(20)");
|
2023-11-14 20:06:02 +00:00
|
|
|
builder.HasIndex(b => b.Username).IsUnique();
|
2023-08-14 11:49:27 +00:00
|
|
|
builder.Property(b => b.Password).HasColumnType("bytea");
|
|
|
|
builder.Property(b => b.Salt).HasColumnType("bytea");
|
|
|
|
builder.HasKey(b => b.Id);
|
2023-11-08 14:33:04 +00:00
|
|
|
builder.HasMany(e => e.Characters).WithOne(e => e.Account).HasForeignKey(e => e.AccountId)
|
|
|
|
.IsRequired();
|
2023-08-14 20:30:35 +00:00
|
|
|
}).Entity<Character>(builder =>
|
|
|
|
{
|
|
|
|
builder.HasKey(c => c.Id);
|
|
|
|
builder.Property(c => c.Name).HasColumnType("varchar(20)");
|
2023-11-14 20:06:02 +00:00
|
|
|
builder.HasIndex(c => c.Name).IsUnique();
|
2023-11-08 14:33:04 +00:00
|
|
|
builder.HasMany(e => e.InventoryItems).WithOne(e => e.Character)
|
|
|
|
.HasForeignKey(e => e.CharacterId).IsRequired();
|
2023-11-13 20:12:12 +00:00
|
|
|
builder.OwnsOne(p => p.BaseStats);
|
|
|
|
builder.OwnsOne(p => p.JobData);
|
2023-11-14 19:07:45 +00:00
|
|
|
}).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();
|
|
|
|
});
|
2023-08-11 09:31:30 +00:00
|
|
|
}
|