Отчистка кода

This commit is contained in:
2025-04-24 17:58:58 +04:00
parent 31ad99ade5
commit cc7e97b6ff
20 changed files with 1972 additions and 1562 deletions
+144 -65
View File
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Pf2eModel.Model;
using Pf2eModel.Model.Base;
using Pf2eModel.Model.Base.Keywords;
@@ -17,8 +18,6 @@ using Pf2eModel.Model.Structure.ClassRelation;
using Pf2eModel.Model.Structure.DeityRelation;
using Pf2eModel.Model.Structure.FeatRelation;
using Pf2eModel.Model.Structure.SpellRelation;
using System.Linq.Expressions;
using System.Reflection;
namespace Pf2eModel;
@@ -37,15 +36,21 @@ public partial class Pf2eDbContext
DefineMethods(modelBuilder);
}
private void BindEntity<T>(EntityTypeBuilder<T> entity) where T : class, IEntity
private void BindEntity<T>(EntityTypeBuilder<T> entity)
where T : class, IEntity
{
entity.HasOne(entity => entity.Entity)
entity
.HasOne(entity => entity.Entity)
.WithOne()
.HasForeignKey<T>(entity => entity.Id)
.IsRequired();
}
private void BindLocaleString<T>(EntityTypeBuilder<T> entity, Expression<Func<T, LocaleString>> func) where T : class
private void BindLocaleString<T>(
EntityTypeBuilder<T> entity,
Expression<Func<T, LocaleString>> func
)
where T : class
{
/*
entity.OwnsMany(func, d =>
@@ -54,11 +59,9 @@ public partial class Pf2eDbContext
});
*/
entity.Property(func)
.HasConversion(
v => v.Serialize(),
v => LocaleString.Deserialize(v)
)
entity
.Property(func)
.HasConversion(v => v.Serialize(), v => LocaleString.Deserialize(v))
.HasColumnType("jsonb");
}
@@ -84,8 +87,7 @@ public partial class Pf2eDbContext
.HasPostgresEnum<SpellDamageKinds>()
.HasPostgresEnum<AttributeValues>()
.HasPostgresEnum<TrainingStage>()
.HasPostgresEnum<SourceCategory>()
;
.HasPostgresEnum<SourceCategory>();
}
private void DefineBase(ModelBuilder modelBuilder)
@@ -99,12 +101,16 @@ public partial class Pf2eDbContext
entity.HasMany(e => e.Keywords).WithMany(t => t.Entities);
// EntitySource
entity.HasMany(e => e.Sources).WithOne(e => e.Entity)
entity
.HasMany(e => e.Sources)
.WithOne(e => e.Entity)
.HasForeignKey(e => e.EntityId)
.OnDelete(DeleteBehavior.Cascade);
// EntityExternal
entity.HasMany(e => e.Externals).WithOne(e => e.Entity)
entity
.HasMany(e => e.Externals)
.WithOne(e => e.Entity)
.HasForeignKey(e => e.EntityId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -114,7 +120,9 @@ public partial class Pf2eDbContext
{
entity.Property(e => e.Id).HasColumnName("id");
entity.HasMany(s => s.EntitySources).WithOne(e => e.Source)
entity
.HasMany(s => s.EntitySources)
.WithOne(e => e.Source)
.HasForeignKey(e => e.SourceId)
.OnDelete(DeleteBehavior.Cascade);
@@ -125,7 +133,9 @@ public partial class Pf2eDbContext
// External
modelBuilder.Entity<ExternalTarget>(entity =>
{
entity.HasMany(t => t.EntityExternals).WithOne(e => e.Target)
entity
.HasMany(t => t.EntityExternals)
.WithOne(e => e.Target)
.HasForeignKey(e => e.TargetId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -134,7 +144,9 @@ public partial class Pf2eDbContext
// Keywords
modelBuilder.Entity<Keyword>(entity =>
{
entity.HasMany(e => e.Aliases).WithOne(e => e.Keyword)
entity
.HasMany(e => e.Aliases)
.WithOne(e => e.Keyword)
.HasForeignKey(e => e.KeywordId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -163,28 +175,38 @@ public partial class Pf2eDbContext
{
BindEntity(entity);
entity.HasMany(e => e.AncestryBoosts).WithOne(e => e.Ancestry)
entity
.HasMany(e => e.AncestryBoosts)
.WithOne(e => e.Ancestry)
.HasForeignKey(e => e.AncestryId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasMany(e => e.AncestryItems).WithOne(e => e.Ancestry)
entity
.HasMany(e => e.AncestryItems)
.WithOne(e => e.Ancestry)
.HasForeignKey(e => e.AncestryId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasMany(e => e.AncestryLanguages).WithOne(e => e.Ancestry)
entity
.HasMany(e => e.AncestryLanguages)
.WithOne(e => e.Ancestry)
.HasForeignKey(e => e.AncestryId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<AncestryBoost>(entity => { });
modelBuilder.Entity<AncestryItem>(entity =>
{
entity.HasOne(d => d.Item).WithMany()
entity
.HasOne(d => d.Item)
.WithMany()
.HasForeignKey(d => d.ItemId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<AncestryLanguage>(entity =>
{
entity.HasOne(d => d.Language).WithMany()
entity
.HasOne(d => d.Language)
.WithMany()
.HasForeignKey(d => d.LanguageId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -194,11 +216,15 @@ public partial class Pf2eDbContext
{
BindEntity(entity);
entity.HasOne(a => a.DedicationFeat).WithOne()
entity
.HasOne(a => a.DedicationFeat)
.WithOne()
.HasForeignKey<Archetype>(a => a.DedicationFeatId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasMany(a => a.Feats).WithMany(f => f.Archetypes)
entity
.HasMany(a => a.Feats)
.WithMany(f => f.Archetypes)
.UsingEntity<ArchetypeFeats>(
l => l.HasOne<Feat>().WithMany().HasForeignKey(af => af.FeatId),
r => r.HasOne<Archetype>().WithMany().HasForeignKey(af => af.ArchetypeId)
@@ -212,23 +238,31 @@ public partial class Pf2eDbContext
});
modelBuilder.Entity<BackgroundBoost>(entity =>
{
entity.HasOne(d => d.Background).WithMany(p => p.BackgroundBoosts)
entity
.HasOne(d => d.Background)
.WithMany(p => p.BackgroundBoosts)
.HasForeignKey(d => d.BackgroundId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<BackgroundItem>(entity =>
{
entity.HasOne(d => d.Background).WithMany(p => p.BackgroundItems)
entity
.HasOne(d => d.Background)
.WithMany(p => p.BackgroundItems)
.HasForeignKey(d => d.BackgroundId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(d => d.Entity).WithMany()
entity
.HasOne(d => d.Entity)
.WithMany()
.HasForeignKey(d => d.EntityId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<BackgroundTraining>(entity =>
{
entity.HasOne(d => d.Background).WithMany(p => p.BackgroundSkills)
entity
.HasOne(d => d.Background)
.WithMany(p => p.BackgroundSkills)
.HasForeignKey(d => d.BackgroundId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -240,27 +274,37 @@ public partial class Pf2eDbContext
});
modelBuilder.Entity<ClassKeyAbility>(entity =>
{
entity.HasOne(d => d.Class).WithMany(p => p.ClassKeyAbilities)
entity
.HasOne(d => d.Class)
.WithMany(p => p.ClassKeyAbilities)
.HasForeignKey(d => d.ClassId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<ClassProgression>(entity =>
{
entity.HasOne(d => d.Class).WithMany(p => p.ClassProgressions)
entity
.HasOne(d => d.Class)
.WithMany(p => p.ClassProgressions)
.HasForeignKey(d => d.ClassId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(d => d.Entity).WithMany()
entity
.HasOne(d => d.Entity)
.WithMany()
.HasForeignKey(d => d.EntityId)
.OnDelete(DeleteBehavior.SetNull);
});
modelBuilder.Entity<ClassTraining>(entity =>
{
entity.HasOne(d => d.Class).WithMany(p => p.ClassTrainings)
entity
.HasOne(d => d.Class)
.WithMany(p => p.ClassTrainings)
.HasForeignKey(d => d.ClassId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(d => d.Training).WithMany()
entity
.HasOne(d => d.Training)
.WithMany()
.HasForeignKey(d => d.TrainingId)
.OnDelete(DeleteBehavior.SetNull);
});
@@ -276,19 +320,27 @@ public partial class Pf2eDbContext
{
BindEntity(entity);
entity.HasMany(e => e.DeityAttributes).WithOne(e => e.Deity)
entity
.HasMany(e => e.DeityAttributes)
.WithOne(e => e.Deity)
.HasForeignKey(e => e.DeityId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasMany(e => e.DeityDomains).WithOne(e => e.Deity)
entity
.HasMany(e => e.DeityDomains)
.WithOne(e => e.Deity)
.HasForeignKey(e => e.DeityId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasMany(e => e.DeitySpells).WithOne(e => e.Deity)
entity
.HasMany(e => e.DeitySpells)
.WithOne(e => e.Deity)
.HasForeignKey(e => e.DeityId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasMany(e => e.DeityWeapons).WithOne(e => e.Deity)
entity
.HasMany(e => e.DeityWeapons)
.WithOne(e => e.Deity)
.HasForeignKey(e => e.DeityId)
.OnDelete(DeleteBehavior.Cascade);
@@ -298,7 +350,9 @@ public partial class Pf2eDbContext
modelBuilder.Entity<DeityDomain>(entity => { });
modelBuilder.Entity<DeitySpell>(entity =>
{
entity.HasOne(d => d.Spell).WithMany()
entity
.HasOne(d => d.Spell)
.WithMany()
.HasForeignKey(d => d.SpellId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -309,7 +363,9 @@ public partial class Pf2eDbContext
{
BindEntity(entity);
entity.HasMany(e => e.FeatPrerequisites).WithOne(e => e.Feat)
entity
.HasMany(e => e.FeatPrerequisites)
.WithOne(e => e.Feat)
.HasForeignKey(e => e.FeatId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -320,7 +376,9 @@ public partial class Pf2eDbContext
{
BindEntity(entity);
entity.HasOne(d => d.Ancestry).WithMany(p => p.Heritages)
entity
.HasOne(d => d.Ancestry)
.WithMany(p => p.Heritages)
.HasForeignKey(d => d.AncestryId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -330,21 +388,29 @@ public partial class Pf2eDbContext
{
BindEntity(entity);
entity.HasOne(e => e.SpellArea).WithOne(e => e.Spell)
entity
.HasOne(e => e.SpellArea)
.WithOne(e => e.Spell)
.HasForeignKey<SpellArea>(e => e.SpellId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired(false);
entity.HasOne(e => e.SpellRitual).WithOne(e => e.Spell)
entity
.HasOne(e => e.SpellRitual)
.WithOne(e => e.Spell)
.HasForeignKey<SpellRitual>(e => e.SpellId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired(false);
entity.HasMany(e => e.SpellDamages).WithOne(e => e.Spell)
entity
.HasMany(e => e.SpellDamages)
.WithOne(e => e.Spell)
.HasForeignKey(e => e.SpellId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasMany(e => e.SpellDefenses).WithOne(e => e.Spell)
entity
.HasMany(e => e.SpellDefenses)
.WithOne(e => e.Spell)
.HasForeignKey(e => e.SpellId)
.OnDelete(DeleteBehavior.Cascade);
});
@@ -362,27 +428,40 @@ public partial class Pf2eDbContext
private void DefineMethods(ModelBuilder modelBuilder)
{
MethodInfo localeSearch = typeof(Pf2eDbContext).GetMethod(nameof(LocaleSearch), BindingFlags.Static | BindingFlags.Public, [typeof(LocaleString), typeof(string)])!;
modelBuilder.HasDbFunction(localeSearch, builder =>
{
builder.HasName("locale_search");
builder.HasParameter("data").HasStoreType("jsonb");
builder.HasParameter("search_value");
});
MethodInfo localeSearch = typeof(Pf2eDbContext).GetMethod(
nameof(LocaleSearch),
BindingFlags.Static | BindingFlags.Public,
[typeof(LocaleString), typeof(string)]
)!;
modelBuilder.HasDbFunction(
localeSearch,
builder =>
{
builder.HasName("locale_search");
builder.HasParameter("data").HasStoreType("jsonb");
builder.HasParameter("search_value");
}
);
MethodInfo localeSearchArray = typeof(Pf2eDbContext).GetMethod(nameof(LocaleSearchArray), BindingFlags.Static | BindingFlags.Public, [typeof(LocaleString), typeof(IEnumerable<string>)])!;
modelBuilder.HasDbFunction(localeSearchArray, builder =>
{
builder.HasName("locale_search_array");
builder.HasParameter("data").HasStoreType("jsonb");
builder.HasParameter("search_values");
});
MethodInfo localeSearchArray = typeof(Pf2eDbContext).GetMethod(
nameof(LocaleSearchArray),
BindingFlags.Static | BindingFlags.Public,
[typeof(LocaleString), typeof(IEnumerable<string>)]
)!;
modelBuilder.HasDbFunction(
localeSearchArray,
builder =>
{
builder.HasName("locale_search_array");
builder.HasParameter("data").HasStoreType("jsonb");
builder.HasParameter("search_values");
}
);
}
public static bool LocaleSearch(LocaleString data, string search_value)
=> throw new InvalidOperationException("Only for LINQ translation");
public static bool LocaleSearchArray(LocaleString data, IEnumerable<string> search_values)
=> throw new InvalidOperationException("Only for LINQ translation");
public static bool LocaleSearch(LocaleString data, string search_value) =>
throw new InvalidOperationException("Only for LINQ translation");
public static bool LocaleSearchArray(LocaleString data, IEnumerable<string> search_values) =>
throw new InvalidOperationException("Only for LINQ translation");
}