Большая пачка изменений
This commit is contained in:
@@ -29,7 +29,7 @@ public class DatabaseHelper
|
||||
/// </summary>
|
||||
public int NextEntityId => ++_id;
|
||||
|
||||
private DatabaseHelper(Pf2eDbContext context, SlugHelper slugHelper)
|
||||
public DatabaseHelper(Pf2eDbContext context, SlugHelper slugHelper)
|
||||
{
|
||||
Context = context;
|
||||
Slugger = slugHelper;
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Pf2eModel;
|
||||
using Pf2eModel.Model;
|
||||
using Pf2eModel.Model.Base;
|
||||
using Pf2eModel.Model.Enum;
|
||||
using Pf2eModel.Model.Structure;
|
||||
|
||||
namespace Pf2eViewer;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, обрабатывающий запросы к сущностям и подключающий необходимые данные.
|
||||
/// </summary>
|
||||
public class EntityView
|
||||
{
|
||||
/// <summary>
|
||||
/// Корневая сущность.
|
||||
/// </summary>
|
||||
public Entity? Entity { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Основная сущность.
|
||||
/// </summary>
|
||||
public IEntity? SubEntity { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор класса, который получает сущность из базы данных и заполняет её данными.
|
||||
/// </summary>
|
||||
/// <param name="context">Контекст базы данных.</param>
|
||||
/// <param name="type">Тип запрашиваемой сущности.</param>
|
||||
/// <param name="id">Идентификатор запрашиваемой сущности.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Словарь, содержащий методы получения сущностей из базы данных.
|
||||
/// </summary>
|
||||
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)
|
||||
.ThenInclude(c => c.Training)
|
||||
.AsQueryable()
|
||||
),
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Абстрактный класс, который определяет метод получения сущности из базы данных.
|
||||
/// </summary>
|
||||
/// <param name="type">Связанный тип сущности.</param>
|
||||
private abstract class Getter(EntityType type)
|
||||
{
|
||||
public readonly EntityType Type = type;
|
||||
|
||||
public abstract IEntity? Get(int id, Pf2eDbContext context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Класс, определяющий метод получения сущности из базы данных.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Тип данных основной сущности.</typeparam>
|
||||
/// <param name="type">Связанный тип сущности.</param>
|
||||
/// <param name="method">Метод получения сущности из базы данных.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace Pf2eDataScrapper.Helpers;
|
||||
|
||||
internal abstract class AbstractLogger
|
||||
{
|
||||
public abstract void Message(LogLevel level, string message);
|
||||
|
||||
public void Important(string message) => Message(LogLevel.Important, message);
|
||||
|
||||
public void Warning(string message) => Message(LogLevel.Warning, message);
|
||||
|
||||
public void Information(string message) => Message(LogLevel.Information, message);
|
||||
|
||||
public void Progress(string message) => Message(LogLevel.Progress, message);
|
||||
}
|
||||
|
||||
internal enum LogLevel
|
||||
{
|
||||
Important,
|
||||
Warning,
|
||||
Information,
|
||||
Progress,
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Pf2eDataScrapper.Helpers;
|
||||
|
||||
internal class ProcessProgress
|
||||
internal class ProcessProgress : AbstractLogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Прогресс обработки обработчиков <see cref="IProcessor"/>.
|
||||
@@ -31,15 +31,19 @@ internal class ProcessProgress
|
||||
ProgressUpdate?.Invoke(this);
|
||||
}
|
||||
|
||||
public void Message(string message)
|
||||
public override void Message(LogLevel level, string message)
|
||||
{
|
||||
ProgressMessage?.Invoke(this, message);
|
||||
ProgressMessage?.Invoke(this, level, message);
|
||||
}
|
||||
|
||||
public delegate void ProgressUpdateDelegate(ProcessProgress sender);
|
||||
public event ProgressUpdateDelegate? ProgressUpdate;
|
||||
|
||||
public delegate void ProgressMessageDelegate(ProcessProgress sender, string message);
|
||||
public delegate void ProgressMessageDelegate(
|
||||
ProcessProgress sender,
|
||||
LogLevel level,
|
||||
string message
|
||||
);
|
||||
public event ProgressMessageDelegate? ProgressMessage;
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user