From 30db6ad9f17a6a422512647808ca425c75c2c344 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 2 May 2025 00:08:21 +0800 Subject: [PATCH] :card_file_box: Realm database modeling --- DysonNetwork.Sphere/AppDatabase.cs | 38 +++++++++++++------ .../Permission/PermissionService.cs | 2 +- DysonNetwork.Sphere/Post/Publisher.cs | 2 +- DysonNetwork.Sphere/Realm/Realm.cs | 25 +++++++++++- DysonNetwork.Sphere/Realm/RealmController.cs | 10 +++++ DysonNetwork.Sphere/Realm/RealmService.cs | 6 +++ 6 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 DysonNetwork.Sphere/Realm/RealmController.cs create mode 100644 DysonNetwork.Sphere/Realm/RealmService.cs diff --git a/DysonNetwork.Sphere/AppDatabase.cs b/DysonNetwork.Sphere/AppDatabase.cs index c55fffb..9d977fb 100644 --- a/DysonNetwork.Sphere/AppDatabase.cs +++ b/DysonNetwork.Sphere/AppDatabase.cs @@ -20,11 +20,11 @@ public class AppDatabase( IConfiguration configuration ) : DbContext(options) { - public DbSet PermissionNodes { get; set; } = null!; - public DbSet PermissionGroups { get; set; } = null!; - public DbSet PermissionGroupMembers { get; set; } = null!; + public DbSet PermissionNodes { get; set; } + public DbSet PermissionGroups { get; set; } + public DbSet PermissionGroupMembers { get; set; } - public DbSet MagicSpells { get; set; } = null!; + public DbSet MagicSpells { get; set; } public DbSet Accounts { get; set; } public DbSet AccountProfiles { get; set; } public DbSet AccountContacts { get; set; } @@ -48,6 +48,9 @@ public class AppDatabase( public DbSet PostCategories { get; set; } public DbSet PostCollections { get; set; } + public DbSet Realms { get; set; } + public DbSet RealmMembers { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var dataSourceBuilder = new NpgsqlDataSourceBuilder(configuration.GetConnectionString("App")); @@ -154,19 +157,30 @@ public class AppDatabase( .HasMany(p => p.Collections) .WithMany(c => c.Posts) .UsingEntity(j => j.ToTable("post_collection_links")); + + modelBuilder.Entity() + .HasKey(pm => new { pm.RealmId, pm.AccountId }); + modelBuilder.Entity() + .HasOne(pm => pm.Realm) + .WithMany(p => p.Members) + .HasForeignKey(pm => pm.RealmId) + .OnDelete(DeleteBehavior.Cascade); + modelBuilder.Entity() + .HasOne(pm => pm.Account) + .WithMany() + .HasForeignKey(pm => pm.AccountId) + .OnDelete(DeleteBehavior.Cascade); // Automatically apply soft-delete filter to all entities inheriting BaseModel foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { - if (typeof(ModelBase).IsAssignableFrom(entityType.ClrType)) - { - var method = typeof(AppDatabase) - .GetMethod(nameof(SetSoftDeleteFilter), - System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)! - .MakeGenericMethod(entityType.ClrType); + if (!typeof(ModelBase).IsAssignableFrom(entityType.ClrType)) continue; + var method = typeof(AppDatabase) + .GetMethod(nameof(SetSoftDeleteFilter), + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)! + .MakeGenericMethod(entityType.ClrType); - method.Invoke(null, [modelBuilder]); - } + method.Invoke(null, [modelBuilder]); } } diff --git a/DysonNetwork.Sphere/Permission/PermissionService.cs b/DysonNetwork.Sphere/Permission/PermissionService.cs index d200521..1cbef7a 100644 --- a/DysonNetwork.Sphere/Permission/PermissionService.cs +++ b/DysonNetwork.Sphere/Permission/PermissionService.cs @@ -23,7 +23,7 @@ public class PermissionService(AppDatabase db) .ToListAsync(); var permission = await db.PermissionNodes .Where(n => n.GroupId == null || groupsId.Contains(n.GroupId.Value)) - .Where(n => n.Key == key && (n.GroupId != null || n.Actor == actor) && n.Area == area) + .Where(n => (n.Key == key || n.Key == "*") && (n.GroupId != null || n.Actor == actor) && n.Area == area) .Where(n => n.ExpiredAt == null || n.ExpiredAt < now) .Where(n => n.AffectedAt == null || n.AffectedAt >= now) .FirstOrDefaultAsync(); diff --git a/DysonNetwork.Sphere/Post/Publisher.cs b/DysonNetwork.Sphere/Post/Publisher.cs index be15c1b..9aeeb1c 100644 --- a/DysonNetwork.Sphere/Post/Publisher.cs +++ b/DysonNetwork.Sphere/Post/Publisher.cs @@ -41,7 +41,7 @@ public enum PublisherMemberRole Viewer = 25 } -public class PublisherMember : ModelBase +public class PublisherMember { public long PublisherId { get; set; } [JsonIgnore] public Publisher Publisher { get; set; } = null!; diff --git a/DysonNetwork.Sphere/Realm/Realm.cs b/DysonNetwork.Sphere/Realm/Realm.cs index 6e8da29..39d75bf 100644 --- a/DysonNetwork.Sphere/Realm/Realm.cs +++ b/DysonNetwork.Sphere/Realm/Realm.cs @@ -1,6 +1,7 @@ using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; using DysonNetwork.Sphere.Storage; +using NodaTime; namespace DysonNetwork.Sphere.Realm; @@ -12,7 +13,27 @@ public class Realm : ModelBase public CloudFile? Picture { get; set; } public CloudFile? Background { get; set; } + + [JsonIgnore] public ICollection Members { get; set; } = new List(); - public long? AccountId { get; set; } - [JsonIgnore] public Account.Account? Account { get; set; } + public long AccountId { get; set; } + [JsonIgnore] public Account.Account Account { get; set; } +} + +public enum RealmMemberRole +{ + Owner = 100, + Moderator = 50, + Normal = 0 +} + +public class RealmMember : ModelBase +{ + public long RealmId { get; set; } + [JsonIgnore] public Realm Realm { get; set; } = null!; + public long AccountId { get; set; } + [JsonIgnore] public Account.Account Account { get; set; } = null!; + + public RealmMemberRole Role { get; set; } + public Instant? JoinedAt { get; set; } } \ No newline at end of file diff --git a/DysonNetwork.Sphere/Realm/RealmController.cs b/DysonNetwork.Sphere/Realm/RealmController.cs new file mode 100644 index 0000000..599947a --- /dev/null +++ b/DysonNetwork.Sphere/Realm/RealmController.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Mvc; + +namespace DysonNetwork.Sphere.Realm; + +[ApiController] +[Route("/realms")] +public class RealmController : ControllerBase +{ + +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Realm/RealmService.cs b/DysonNetwork.Sphere/Realm/RealmService.cs new file mode 100644 index 0000000..4079abf --- /dev/null +++ b/DysonNetwork.Sphere/Realm/RealmService.cs @@ -0,0 +1,6 @@ +namespace DysonNetwork.Sphere.Realm; + +public class RealmService(AppDatabase db) +{ + +} \ No newline at end of file