Browse Source

Move model configuration to its own classes

Fernando Fernández 3 years ago
parent
commit
f4af78817d

+ 2 - 92
Jellyfin.Server.Implementations/JellyfinDb.cs

@@ -153,100 +153,10 @@ namespace Jellyfin.Server.Implementations
         {
         {
             modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);
             modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);
             base.OnModelCreating(modelBuilder);
             base.OnModelCreating(modelBuilder);
-
             modelBuilder.HasDefaultSchema("jellyfin");
             modelBuilder.HasDefaultSchema("jellyfin");
 
 
-            // Collations
-
-            modelBuilder.Entity<User>()
-                .Property(user => user.Username)
-                .UseCollation("NOCASE");
-
-            // Delete behavior
-
-            modelBuilder.Entity<User>()
-                .HasOne(u => u.ProfileImage)
-                .WithOne()
-                .OnDelete(DeleteBehavior.Cascade);
-
-            modelBuilder.Entity<User>()
-                .HasMany(u => u.Permissions)
-                .WithOne()
-                .HasForeignKey(p => p.UserId)
-                .OnDelete(DeleteBehavior.Cascade);
-
-            modelBuilder.Entity<User>()
-                .HasMany(u => u.Preferences)
-                .WithOne()
-                .HasForeignKey(p => p.UserId)
-                .OnDelete(DeleteBehavior.Cascade);
-
-            modelBuilder.Entity<User>()
-                .HasMany(u => u.AccessSchedules)
-                .WithOne()
-                .OnDelete(DeleteBehavior.Cascade);
-
-            modelBuilder.Entity<User>()
-                .HasMany(u => u.DisplayPreferences)
-                .WithOne()
-                .OnDelete(DeleteBehavior.Cascade);
-
-            modelBuilder.Entity<User>()
-                .HasMany(u => u.ItemDisplayPreferences)
-                .WithOne()
-                .OnDelete(DeleteBehavior.Cascade);
-
-            modelBuilder.Entity<DisplayPreferences>()
-                .HasMany(d => d.HomeSections)
-                .WithOne()
-                .OnDelete(DeleteBehavior.Cascade);
-
-            // Indexes
-
-            modelBuilder.Entity<ApiKey>()
-                .HasIndex(entity => entity.AccessToken)
-                .IsUnique();
-
-            modelBuilder.Entity<User>()
-                .HasIndex(entity => entity.Username)
-                .IsUnique();
-
-            modelBuilder.Entity<Device>()
-                .HasIndex(entity => new { entity.DeviceId, entity.DateLastActivity });
-
-            modelBuilder.Entity<Device>()
-                .HasIndex(entity => new { entity.AccessToken, entity.DateLastActivity });
-
-            modelBuilder.Entity<Device>()
-                .HasIndex(entity => new { entity.UserId, entity.DeviceId });
-
-            modelBuilder.Entity<Device>()
-                .HasIndex(entity => entity.DeviceId);
-
-            modelBuilder.Entity<DeviceOptions>()
-                .HasIndex(entity => entity.DeviceId)
-                .IsUnique();
-
-            modelBuilder.Entity<DisplayPreferences>()
-                .HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client })
-                .IsUnique();
-
-            modelBuilder.Entity<CustomItemDisplayPreferences>()
-                .HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client, entity.Key })
-                .IsUnique();
-
-            // Used to get a user's permissions or a specific permission for a user.
-            // Also prevents multiple values being created for a user.
-            // Filtered over non-null user ids for when other entities (groups, API keys) get permissions
-            modelBuilder.Entity<Permission>()
-                .HasIndex(p => new { p.UserId, p.Kind })
-                .HasFilter("[UserId] IS NOT NULL")
-                .IsUnique();
-
-            modelBuilder.Entity<Preference>()
-                .HasIndex(p => new { p.UserId, p.Kind })
-                .HasFilter("[UserId] IS NOT NULL")
-                .IsUnique();
+            // Configuration for each entity is in it's own class inside 'ModelConfiguratio'.
+            modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDb).Assembly);
         }
         }
     }
     }
 }
 }

+ 22 - 0
Jellyfin.Server.Implementations/ModelConfiguration/ApiKeyConfiguration.cs

@@ -0,0 +1,22 @@
+using Jellyfin.Data.Entities.Security;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the ApiKey entity.
+    /// </summary>
+    public class ApiKeyConfiguration : IEntityTypeConfiguration<ApiKey>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<ApiKey> builder)
+        {
+            // Indexes
+
+            builder
+                .HasIndex(entity => entity.AccessToken)
+                .IsUnique();
+        }
+    }
+}

+ 22 - 0
Jellyfin.Server.Implementations/ModelConfiguration/CustomItemDisplayPreferencesConfiguration.cs

@@ -0,0 +1,22 @@
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the CustomItemDisplayPreferences entity.
+    /// </summary>
+    public class CustomItemDisplayPreferencesConfiguration : IEntityTypeConfiguration<CustomItemDisplayPreferences>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<CustomItemDisplayPreferences> builder)
+        {
+            // Indexes
+
+            builder
+                .HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client, entity.Key })
+                .IsUnique();
+        }
+    }
+}

