95 lines
3.1 KiB
C#
95 lines
3.1 KiB
C#
using DysonNetwork.Shared.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Design;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NodaTime;
|
|
|
|
namespace DysonNetwork.Pass;
|
|
|
|
public abstract class ModelBase
|
|
{
|
|
public Instant CreatedAt { get; set; }
|
|
public Instant UpdatedAt { get; set; }
|
|
public Instant? DeletedAt { get; set; }
|
|
}
|
|
|
|
public class AppDatabase(
|
|
DbContextOptions<AppDatabase> options,
|
|
IConfiguration configuration
|
|
) : DbContext(options)
|
|
{
|
|
public DbSet<PermissionNode> PermissionNodes { get; set; }
|
|
public DbSet<PermissionGroup> PermissionGroups { get; set; }
|
|
public DbSet<PermissionGroupMember> PermissionGroupMembers { get; set; }
|
|
|
|
public DbSet<Account> Accounts { get; set; }
|
|
public DbSet<AccountConnection> AccountConnections { get; set; }
|
|
public DbSet<Profile> AccountProfiles { get; set; }
|
|
public DbSet<AccountContact> AccountContacts { get; set; }
|
|
public DbSet<AccountAuthFactor> AccountAuthFactors { get; set; }
|
|
public DbSet<Relationship> AccountRelationships { get; set; }
|
|
|
|
public DbSet<Session> AuthSessions { get; set; }
|
|
public DbSet<Challenge> AuthChallenges { get; set; }
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
optionsBuilder.UseNpgsql(
|
|
configuration.GetConnectionString("App"),
|
|
opt => opt
|
|
.UseNodaTime()
|
|
).UseSnakeCaseNamingConvention();
|
|
|
|
base.OnConfiguring(optionsBuilder);
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.Entity<PermissionGroupMember>()
|
|
.HasKey(pg => new { pg.GroupId, pg.Actor });
|
|
|
|
modelBuilder.Entity<Relationship>()
|
|
.HasKey(r => new { FromAccountId = r.AccountId, ToAccountId = r.RelatedId });
|
|
}
|
|
|
|
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var now = SystemClock.Instance.GetCurrentInstant();
|
|
|
|
foreach (var entry in ChangeTracker.Entries<ModelBase>())
|
|
{
|
|
if (entry.State == EntityState.Added)
|
|
{
|
|
entry.Entity.CreatedAt = now;
|
|
entry.Entity.UpdatedAt = now;
|
|
}
|
|
else if (entry.State == EntityState.Modified)
|
|
{
|
|
entry.Entity.UpdatedAt = now;
|
|
}
|
|
else if (entry.State == EntityState.Deleted)
|
|
{
|
|
entry.State = EntityState.Modified;
|
|
entry.Entity.DeletedAt = now;
|
|
}
|
|
}
|
|
|
|
return await base.SaveChangesAsync(cancellationToken);
|
|
}
|
|
}
|
|
|
|
public class AppDatabaseFactory : IDesignTimeDbContextFactory<AppDatabase>
|
|
{
|
|
public AppDatabase CreateDbContext(string[] args)
|
|
{
|
|
var configuration = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json")
|
|
.Build();
|
|
|
|
var optionsBuilder = new DbContextOptionsBuilder<AppDatabase>();
|
|
return new AppDatabase(optionsBuilder.Options, configuration);
|
|
}
|
|
} |