From c751709b1b4fe6f16fd84647e8e071455e7b78d6 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 17 Mar 2026 03:04:36 +1100 Subject: v0.1a --- HBContext.cs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 HBContext.cs (limited to 'HBContext.cs') diff --git a/HBContext.cs b/HBContext.cs new file mode 100644 index 0000000..bb0572f --- /dev/null +++ b/HBContext.cs @@ -0,0 +1,84 @@ +using Microsoft.EntityFrameworkCore; +using HyperBooru.Services; + +namespace HyperBooru; + +enum HBObjectId { + NsfwTag = -1, + IngestTag = -2, + AdminUser = -3 +} + +public class HBContext : DbContext { + public static readonly Guid NsfwTag = new("EBDAD4F8-455A-4351-8017-1D4854D6FA38"); + public static readonly Guid IngestTag = new("EA212801-5BCC-4C0E-814F-FB9D30DB58BC"); + public static readonly Guid AdminUser = new("4FA948F4-7C45-4F81-BB6B-E417491E6C96"); + + public DbSet Objects { get; set; } + public DbSet Users { get; set; } + public DbSet TagDefinitions { get; set; } + public DbSet Tags { get; set; } + public DbSet Media { get; set; } + public DbSet UploadedFiles { get; set; } + public DbSet OcrData { get; set; } + + private IConfigService config; + + public HBContext(DbContextOptions options, IConfigService config) : base(options) => + this.config = config; + + protected override void OnConfiguring(DbContextOptionsBuilder options) { + options.UseNpgsql(config.DbConnectionString); + + #if DEBUG + options.EnableSensitiveDataLogging(); + #endif + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) { + // Don't use shared tables for inherited types + modelBuilder.Entity().ToTable("Objects"); + modelBuilder.Entity().ToTable("TagDefinitions"); + modelBuilder.Entity().ToTable("Tags"); + modelBuilder.Entity().ToTable("Media"); + modelBuilder.Entity().ToTable("UploadedFiles"); + + // Seed internal tag definitions + // These should NEVER change + modelBuilder.Entity().HasData(new TagDefinition[] { + new() { + ObjectId = (int) HBObjectId.NsfwTag, + Guid = NsfwTag, + Source = TagSource.Internal, + Name = "nsfw" + }, + new() { + ObjectId = (int) HBObjectId.IngestTag, + Guid = IngestTag, + Source = TagSource.Internal, + Name = "ingest" + } + }); + + // Seed initial admin user + modelBuilder.Entity().HasData(new User[] { + new() { + ObjectId = (int) HBObjectId.AdminUser, + Guid = AdminUser, + Username = "admin", + PasswordHash = UserService.HashPassword("admin") + } + }); + + // Some complex relationships cannot be inferred and require + // additional configuration, as seen below. + modelBuilder.Entity() + .HasMany(e => e.ImplicitTags) + .WithMany(); + + modelBuilder.Entity() + .HasOne(m => m.CurrentUploadedFile) + .WithOne() + .HasForeignKey("CurrentUploadedFileId"); + } +} \ No newline at end of file -- cgit v1.3