summaryrefslogtreecommitdiff
path: root/HBContext.cs
blob: 162a2c7ab3b5d7c718682060d8b938bf17c55deb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using Microsoft.EntityFrameworkCore;
using HyperBooru.Services;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;

namespace HyperBooru;

public class HBContext : DbContext {
    public DbSet<HBObject>      Objects        { 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; }

    private IConfigService config;

    public HBContext(DbContextOptions<HBContext> options, IConfigService config) : base(options) =>
        this.config = config;

    protected override void OnConfiguring(DbContextOptionsBuilder options) {
        options.UseLazyLoadingProxies();

        var path = Path.Join(config.DataPath, "HyperBooru.db");
        options.UseSqlite($"Data Source = {config.DbPath}");

        #if DEBUG
        options.EnableSensitiveDataLogging();
        #endif
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        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");

        modelBuilder.Entity<TagDefinition>().HasData(new TagDefinition[] {
            new() { ObjectId = -1, Source = TagSource.Internal, Name = "nsfw"   },
            new() { ObjectId = -2, Source = TagSource.Internal, Name = "ingest" }
        });

        modelBuilder.Entity<TagDefinition>()
            .HasMany(e => e.ImplicitTags)
            .WithMany();
    }
}