feat: add content navigation and tenant configuration

This commit is contained in:
xiong
2026-07-26 05:05:24 +08:00
parent 5dfb68d8cc
commit e41b38e3a1
13 changed files with 6197 additions and 1 deletions

View File

@@ -0,0 +1,188 @@
using System.Text.Json;
using Tiku.Domain.Common;
namespace Tiku.Domain.Content;
public sealed class ContentEntry : AuditableTenantEntity
{
public Guid? RegionId { get; set; }
public string? LegacyId { get; set; }
public string EntryKey { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public ContentEntryType EntryType { get; set; } = ContentEntryType.QuestionPractice;
public string? Icon { get; set; }
public string? Route { get; set; }
public string? Description { get; set; }
public ContentVisibility Visibility { get; set; } = ContentVisibility.Public;
public JsonElement AccessRules { get; set; } = JsonDefaults.Object();
public JsonElement LayoutConfig { get; set; } = JsonDefaults.Object();
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
public Guid? CreatedBy { get; set; }
public Guid? UpdatedBy { get; set; }
}
public enum ContentEntryType
{
QuestionPractice,
Vocabulary,
Handbook,
Scoreline,
Resource,
AiReport,
Custom
}
public enum ContentVisibility
{
Public,
Members,
Svip,
Hidden
}
public sealed class ContentNode : AuditableTenantEntity
{
public Guid EntryId { get; set; }
public Guid? RegionId { get; set; }
public Guid? ParentId { get; set; }
public string? LegacyId { get; set; }
public string? NodeKey { get; set; }
public string Name { get; set; } = string.Empty;
public ContentNodeType NodeType { get; set; } = ContentNodeType.Category;
public ContentMarkerType? MarkerType { get; set; }
public JsonElement MarkerConfig { get; set; } = JsonDefaults.Object();
public string? Path { get; set; }
public int Depth { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
public bool IsSelectable { get; set; } = true;
public bool IsLeaf { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
public Guid? CreatedBy { get; set; }
public Guid? UpdatedBy { get; set; }
}
public enum ContentNodeType
{
Category,
Subject,
Chapter,
Paper,
School,
Major,
ExamTarget,
ResourceGroup,
Custom
}
public enum ContentMarkerType
{
School,
Major,
Subject,
ExamTrack,
CoursePackage,
SalesIntent,
Custom
}
public sealed class QuestionCollection : AuditableTenantEntity
{
public Guid? RegionId { get; set; }
public Guid? EntryId { get; set; }
public Guid? NodeId { get; set; }
public Guid? SubjectId { get; set; }
public Guid? CategoryId { get; set; }
public Guid? QuestionBankId { get; set; }
public string? LegacyId { get; set; }
public string Name { get; set; } = string.Empty;
public QuestionCollectionType CollectionType { get; set; } = QuestionCollectionType.Dynamic;
public QuestionCollectionSourceType SourceType { get; set; } = QuestionCollectionSourceType.Filters;
public JsonElement Filters { get; set; } = JsonDefaults.Object();
public int QuestionCount { get; set; }
public decimal? TotalScore { get; set; }
public int? DurationMinutes { get; set; }
public ContentStatus Status { get; set; } = ContentStatus.Active;
public int SortOrder { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
public Guid? CreatedBy { get; set; }
public Guid? UpdatedBy { get; set; }
}
public enum QuestionCollectionType
{
Dynamic,
Manual,
Paper,
Chapter,
MockExam
}
public enum QuestionCollectionSourceType
{
Filters,
ManualQuestions,
NodeDescendants,
Category,
Subject,
QuestionBank
}
public enum ContentStatus
{
Draft,
Active,
Archived
}
public sealed class QuestionCollectionItem : AuditableTenantEntity
{
public Guid CollectionId { get; set; }
public Guid QuestionId { get; set; }
public string? SectionKey { get; set; }
public int SortOrder { get; set; }
public decimal? Score { get; set; }
public bool Required { get; set; } = true;
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}
public sealed class PracticeBlueprint : AuditableTenantEntity
{
public Guid? RegionId { get; set; }
public Guid? EntryId { get; set; }
public Guid? NodeId { get; set; }
public Guid? CollectionId { get; set; }
public string? LegacyId { get; set; }
public string Name { get; set; } = string.Empty;
public PracticeMode Mode { get; set; } = PracticeMode.Sequential;
public PracticeAssemblyType AssemblyType { get; set; } = PracticeAssemblyType.Collection;
public int? QuestionLimit { get; set; }
public int? DurationMinutes { get; set; }
public decimal? TotalScore { get; set; }
public decimal? PassScore { get; set; }
public JsonElement Sections { get; set; } = JsonDefaults.Array();
public JsonElement Rules { get; set; } = JsonDefaults.Object();
public ContentStatus Status { get; set; } = ContentStatus.Active;
public int SortOrder { get; set; }
public Guid? CreatedBy { get; set; }
public Guid? UpdatedBy { get; set; }
}
public enum PracticeMode
{
Sequential,
Random,
MockExam,
Paper,
WrongReview,
FavoriteReview
}
public enum PracticeAssemblyType
{
Collection,
NodeDescendants,
Manual,
Filters
}

View File

@@ -6,11 +6,20 @@ namespace Tiku.Domain.Learning;
public sealed class PracticeSession : TenantEntity
{
public Guid UserId { get; set; }
public Guid? BlueprintId { get; set; }
public Guid? CollectionId { get; set; }
public Guid? EntryId { get; set; }
public Guid? ContentNodeId { get; set; }
public string Mode { get; set; } = "chapter";
public string? TargetType { get; set; }
public Guid? TargetId { get; set; }
public DateTimeOffset StartedAt { get; set; } = DateTimeOffset.UtcNow;
public DateTimeOffset? FinishedAt { get; set; }
public JsonElement QuestionIds { get; set; } = JsonDefaults.Array();
public int QuestionCount { get; set; }
public int? DurationMinutes { get; set; }
public decimal? TotalScore { get; set; }
public DateTimeOffset? ExpiresAt { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}

View File

@@ -30,6 +30,9 @@ public sealed class Question : AuditableTenantEntity
public Guid? SubjectId { get; set; }
public Guid? CategoryId { get; set; }
public Guid? NodeId { get; set; }
public Guid? EntryId { get; set; }
public Guid? ContentNodeId { get; set; }
public Guid? PrimaryCollectionId { get; set; }
public string? LegacyId { get; set; }
public string? LegacySubjectId { get; set; }
public string? LegacyCategoryId { get; set; }
@@ -38,6 +41,7 @@ public sealed class Question : AuditableTenantEntity
public string? TypeLabel { get; set; }
public int? Difficulty { get; set; }
public JsonElement Tags { get; set; } = JsonDefaults.Array();
public JsonElement ExamMarkers { get; set; } = JsonDefaults.Object();
public string? MediaUrl { get; set; }
public bool HasVideoExplanation { get; set; }
public QuestionStatus Status { get; set; } = QuestionStatus.Published;

View File

@@ -0,0 +1,56 @@
using System.Text.Json;
using Tiku.Domain.Common;
namespace Tiku.Domain.Tenancy;
public sealed class TenantDomain : AuditableTenantEntity
{
public string Host { get; set; } = string.Empty;
public TenantDomainType DomainType { get; set; } = TenantDomainType.Custom;
public TenantDomainStatus Status { get; set; } = TenantDomainStatus.Pending;
public bool IsPrimary { get; set; }
public string? VerificationToken { get; set; }
public DateTimeOffset? VerifiedAt { get; set; }
}
public enum TenantDomainType
{
System,
Custom,
Miniapp
}
public enum TenantDomainStatus
{
Pending,
Active,
Failed,
Disabled
}
public sealed class TenantBranding : IHasTimestamps
{
public Guid TenantId { get; set; }
public string BrandName { get; set; } = string.Empty;
public string? ShortName { get; set; }
public string? Slogan { get; set; }
public string? OrganizationName { get; set; }
public string? LogoUrl { get; set; }
public string? FaviconUrl { get; set; }
public string? ServiceWechat { get; set; }
public string? ServiceAccountName { get; set; }
public JsonElement Theme { get; set; } = JsonDefaults.Object();
public JsonElement PublicAssets { get; set; } = JsonDefaults.Object();
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
}
public sealed class TenantSettings : IHasTimestamps
{
public Guid TenantId { get; set; }
public JsonElement FeatureFlags { get; set; } = JsonDefaults.Object();
public JsonElement AdminFeatureFlags { get; set; } = JsonDefaults.Object();
public JsonElement PublicConfig { get; set; } = JsonDefaults.Object();
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
}

View File

@@ -0,0 +1,260 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Tiku.Domain.Catalog;
using Tiku.Domain.Content;
using Tiku.Domain.Identity;
using Tiku.Domain.QuestionBanks;
namespace Tiku.Infrastructure.Persistence.Configurations;
internal sealed class ContentEntryConfiguration : IEntityTypeConfiguration<ContentEntry>
{
public void Configure(EntityTypeBuilder<ContentEntry> builder)
{
builder.ConfigureTenantEntity("content_entries");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.EntryKey).HasMaxLength(100);
builder.Property(entity => entity.Name).HasMaxLength(300);
builder.Property(entity => entity.EntryType).HasSnakeCaseEnum();
builder.Property(entity => entity.Icon).HasMaxLength(100);
builder.Property(entity => entity.Route).HasMaxLength(500);
builder.Property(entity => entity.Description).HasMaxLength(2000);
builder.Property(entity => entity.Visibility).HasSnakeCaseEnum();
builder.Property(entity => entity.AccessRules).IsJson("{}");
builder.Property(entity => entity.LayoutConfig).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.EntryKey }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.RegionId,
entity.EntryType,
entity.IsActive,
entity.SortOrder
});
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.CreatedBy)
.OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.UpdatedBy)
.OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class ContentNodeConfiguration : IEntityTypeConfiguration<ContentNode>
{
public void Configure(EntityTypeBuilder<ContentNode> builder)
{
builder.ConfigureTenantEntity("content_nodes");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.NodeKey).HasMaxLength(100);
builder.Property(entity => entity.Name).HasMaxLength(300);
builder.Property(entity => entity.NodeType).HasSnakeCaseEnum();
builder.Property(entity => entity.MarkerType).HasNullableSnakeCaseEnum();
builder.Property(entity => entity.MarkerConfig).IsJson("{}");
builder.Property(entity => entity.Path).HasColumnType("ltree");
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.EntryId, entity.NodeKey }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.EntryId,
entity.ParentId,
entity.SortOrder
});
builder.HasIndex(entity => entity.Path).HasMethod("gist");
builder.HasIndex(entity => new { entity.TenantId, entity.MarkerType })
.HasFilter("marker_type is not null");
builder.ToTable(table =>
{
table.HasCheckConstraint("ck_content_nodes_depth", "depth >= 0");
});
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ParentId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.CreatedBy)
.OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.UpdatedBy)
.OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class QuestionCollectionConfiguration : IEntityTypeConfiguration<QuestionCollection>
{
public void Configure(EntityTypeBuilder<QuestionCollection> builder)
{
builder.ConfigureTenantEntity("question_collections");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Name).HasMaxLength(300);
builder.Property(entity => entity.CollectionType).HasSnakeCaseEnum();
builder.Property(entity => entity.SourceType).HasSnakeCaseEnum();
builder.Property(entity => entity.Filters).IsJson("{}");
builder.Property(entity => entity.TotalScore).HasPrecision(8, 2);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.NodeId,
entity.Status,
entity.SortOrder
});
builder.ToTable(table =>
{
table.HasCheckConstraint(
"ck_question_collections_question_count",
"question_count >= 0");
table.HasCheckConstraint(
"ck_question_collections_duration",
"duration_minutes is null or duration_minutes > 0");
});
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.NodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Subject>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.SubjectId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Category>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.CategoryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<QuestionBank>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.QuestionBankId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.CreatedBy)
.OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.UpdatedBy)
.OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class QuestionCollectionItemConfiguration :
IEntityTypeConfiguration<QuestionCollectionItem>
{
public void Configure(EntityTypeBuilder<QuestionCollectionItem> builder)
{
builder.ConfigureTenantEntity("question_collection_items");
builder.ConfigureTimestamps();
builder.Property(entity => entity.SectionKey).HasMaxLength(100);
builder.Property(entity => entity.Score).HasPrecision(8, 2);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new
{
entity.TenantId,
entity.CollectionId,
entity.QuestionId
}).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.CollectionId,
entity.SectionKey,
entity.SortOrder
});
builder.HasOne<QuestionCollection>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.CollectionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<Question>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.QuestionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
}
}
internal sealed class PracticeBlueprintConfiguration : IEntityTypeConfiguration<PracticeBlueprint>
{
public void Configure(EntityTypeBuilder<PracticeBlueprint> builder)
{
builder.ConfigureTenantEntity("practice_blueprints");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Name).HasMaxLength(300);
builder.Property(entity => entity.Mode).HasSnakeCaseEnum();
builder.Property(entity => entity.AssemblyType).HasSnakeCaseEnum();
builder.Property(entity => entity.TotalScore).HasPrecision(8, 2);
builder.Property(entity => entity.PassScore).HasPrecision(8, 2);
builder.Property(entity => entity.Sections).IsJson("[]");
builder.Property(entity => entity.Rules).IsJson("{}");
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.NodeId,
entity.CollectionId,
entity.Mode,
entity.Status,
entity.SortOrder
});
builder.ToTable(table =>
{
table.HasCheckConstraint(
"ck_practice_blueprints_question_limit",
"question_limit is null or question_limit > 0");
table.HasCheckConstraint(
"ck_practice_blueprints_duration",
"duration_minutes is null or duration_minutes > 0");
});
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.NodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<QuestionCollection>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.CollectionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.CreatedBy)
.OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.UpdatedBy)
.OnDelete(DeleteBehavior.SetNull);
}
}

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Tiku.Domain.Content;
using Tiku.Domain.Identity;
using Tiku.Domain.Learning;
using Tiku.Domain.QuestionBanks;
@@ -16,12 +17,30 @@ internal sealed class PracticeSessionConfiguration : IEntityTypeConfiguration<Pr
builder.Property(entity => entity.Mode).HasMaxLength(50);
builder.Property(entity => entity.TargetType).HasMaxLength(50);
builder.Property(entity => entity.StartedAt).HasDefaultValueSql("now()");
builder.Property(entity => entity.QuestionIds).IsJson("[]");
builder.Property(entity => entity.TotalScore).HasPrecision(8, 2);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.StartedAt });
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<PracticeBlueprint>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.BlueprintId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<QuestionCollection>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.CollectionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ContentNodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}

