The Free Software Media System - Server Backend & API
Move model configuration to its own classes
Fernando Fernándezf4af78817d58
diff --git a/Jellyfin.Server.Implementations/JellyfinDb.cs b/Jellyfin.Server.Implementations/JellyfinDb.csindex 6f35a2c1c2..b176e14a61 100644--- a/Jellyfin.Server.Implementations/JellyfinDb.cs+++ b/Jellyfin.Server.Implementations/JellyfinDb.cs@@ -153,100 +153,10 @@ namespace Jellyfin.Server.Implementations{modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);base.OnModelCreating(modelBuilder);-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);}}}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/ApiKeyConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/ApiKeyConfiguration.csnew file mode 100644index 0000000000..9067d3833d--- /dev/null+++ b/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();+ }+ }+}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/CustomItemDisplayPreferencesConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/CustomItemDisplayPreferencesConfiguration.csnew file mode 100644index 0000000000..75d9ba529e--- /dev/null+++ b/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();+ }+ }+}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/DeviceConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/DeviceConfiguration.csnew file mode 100644index 0000000000..5b4e711cc4--- /dev/null+++ b/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);+ }+ }+}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/DeviceOptionsConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/DeviceOptionsConfiguration.csnew file mode 100644index 0000000000..0db1944b58--- /dev/null+++ b/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();+ }+ }+}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/DisplayPreferencesConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/DisplayPreferencesConfiguration.csnew file mode 100644index 0000000000..807078803d--- /dev/null+++ b/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();+ }+ }+}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/PermissionConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/PermissionConfiguration.csnew file mode 100644index 0000000000..56e76d88dd--- /dev/null+++ b/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();+ }+ }+}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/PreferenceConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/PreferenceConfiguration.csnew file mode 100644index 0000000000..3cd5f8afbe--- /dev/null+++ b/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();+ }+ }+}diff --git a/Jellyfin.Server.Implementations/ModelConfiguration/UserConfiguration.cs b/Jellyfin.Server.Implementations/ModelConfiguration/UserConfiguration.csnew file mode 100644index 0000000000..ee88318048--- /dev/null+++ b/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();+ }+ }+}