Отказ от прямой работы с БД в обработчиках и переход на EF

This commit is contained in:
2025-04-14 19:08:27 +04:00
parent 8277294319
commit 80b5770036
3 changed files with 49 additions and 50 deletions
@@ -1,24 +0,0 @@
using Newtonsoft.Json.Linq;
using Slugify;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
{
internal abstract class AbstractEntityProcessor(JObject input) : AbstractProcessor(input)
{
public abstract string DbType { get; }
public override void Process()
{
Add("name", "name");
Add("img", "img");
Add("description", "system.description.value");
Add("description_gm", "system.description.gm");
Add("type", DbType);
Add("rarity", "system.traits.rarity");
SlugHelper sh = new();
Data.Add("slug", sh.GenerateSlug(Data["name"]?.ToString() ?? throw new NullReferenceException("'name' is null")));
}
}
}
@@ -1,30 +1,43 @@
using Newtonsoft.Json.Linq;
using LinqToDB.DataProvider.SapHana;
using Newtonsoft.Json.Linq;
using Npgsql;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder
{
internal abstract class AbstractProcessor(JObject input)
{
public readonly Pf2eTestContext Context = new();
public readonly JObject Input = input;
public Pf2eDatabase Database = Pf2eDatabase.Instance;
public DataBuilderHelper Helper = DataBuilderHelper.Instance;
public abstract string Table { get; }
public Dictionary<string, object?> Data = [];
protected void Add(string key, string path)
{
Data.Add(key, DataBuilderHelper.JsonGet(Input, path));
}
public abstract void Process();
public int Save()
public void Save()
{
return Helper.Insert(Table, Data);
}
Context.SaveChanges();
}
protected T GetRequired<T>(string path)
{
var token = Input.SelectToken(path) ?? throw new NullReferenceException($"'{path}' is null");
return token.ToObject<T>() ?? throw new NullReferenceException($"'{path}' is null");
}
protected T? GetOptional<T>(string path)
{
var token = Input.SelectToken(path) ?? null;
if (token is null)
{
return default!;
}
return token.ToObject<T>() ?? default;
}
}
}
@@ -1,20 +1,15 @@
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.Model;
using Slugify;
using static LinqToDB.Common.Configuration;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder.Processors
{
[Pf2eTypeProcessor("Item.condition")]
internal class ConditionProcessor(JObject input) : AbstractEntityProcessor(input)
internal class ConditionProcessor(JObject input) : AbstractProcessor(input)
{
public override string Table => "condition";
public override string DbType => "condition";
public override void Process()
{
base.Process();
Add("group", "system.group");
JObject extra = new()
{
{ "duration", Input.SelectToken("duration") },
@@ -23,7 +18,22 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.DataBuilder.Pro
{ "value", Input.SelectToken("value") }
};
Data.Add("extra", extra.ToString(Newtonsoft.Json.Formatting.Indented));
Condition condition = new()
{
Name = GetRequired<string>("name"),
Img = GetRequired<string>("img"),
Description = GetRequired<string>("system.description.value"),
DescriptionGm = GetOptional<string>("system.description.gm"),
// Type =
IsTranslated = false,
IsChecked = false,
// Group = GetOptional<string>("system.group");
Extra = extra.ToString(Newtonsoft.Json.Formatting.Indented)
};
condition.Slug = new SlugHelper().GenerateSlug(condition.Name);
Context.Conditions.Add(condition);
}
}
}