105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Pf2eModel;
|
|
using Pf2eModel.Model;
|
|
using Pf2eModel.Model.Base;
|
|
using Pf2eModel.Model.Enum;
|
|
using Pf2eModel.Model.Structure;
|
|
|
|
namespace Pf2eViewer;
|
|
|
|
public class EntityView
|
|
{
|
|
public Entity? Entity;
|
|
public IEntity? SubEntity;
|
|
|
|
public EntityView(Pf2eDbContext context, EntityType type, int id)
|
|
{
|
|
Getter? getter = _getters.FirstOrDefault(g => g.Type == type);
|
|
|
|
Entity fullEntity =
|
|
context
|
|
.Entities.Where(e => e.Id == id && e.Type == type)
|
|
.Include(e => e.Sources)
|
|
.ThenInclude(s => s.Source)
|
|
.Include(e => e.Keywords)
|
|
.Include(e => e.Externals)
|
|
.ThenInclude(x => x.Target)
|
|
.Include(e => e.Traits)
|
|
.ThenInclude(t => t.Entity)
|
|
.FirstOrDefault() ?? throw new("Entity is null");
|
|
|
|
IEntity? subEntity =
|
|
(getter != null)
|
|
? getter.Get(id, context)
|
|
: (IEntity?)context.Find(EntityOfTypeAttribute.Resolve(fullEntity), id);
|
|
|
|
Entity = fullEntity;
|
|
SubEntity = subEntity;
|
|
}
|
|
|
|
private readonly ICollection<Getter> _getters =
|
|
[
|
|
new Getter<Ancestry>(
|
|
EntityType.Ancestry,
|
|
context => context.Ancestries.Include(a => a.AncestryBoosts)
|
|
),
|
|
new Getter<Archetype>(
|
|
EntityType.Archetype,
|
|
context =>
|
|
context
|
|
.Archetypes.Include(a => a.DedicationFeat)
|
|
.ThenInclude(f => f.Entity)
|
|
.Include(a => a.Feats)
|
|
.ThenInclude(f => f.Entity)
|
|
),
|
|
new Getter<Background>(
|
|
EntityType.Background,
|
|
context =>
|
|
context
|
|
.Backgrounds.Include(b => b.BackgroundBoosts)
|
|
.Include(b => b.BackgroundSkills)
|
|
.ThenInclude(t => t.Training)
|
|
.Include(b => b.BackgroundItems)
|
|
),
|
|
new Getter<Class>(
|
|
EntityType.Class,
|
|
context =>
|
|
context
|
|
.Classes.Include(c => c.ClassKeyAbilities)
|
|
.Include(c => c.ClassTrainings)
|
|
.ThenInclude(t => t.Training)
|
|
.Include(c => c.ClassProgressions)
|
|
),
|
|
new Getter<Feat>(
|
|
EntityType.Feat,
|
|
context =>
|
|
context.Feats.Include(f => f.Archetypes).ThenInclude(a => a.Entity).AsQueryable()
|
|
),
|
|
new Getter<Spell>(
|
|
EntityType.Spell,
|
|
context => context.Spells.Include(s => s.SpellAreas).Include(s => s.SpellDefenses)
|
|
),
|
|
new Getter<Ritual>(
|
|
EntityType.Ritual,
|
|
context => context.Rituals.Include(r => r.Area).Include(r => r.Checks)
|
|
),
|
|
];
|
|
|
|
private abstract class Getter(EntityType type)
|
|
{
|
|
public readonly EntityType Type = type;
|
|
|
|
public abstract IEntity? Get(int id, Pf2eDbContext context);
|
|
}
|
|
|
|
private class Getter<T>(EntityType type, Func<Pf2eDbContext, IQueryable<T>> method)
|
|
: Getter(type)
|
|
where T : IEntity
|
|
{
|
|
public override IEntity? Get(int id, Pf2eDbContext context)
|
|
{
|
|
return method.Invoke(context).FirstOrDefault(c => c.Id == id);
|
|
}
|
|
}
|
|
}
|