+ 30 - 0
Jellyfin.Server.Implementations/ModelConfiguration/DeviceConfiguration.cs

@@ -0,0 +1,30 @@
+using Jellyfin.Data.Entities.Security;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the Device entity.
+    /// </summary>
+    public class DeviceConfiguration : IEntityTypeConfiguration<Device>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<Device> builder)
+        {
+            // Indexes
+
+            builder
+                .HasIndex(entity => new { entity.DeviceId, entity.DateLastActivity });
+
+            builder
+                .HasIndex(entity => new { entity.AccessToken, entity.DateLastActivity });
+
+            builder
+                .HasIndex(entity => new { entity.UserId, entity.DeviceId });
+
+            builder
+                .HasIndex(entity => entity.DeviceId);
+        }
+    }
+}

+ 22 - 0
Jellyfin.Server.Implementations/ModelConfiguration/DeviceOptionsConfiguration.cs

@@ -0,0 +1,22 @@
+using Jellyfin.Data.Entities.Security;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the DeviceOptions entity.
+    /// </summary>
+    public class DeviceOptionsConfiguration : IEntityTypeConfiguration<DeviceOptions>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<DeviceOptions> builder)
+        {
+            // Indexes
+
+            builder
+                .HasIndex(entity => entity.DeviceId)
+                .IsUnique();
+        }
+    }
+}

+ 29 - 0
Jellyfin.Server.Implementations/ModelConfiguration/DisplayPreferencesConfiguration.cs

@@ -0,0 +1,29 @@
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the DisplayPreferencesConfiguration entity.
+    /// </summary>
+    public class DisplayPreferencesConfiguration : IEntityTypeConfiguration<DisplayPreferences>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<DisplayPreferences> builder)
+        {
+            // Delete behaviour
+
+            builder
+                .HasMany(d => d.HomeSections)
+                .WithOne()
+                .OnDelete(DeleteBehavior.Cascade);
+
+            // Indexes
+
+            builder
+                .HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client })
+                .IsUnique();
+        }
+    }
+}

+ 26 - 0
Jellyfin.Server.Implementations/ModelConfiguration/PermissionConfiguration.cs

@@ -0,0 +1,26 @@
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the Permission entity.
+    /// </summary>
+    public class PermissionConfiguration : IEntityTypeConfiguration<Permission>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<Permission> builder)
+        {
+            // Indexes
+
+            // Used to get a user's permissions or a specific permission for a user.
+            // Also prevents multiple values being created for a user.
+            // Filtered over non-null user ids for when other entities (groups, API keys) get permissions
+            builder
+                .HasIndex(p => new { p.UserId, p.Kind })
+                .HasFilter("[UserId] IS NOT NULL")
+                .IsUnique();
+        }
+    }
+}

+ 23 - 0
Jellyfin.Server.Implementations/ModelConfiguration/PreferenceConfiguration.cs

@@ -0,0 +1,23 @@
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the Permission entity.
+    /// </summary>
+    public class PreferenceConfiguration : IEntityTypeConfiguration<Preference>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<Preference> builder)
+        {
+            // Indexes
+
+            builder
+                .HasIndex(p => new { p.UserId, p.Kind })
+                .HasFilter("[UserId] IS NOT NULL")
+                .IsUnique();
+        }
+    }
+}

+ 62 - 0
Jellyfin.Server.Implementations/ModelConfiguration/UserConfiguration.cs

@@ -0,0 +1,62 @@
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration
+{
+    /// <summary>
+    /// FluentAPI configuration for the User entity.
+    /// </summary>
+    public class UserConfiguration : IEntityTypeConfiguration<User>
+    {
+        /// <inheritdoc/>
+        public void Configure(EntityTypeBuilder<User> builder)
+        {
+            // Collations
+
+            builder
+                .Property(user => user.Username)
+                .UseCollation("NOCASE");
+
+            // Delete behavior
+
+            builder
+                .HasOne(u => u.ProfileImage)
+                .WithOne()
+                .OnDelete(DeleteBehavior.Cascade);
+
+            builder
+                .HasMany(u => u.Permissions)
+                .WithOne()
+                .HasForeignKey(p => p.UserId)
+                .OnDelete(DeleteBehavior.Cascade);
+
+            builder
+                .HasMany(u => u.Preferences)
+                .WithOne()
+                .HasForeignKey(p => p.UserId)
+                .OnDelete(DeleteBehavior.Cascade);
+
+            builder
+                .HasMany(u => u.AccessSchedules)
+                .WithOne()
+                .OnDelete(DeleteBehavior.Cascade);
+
+            builder
+                .HasMany(u => u.DisplayPreferences)
+                .WithOne()
+                .OnDelete(DeleteBehavior.Cascade);
+
+            builder
+                .HasMany(u => u.ItemDisplayPreferences)
+                .WithOne()
+                .OnDelete(DeleteBehavior.Cascade);
+
+            // Indexes
+
+            builder
+                .HasIndex(entity => entity.Username)
+                .IsUnique();
+        }
+    }
+}