summaryrefslogtreecommitdiff
path: root/Server/HBContext.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2026-05-22 00:52:16 +1000
committerJake Mannens <jake@asger.xyz>2026-05-23 22:22:55 +1000
commit12eaa5814ef20b0910e8d64a753378b6f6797989 (patch)
tree062cf477c29054e0f089cb80f0cd79a9f3b7ccd9 /Server/HBContext.cs
parent6de5d7f5364fe1d54703da6d6b7cb08ea26e939f (diff)
Initial commitwasm-initial
Diffstat (limited to 'Server/HBContext.cs')
-rw-r--r--Server/HBContext.cs84
1 files changed, 84 insertions, 0 deletions
diff --git a/Server/HBContext.cs b/Server/HBContext.cs
new file mode 100644
index 0000000..b684a51
--- /dev/null
+++ b/Server/HBContext.cs
@@ -0,0 +1,84 @@
+using HyperBooru.Services;
+using Microsoft.EntityFrameworkCore;
+
+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<HBObject> Objects { get; set; }
+ public DbSet<User> Users { get; set; }
+ public DbSet<TagDefinition> TagDefinitions { get; set; }
+ public DbSet<Tag> Tags { get; set; }
+ public DbSet<Media> Media { get; set; }
+ public DbSet<UploadedFile> UploadedFiles { get; set; }
+ public DbSet<OcrData> OcrData { get; set; }
+
+ private IConfigService config;
+
+ public HBContext(DbContextOptions<HBContext> 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<HBObject>().ToTable("Objects");
+ modelBuilder.Entity<TagDefinition>().ToTable("TagDefinitions");
+ modelBuilder.Entity<Tag>().ToTable("Tags");
+ modelBuilder.Entity<Media>().ToTable("Media");
+ modelBuilder.Entity<UploadedFile>().ToTable("UploadedFiles");
+
+ // Seed internal tag definitions
+ // These should NEVER change
+ modelBuilder.Entity<TagDefinition>().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<User>().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<TagDefinition>()
+ .HasMany(e => e.ImplicitTags)
+ .WithMany();
+
+ modelBuilder.Entity<Media>()
+ .HasOne(m => m.CurrentUploadedFile)
+ .WithOne()
+ .HasForeignKey<Media>("CurrentUploadedFileId");
+ }
+} \ No newline at end of file