View File

@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Tiku.Domain.Catalog;
using Tiku.Domain.Content;
using Tiku.Domain.Identity;
using Tiku.Domain.QuestionBanks;
@@ -37,6 +38,7 @@ internal sealed class QuestionConfiguration : IEntityTypeConfiguration<Question>
builder.Property(entity => entity.Type).HasMaxLength(50);
builder.Property(entity => entity.TypeLabel).HasMaxLength(100);
builder.Property(entity => entity.Tags).IsJson("[]");
builder.Property(entity => entity.ExamMarkers).IsJson("{}");
builder.Property(entity => entity.MediaUrl).HasMaxLength(2048);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
@@ -57,6 +59,18 @@ internal sealed class QuestionConfiguration : IEntityTypeConfiguration<Question>
.HasForeignKey(entity => new { entity.TenantId, entity.NodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ContentNodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<QuestionCollection>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.PrimaryCollectionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<QuestionVersion>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.Id, entity.CurrentVersionId })

View File

@@ -51,3 +51,61 @@ internal sealed class TenantMembershipConfiguration : IEntityTypeConfiguration<T
.OnDelete(DeleteBehavior.Cascade);
}
}
internal sealed class TenantDomainConfiguration : IEntityTypeConfiguration<TenantDomain>
{
public void Configure(EntityTypeBuilder<TenantDomain> builder)
{
builder.ConfigureTenantEntity("tenant_domains");
builder.ConfigureTimestamps();
builder.Property(entity => entity.Host).HasColumnType("citext").HasMaxLength(253);
builder.Property(entity => entity.DomainType).HasSnakeCaseEnum();
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.VerificationToken).HasMaxLength(256);
builder.HasIndex(entity => entity.Host).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.IsPrimary })
.IsUnique()
.HasFilter("is_primary");
}
}
internal sealed class TenantBrandingConfiguration : IEntityTypeConfiguration<TenantBranding>
{
public void Configure(EntityTypeBuilder<TenantBranding> builder)
{
builder.ToTable("tenant_branding");
builder.HasKey(entity => entity.TenantId);
builder.ConfigureTimestamps();
builder.Property(entity => entity.BrandName).HasMaxLength(200);
builder.Property(entity => entity.ShortName).HasMaxLength(100);
builder.Property(entity => entity.Slogan).HasMaxLength(300);
builder.Property(entity => entity.OrganizationName).HasMaxLength(300);
builder.Property(entity => entity.LogoUrl).HasMaxLength(2048);
builder.Property(entity => entity.FaviconUrl).HasMaxLength(2048);
builder.Property(entity => entity.ServiceWechat).HasMaxLength(100);
builder.Property(entity => entity.ServiceAccountName).HasMaxLength(200);
builder.Property(entity => entity.Theme).IsJson("{}");
builder.Property(entity => entity.PublicAssets).IsJson("{}");
builder.HasOne<Tenant>().WithOne()
.HasForeignKey<TenantBranding>(entity => entity.TenantId)
.OnDelete(DeleteBehavior.Cascade);
}
}
internal sealed class TenantSettingsConfiguration : IEntityTypeConfiguration<TenantSettings>
{
public void Configure(EntityTypeBuilder<TenantSettings> builder)
{
builder.ToTable("tenant_settings");
builder.HasKey(entity => entity.TenantId);
builder.ConfigureTimestamps();
builder.Property(entity => entity.FeatureFlags).IsJson("{}");
builder.Property(entity => entity.AdminFeatureFlags).IsJson("{}");
builder.Property(entity => entity.PublicConfig).IsJson("{}");
builder.HasOne<Tenant>().WithOne()
.HasForeignKey<TenantSettings>(entity => entity.TenantId)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@@ -0,0 +1,932 @@
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Tiku.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddContentNavigationAndTenantConfiguration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("Npgsql:PostgresExtension:citext", ",,")
.Annotation("Npgsql:PostgresExtension:ltree", ",,")
.OldAnnotation("Npgsql:PostgresExtension:citext", ",,");
migrationBuilder.AddColumn<Guid>(
name: "content_node_id",
table: "questions",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "entry_id",
table: "questions",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<JsonElement>(
name: "exam_markers",
table: "questions",
type: "jsonb",
nullable: false,
defaultValueSql: "'{}'::jsonb");
migrationBuilder.AddColumn<Guid>(
name: "primary_collection_id",
table: "questions",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "blueprint_id",
table: "practice_sessions",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "collection_id",
table: "practice_sessions",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "content_node_id",
table: "practice_sessions",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "duration_minutes",
table: "practice_sessions",
type: "integer",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "entry_id",
table: "practice_sessions",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "expires_at",
table: "practice_sessions",
type: "timestamp with time zone",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "question_count",
table: "practice_sessions",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<JsonElement>(
name: "question_ids",
table: "practice_sessions",
type: "jsonb",
nullable: false,
defaultValueSql: "'[]'::jsonb");
migrationBuilder.AddColumn<decimal>(
name: "total_score",
table: "practice_sessions",
type: "numeric(8,2)",
precision: 8,
scale: 2,
nullable: true);
migrationBuilder.CreateTable(
name: "content_entries",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
entry_key = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
entry_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
icon = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
route = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
description = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
visibility = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
access_rules = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
layout_config = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
updated_by = table.Column<Guid>(type: "uuid", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_content_entries", x => x.id);
table.UniqueConstraint("ak_content_entries_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_content_entries_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_content_entries_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_content_entries_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_content_entries_users_updated_by",
column: x => x.updated_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "tenant_branding",
columns: table => new
{
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
brand_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
short_name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
slogan = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
organization_name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
logo_url = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
favicon_url = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
service_wechat = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
service_account_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
theme = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
public_assets = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_branding", x => x.tenant_id);
table.ForeignKey(
name: "fk_tenant_branding_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tenant_domains",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
host = table.Column<string>(type: "citext", maxLength: 253, nullable: false),
domain_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
is_primary = table.Column<bool>(type: "boolean", nullable: false),
verification_token = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
verified_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_domains", x => x.id);
table.UniqueConstraint("ak_tenant_domains_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_tenant_domains_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "tenant_settings",
columns: table => new
{
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
feature_flags = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
admin_feature_flags = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
public_config = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_tenant_settings", x => x.tenant_id);
table.ForeignKey(
name: "fk_tenant_settings_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "content_nodes",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
entry_id = table.Column<Guid>(type: "uuid", nullable: false),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
parent_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
node_key = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
node_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
marker_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
marker_config = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
path = table.Column<string>(type: "ltree", nullable: true),
depth = table.Column<int>(type: "integer", nullable: false),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
is_selectable = table.Column<bool>(type: "boolean", nullable: false),
is_leaf = table.Column<bool>(type: "boolean", nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
updated_by = table.Column<Guid>(type: "uuid", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_content_nodes", x => x.id);
table.UniqueConstraint("ak_content_nodes_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_content_nodes_depth", "depth >= 0");
table.ForeignKey(
name: "fk_content_nodes_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_content_nodes_content_nodes_tenant_id_parent_id",
columns: x => new { x.tenant_id, x.parent_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_content_nodes_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_content_nodes_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_content_nodes_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_content_nodes_users_updated_by",
column: x => x.updated_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "question_collections",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
entry_id = table.Column<Guid>(type: "uuid", nullable: true),
node_id = table.Column<Guid>(type: "uuid", nullable: true),
subject_id = table.Column<Guid>(type: "uuid", nullable: true),
category_id = table.Column<Guid>(type: "uuid", nullable: true),
question_bank_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
collection_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
source_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
filters = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
question_count = table.Column<int>(type: "integer", nullable: false),
total_score = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
duration_minutes = table.Column<int>(type: "integer", nullable: true),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
sort_order = table.Column<int>(type: "integer", nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
updated_by = table.Column<Guid>(type: "uuid", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_question_collections", x => x.id);
table.UniqueConstraint("ak_question_collections_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_question_collections_duration", "duration_minutes is null or duration_minutes > 0");
table.CheckConstraint("ck_question_collections_question_count", "question_count >= 0");
table.ForeignKey(
name: "fk_question_collections_categories_tenant_id_category_id",
columns: x => new { x.tenant_id, x.category_id },
principalTable: "categories",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_question_collections_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_question_collections_content_nodes_tenant_id_node_id",
columns: x => new { x.tenant_id, x.node_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_question_collections_question_banks_tenant_id_question_bank~",
columns: x => new { x.tenant_id, x.question_bank_id },
principalTable: "question_banks",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_question_collections_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_question_collections_subjects_tenant_id_subject_id",
columns: x => new { x.tenant_id, x.subject_id },
principalTable: "subjects",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_question_collections_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_question_collections_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_question_collections_users_updated_by",
column: x => x.updated_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "practice_blueprints",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
entry_id = table.Column<Guid>(type: "uuid", nullable: true),
node_id = table.Column<Guid>(type: "uuid", nullable: true),
collection_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
mode = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
assembly_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
question_limit = table.Column<int>(type: "integer", nullable: true),
duration_minutes = table.Column<int>(type: "integer", nullable: true),
total_score = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
pass_score = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
sections = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'[]'::jsonb"),
rules = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
sort_order = table.Column<int>(type: "integer", nullable: false),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
updated_by = table.Column<Guid>(type: "uuid", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_practice_blueprints", x => x.id);
table.UniqueConstraint("ak_practice_blueprints_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_practice_blueprints_duration", "duration_minutes is null or duration_minutes > 0");
table.CheckConstraint("ck_practice_blueprints_question_limit", "question_limit is null or question_limit > 0");
table.ForeignKey(
name: "fk_practice_blueprints_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_practice_blueprints_content_nodes_tenant_id_node_id",
columns: x => new { x.tenant_id, x.node_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_practice_blueprints_question_collections_tenant_id_collecti~",
columns: x => new { x.tenant_id, x.collection_id },
principalTable: "question_collections",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_practice_blueprints_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_practice_blueprints_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_practice_blueprints_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_practice_blueprints_users_updated_by",
column: x => x.updated_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "question_collection_items",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
collection_id = table.Column<Guid>(type: "uuid", nullable: false),
question_id = table.Column<Guid>(type: "uuid", nullable: false),
section_key = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
sort_order = table.Column<int>(type: "integer", nullable: false),
score = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
required = table.Column<bool>(type: "boolean", nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_question_collection_items", x => x.id);
table.UniqueConstraint("ak_question_collection_items_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_question_collection_items_question_collections_tenant_id_co~",
columns: x => new { x.tenant_id, x.collection_id },
principalTable: "question_collections",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_question_collection_items_questions_tenant_id_question_id",
columns: x => new { x.tenant_id, x.question_id },
principalTable: "questions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_question_collection_items_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "ix_questions_tenant_id_content_node_id",
table: "questions",
columns: new[] { "tenant_id", "content_node_id" });
migrationBuilder.CreateIndex(
name: "ix_questions_tenant_id_entry_id",
table: "questions",
columns: new[] { "tenant_id", "entry_id" });
migrationBuilder.CreateIndex(
name: "ix_questions_tenant_id_primary_collection_id",
table: "questions",
columns: new[] { "tenant_id", "primary_collection_id" });
migrationBuilder.CreateIndex(
name: "ix_practice_sessions_tenant_id_blueprint_id",
table: "practice_sessions",
columns: new[] { "tenant_id", "blueprint_id" });
migrationBuilder.CreateIndex(
name: "ix_practice_sessions_tenant_id_collection_id",
table: "practice_sessions",
columns: new[] { "tenant_id", "collection_id" });
migrationBuilder.CreateIndex(
name: "ix_practice_sessions_tenant_id_content_node_id",
table: "practice_sessions",
columns: new[] { "tenant_id", "content_node_id" });
migrationBuilder.CreateIndex(
name: "ix_practice_sessions_tenant_id_entry_id",
table: "practice_sessions",
columns: new[] { "tenant_id", "entry_id" });
migrationBuilder.CreateIndex(
name: "ix_content_entries_created_by",
table: "content_entries",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_content_entries_tenant_id_entry_key",
table: "content_entries",
columns: new[] { "tenant_id", "entry_key" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_content_entries_tenant_id_legacy_id",
table: "content_entries",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_content_entries_tenant_id_region_id_entry_type_is_active_so~",
table: "content_entries",
columns: new[] { "tenant_id", "region_id", "entry_type", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_content_entries_updated_by",
table: "content_entries",
column: "updated_by");
migrationBuilder.CreateIndex(
name: "ix_content_nodes_created_by",
table: "content_nodes",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_content_nodes_path",
table: "content_nodes",
column: "path")
.Annotation("Npgsql:IndexMethod", "gist");
migrationBuilder.CreateIndex(
name: "ix_content_nodes_tenant_id_entry_id_node_key",
table: "content_nodes",
columns: new[] { "tenant_id", "entry_id", "node_key" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_content_nodes_tenant_id_entry_id_parent_id_sort_order",
table: "content_nodes",
columns: new[] { "tenant_id", "entry_id", "parent_id", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_content_nodes_tenant_id_legacy_id",
table: "content_nodes",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_content_nodes_tenant_id_marker_type",
table: "content_nodes",
columns: new[] { "tenant_id", "marker_type" },
filter: "marker_type is not null");
migrationBuilder.CreateIndex(
name: "ix_content_nodes_tenant_id_parent_id",
table: "content_nodes",
columns: new[] { "tenant_id", "parent_id" });
migrationBuilder.CreateIndex(
name: "ix_content_nodes_tenant_id_region_id",
table: "content_nodes",
columns: new[] { "tenant_id", "region_id" });
migrationBuilder.CreateIndex(
name: "ix_content_nodes_updated_by",
table: "content_nodes",
column: "updated_by");
migrationBuilder.CreateIndex(
name: "ix_practice_blueprints_created_by",
table: "practice_blueprints",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_practice_blueprints_tenant_id_collection_id",
table: "practice_blueprints",
columns: new[] { "tenant_id", "collection_id" });
migrationBuilder.CreateIndex(
name: "ix_practice_blueprints_tenant_id_entry_id",
table: "practice_blueprints",
columns: new[] { "tenant_id", "entry_id" });
migrationBuilder.CreateIndex(
name: "ix_practice_blueprints_tenant_id_legacy_id",
table: "practice_blueprints",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_practice_blueprints_tenant_id_node_id_collection_id_mode_st~",
table: "practice_blueprints",
columns: new[] { "tenant_id", "node_id", "collection_id", "mode", "status", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_practice_blueprints_tenant_id_region_id",
table: "practice_blueprints",
columns: new[] { "tenant_id", "region_id" });
migrationBuilder.CreateIndex(
name: "ix_practice_blueprints_updated_by",
table: "practice_blueprints",
column: "updated_by");
migrationBuilder.CreateIndex(
name: "ix_question_collection_items_tenant_id_collection_id_question_~",
table: "question_collection_items",
columns: new[] { "tenant_id", "collection_id", "question_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_question_collection_items_tenant_id_collection_id_section_k~",
table: "question_collection_items",
columns: new[] { "tenant_id", "collection_id", "section_key", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_question_collection_items_tenant_id_question_id",
table: "question_collection_items",
columns: new[] { "tenant_id", "question_id" });
migrationBuilder.CreateIndex(
name: "ix_question_collections_created_by",
table: "question_collections",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_question_collections_tenant_id_category_id",
table: "question_collections",
columns: new[] { "tenant_id", "category_id" });
migrationBuilder.CreateIndex(
name: "ix_question_collections_tenant_id_entry_id",
table: "question_collections",
columns: new[] { "tenant_id", "entry_id" });
migrationBuilder.CreateIndex(
name: "ix_question_collections_tenant_id_legacy_id",
table: "question_collections",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_question_collections_tenant_id_node_id_status_sort_order",
table: "question_collections",
columns: new[] { "tenant_id", "node_id", "status", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_question_collections_tenant_id_question_bank_id",
table: "question_collections",
columns: new[] { "tenant_id", "question_bank_id" });
migrationBuilder.CreateIndex(
name: "ix_question_collections_tenant_id_region_id",
table: "question_collections",
columns: new[] { "tenant_id", "region_id" });
migrationBuilder.CreateIndex(
name: "ix_question_collections_tenant_id_subject_id",
table: "question_collections",
columns: new[] { "tenant_id", "subject_id" });
migrationBuilder.CreateIndex(
name: "ix_question_collections_updated_by",
table: "question_collections",
column: "updated_by");
migrationBuilder.CreateIndex(
name: "ix_tenant_domains_host",
table: "tenant_domains",
column: "host",
unique: true);
migrationBuilder.CreateIndex(
name: "ix_tenant_domains_tenant_id_is_primary",
table: "tenant_domains",
columns: new[] { "tenant_id", "is_primary" },
unique: true,
filter: "is_primary");
migrationBuilder.AddForeignKey(
name: "fk_practice_sessions_content_entries_tenant_id_entry_id",
table: "practice_sessions",
columns: new[] { "tenant_id", "entry_id" },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "fk_practice_sessions_content_nodes_tenant_id_content_node_id",
table: "practice_sessions",
columns: new[] { "tenant_id", "content_node_id" },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "fk_practice_sessions_practice_blueprints_tenant_id_blueprint_id",
table: "practice_sessions",
columns: new[] { "tenant_id", "blueprint_id" },
principalTable: "practice_blueprints",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "fk_practice_sessions_question_collections_tenant_id_collection~",
table: "practice_sessions",
columns: new[] { "tenant_id", "collection_id" },
principalTable: "question_collections",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "fk_questions_content_entries_tenant_id_entry_id",
table: "questions",
columns: new[] { "tenant_id", "entry_id" },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "fk_questions_content_nodes_tenant_id_content_node_id",
table: "questions",
columns: new[] { "tenant_id", "content_node_id" },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "fk_questions_question_collections_tenant_id_primary_collection~",
table: "questions",
columns: new[] { "tenant_id", "primary_collection_id" },
principalTable: "question_collections",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "fk_practice_sessions_content_entries_tenant_id_entry_id",
table: "practice_sessions");
migrationBuilder.DropForeignKey(
name: "fk_practice_sessions_content_nodes_tenant_id_content_node_id",
table: "practice_sessions");
migrationBuilder.DropForeignKey(
name: "fk_practice_sessions_practice_blueprints_tenant_id_blueprint_id",
table: "practice_sessions");
migrationBuilder.DropForeignKey(
name: "fk_practice_sessions_question_collections_tenant_id_collection~",
table: "practice_sessions");
migrationBuilder.DropForeignKey(
name: "fk_questions_content_entries_tenant_id_entry_id",
table: "questions");
migrationBuilder.DropForeignKey(
name: "fk_questions_content_nodes_tenant_id_content_node_id",
table: "questions");
migrationBuilder.DropForeignKey(
name: "fk_questions_question_collections_tenant_id_primary_collection~",
table: "questions");
migrationBuilder.DropTable(
name: "practice_blueprints");
migrationBuilder.DropTable(
name: "question_collection_items");
migrationBuilder.DropTable(
name: "tenant_branding");
migrationBuilder.DropTable(
name: "tenant_domains");
migrationBuilder.DropTable(
name: "tenant_settings");
migrationBuilder.DropTable(
name: "question_collections");
migrationBuilder.DropTable(
name: "content_nodes");
migrationBuilder.DropTable(
name: "content_entries");
migrationBuilder.DropIndex(
name: "ix_questions_tenant_id_content_node_id",
table: "questions");
migrationBuilder.DropIndex(
name: "ix_questions_tenant_id_entry_id",
table: "questions");
migrationBuilder.DropIndex(
name: "ix_questions_tenant_id_primary_collection_id",
table: "questions");
migrationBuilder.DropIndex(
name: "ix_practice_sessions_tenant_id_blueprint_id",
table: "practice_sessions");
migrationBuilder.DropIndex(
name: "ix_practice_sessions_tenant_id_collection_id",
table: "practice_sessions");
migrationBuilder.DropIndex(
name: "ix_practice_sessions_tenant_id_content_node_id",
table: "practice_sessions");
migrationBuilder.DropIndex(
name: "ix_practice_sessions_tenant_id_entry_id",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "content_node_id",
table: "questions");
migrationBuilder.DropColumn(
name: "entry_id",
table: "questions");
migrationBuilder.DropColumn(
name: "exam_markers",
table: "questions");
migrationBuilder.DropColumn(
name: "primary_collection_id",
table: "questions");
migrationBuilder.DropColumn(
name: "blueprint_id",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "collection_id",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "content_node_id",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "duration_minutes",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "entry_id",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "expires_at",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "question_count",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "question_ids",
table: "practice_sessions");
migrationBuilder.DropColumn(
name: "total_score",
table: "practice_sessions");
migrationBuilder.AlterDatabase()
.Annotation("Npgsql:PostgresExtension:citext", ",,")
.OldAnnotation("Npgsql:PostgresExtension:citext", ",,")
.OldAnnotation("Npgsql:PostgresExtension:ltree", ",,");
}
}
}

View File

@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Tiku.Domain.Catalog;
using Tiku.Domain.Common;
using Tiku.Domain.Content;
using Tiku.Domain.Identity;
using Tiku.Domain.Learning;
using Tiku.Domain.QuestionBanks;
@@ -14,6 +15,9 @@ public sealed class TikuDbContext(DbContextOptions<TikuDbContext> options) : DbC
public DbSet<User> Users => Set<User>();
public DbSet<UserIdentity> UserIdentities => Set<UserIdentity>();
public DbSet<TenantMembership> TenantMemberships => Set<TenantMembership>();
public DbSet<TenantDomain> TenantDomains => Set<TenantDomain>();
public DbSet<TenantBranding> TenantBrandings => Set<TenantBranding>();
public DbSet<TenantSettings> TenantSettings => Set<TenantSettings>();
public DbSet<StudentProfile> StudentProfiles => Set<StudentProfile>();
public DbSet<Region> Regions => Set<Region>();
public DbSet<RegionModule> RegionModules => Set<RegionModule>();
@@ -25,6 +29,11 @@ public sealed class TikuDbContext(DbContextOptions<TikuDbContext> options) : DbC
public DbSet<QuestionBank> QuestionBanks => Set<QuestionBank>();
public DbSet<Question> Questions => Set<Question>();
public DbSet<QuestionVersion> QuestionVersions => Set<QuestionVersion>();
public DbSet<ContentEntry> ContentEntries => Set<ContentEntry>();
public DbSet<ContentNode> ContentNodes => Set<ContentNode>();
public DbSet<QuestionCollection> QuestionCollections => Set<QuestionCollection>();
public DbSet<QuestionCollectionItem> QuestionCollectionItems => Set<QuestionCollectionItem>();
public DbSet<PracticeBlueprint> PracticeBlueprints => Set<PracticeBlueprint>();
public DbSet<PracticeSession> PracticeSessions => Set<PracticeSession>();
public DbSet<AnswerRecord> AnswerRecords => Set<AnswerRecord>();
public DbSet<FavoriteQuestion> FavoriteQuestions => Set<FavoriteQuestion>();
@@ -34,6 +43,7 @@ public sealed class TikuDbContext(DbContextOptions<TikuDbContext> options) : DbC
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasPostgresExtension("citext");
modelBuilder.HasPostgresExtension("ltree");
modelBuilder.ApplyConfigurationsFromAssembly(typeof(TikuDbContext).Assembly);
modelBuilder.UseSnakeCaseIdentifiers();
}

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Tiku.Domain.Content;
using Tiku.Domain.QuestionBanks;
using Tiku.Infrastructure.Persistence;
@@ -21,13 +22,46 @@ public sealed class PersistenceModelTests
.Select(entity => entity.GetTableName())
.ToHashSet(StringComparer.Ordinal);
Assert.Equal(20, tableNames.Count);
Assert.Equal(28, tableNames.Count);
Assert.Contains("tenants", tableNames);
Assert.Contains("tenant_settings", tableNames);
Assert.Contains("content_entries", tableNames);
Assert.Contains("content_nodes", tableNames);
Assert.Contains("question_collections", tableNames);
Assert.Contains("practice_blueprints", tableNames);
Assert.Contains("questions", tableNames);
Assert.Contains("question_versions", tableNames);
Assert.Contains("answer_records", tableNames);
}
[Fact]
public void Content_node_path_is_mapped_to_ltree()
{
using var context = new TikuDbContext(Options);
var path = context.Model.FindEntityType(typeof(ContentNode))!
.FindProperty(nameof(ContentNode.Path))!;
Assert.Equal("ltree", path.GetColumnType());
}
[Fact]
public void Collection_relations_include_tenant_in_foreign_keys()
{
using var context = new TikuDbContext(Options);
var foreignKeys = context.Model.FindEntityType(typeof(QuestionCollection))!
.GetForeignKeys()
.Where(foreignKey => foreignKey.Properties.Count > 1)
.Select(foreignKey => foreignKey.Properties
.Select(property => property.Name)
.ToArray());
Assert.All(
foreignKeys,
properties => Assert.Contains("TenantId", properties));
}
[Theory]
[InlineData(nameof(Question.Tags), "'[]'::jsonb")]
public void Question_json_properties_are_mapped_to_jsonb(