Глобальный рефакторинг пространств имён
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2e.Scrapper.Processors;
|
||||
using Pf2e.Scrapper.Processors.Attributes;
|
||||
using Pf2e.Scrapper.Processors.Definitions;
|
||||
using Pf2e.Database;
|
||||
using Pf2e.Database.Model.Base;
|
||||
using Pf2e.Database.Model.Base.Targets;
|
||||
|
||||
namespace Pf2e.Scrapper.Importers.Implementations.Aonprd.Processors.Finalization;
|
||||
|
||||
[ProcessorCollection(
|
||||
"aonprd:legacy-remaster",
|
||||
InnerType = null,
|
||||
CompendiumType = ProcessorCompendiumEnum.Aonprd,
|
||||
Description = "Связь устаревших и обновлённых сущностей",
|
||||
Group = ProcessorGroupEnum.AonprdFinalization,
|
||||
Parallel = true
|
||||
)]
|
||||
internal class LegacyRemasterProcessors(ProcessorInput input) : AbstractInputProcessor(input)
|
||||
{
|
||||
private Pf2eDbContext Context { get; } = new();
|
||||
|
||||
public override void Process()
|
||||
{
|
||||
string aonprdId = Input.GetRequired<string>("id");
|
||||
EntityExternal entity =
|
||||
Context.EntityExternals.AsNoTracking().FirstOrDefault(x => x.ExternalId == aonprdId)
|
||||
?? throw new SilentSkipItemException();
|
||||
int entityId = entity.EntityId;
|
||||
Entity legacyEntity =
|
||||
Context.Entities.FirstOrDefault(e => e.Id == entityId)
|
||||
?? throw new($"Неизвестная сущность с ID {entityId}");
|
||||
|
||||
legacyEntity.IsLegacy = true;
|
||||
|
||||
foreach (int remasterId in GetEntityIds("remaster_id"))
|
||||
{
|
||||
Context.LegacyRemasters.Add(new() { LegacyId = entityId, RemasterId = remasterId });
|
||||
Entity remasterEntity =
|
||||
Context.Entities.AsNoTracking().FirstOrDefault(e => e.Id == remasterId)
|
||||
?? throw new($"Неизвестная сущность с ID {remasterId}");
|
||||
|
||||
if (legacyEntity.Slug == remasterEntity.Slug)
|
||||
{
|
||||
legacyEntity.Slug += "-legacy";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected List<int> GetEntityIds(string path)
|
||||
{
|
||||
List<string> aonprdIds = (Input.GetOptional<JArray>(path) ?? [])
|
||||
.Select(token => token.ToString())
|
||||
.ToList();
|
||||
|
||||
return Context
|
||||
.EntityExternals.AsNoTracking()
|
||||
.Where(x => aonprdIds.Contains(x.ExternalId!))
|
||||
.Select(x => x.EntityId)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public override void Save()
|
||||
{
|
||||
Context.SaveChanges();
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Pf2e.Scrapper.Importers.Implementations.Aonprd.Helpers;
|
||||
using Pf2e.Scrapper.Processors;
|
||||
using Pf2e.Scrapper.Processors.Attributes;
|
||||
using Pf2e.Scrapper.Processors.Definitions;
|
||||
using Pf2e.Scrapper.Processors.Factory;
|
||||
using Pf2e.Database;
|
||||
using Pf2e.Database.Model.Base;
|
||||
using Pf2e.Database.Model.Base.Targets;
|
||||
|
||||
namespace Pf2e.Scrapper.Importers.Implementations.Aonprd.Processors.Finalization;
|
||||
|
||||
[ProcessorFactory(
|
||||
"aonprd:fix-legacy-remaster",
|
||||
ProcessorType = typeof(MissingLegacyRemasterProcessor),
|
||||
Description = "Дополнение связяй устаревших и обновлённых сущностей",
|
||||
Group = ProcessorGroupEnum.AonprdFinalization,
|
||||
Parallel = true
|
||||
)]
|
||||
[ProcessorDependencies(["aonprd:legacy-remaster"])]
|
||||
internal class MissingLegacyRemasterProcessorFactory(
|
||||
IServiceProvider serviceProvider,
|
||||
AonprdBuilderGlobals globals
|
||||
) : AbstractProcessorFactory
|
||||
{
|
||||
public IServiceProvider ServiceProvider { get; } = serviceProvider;
|
||||
public AonprdBuilderGlobals Globals { get; } = globals;
|
||||
|
||||
public override int Count() => Globals.LegacyRemasterExtra.Count;
|
||||
|
||||
public override IEnumerable<Tuple<string, IProcessor>> GetEnumerator()
|
||||
{
|
||||
foreach ((string key, string value) in Globals.LegacyRemasterExtra)
|
||||
{
|
||||
yield return new(
|
||||
$"{key} -> {value}",
|
||||
ActivatorUtilities.CreateInstance<MissingLegacyRemasterProcessor>(
|
||||
ServiceProvider,
|
||||
key,
|
||||
value
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class MissingLegacyRemasterProcessor(string legacyId, string remasterId) : IProcessor
|
||||
{
|
||||
public Pf2eDbContext Context { get; } = new();
|
||||
public string LegacyId { get; } = legacyId;
|
||||
public string RemasterId { get; } = remasterId;
|
||||
|
||||
private Entity GetEntity(string aonprdId)
|
||||
{
|
||||
EntityExternal entity =
|
||||
Context.EntityExternals.AsNoTracking().FirstOrDefault(x => x.ExternalId == aonprdId)
|
||||
?? throw new SilentSkipItemException();
|
||||
|
||||
int entityId = entity.EntityId;
|
||||
|
||||
return Context.Entities.FirstOrDefault(e => e.Id == entityId)
|
||||
?? throw new($"Неизвестная сущность с ID {entityId}");
|
||||
}
|
||||
|
||||
public void Process()
|
||||
{
|
||||
Entity legacyEntity = GetEntity(LegacyId);
|
||||
Entity remasterEntity = GetEntity(RemasterId);
|
||||
|
||||
TestExist(legacyEntity, remasterEntity);
|
||||
|
||||
Context.LegacyRemasters.Add(
|
||||
new() { LegacyId = legacyEntity.Id, RemasterId = remasterEntity.Id }
|
||||
);
|
||||
|
||||
legacyEntity.IsLegacy = true;
|
||||
|
||||
if (legacyEntity.Slug == remasterEntity.Slug)
|
||||
{
|
||||
legacyEntity.Slug += "-legacy";
|
||||
}
|
||||
}
|
||||
|
||||
public void TestExist(Entity legacyEntity, Entity remasterEntity)
|
||||
{
|
||||
if (
|
||||
Context
|
||||
.LegacyRemasters.AsNoTracking()
|
||||
.Any(x => x.LegacyId == legacyEntity.Id && x.RemasterId == remasterEntity.Id)
|
||||
)
|
||||
{
|
||||
throw new SilentSkipItemException();
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Context.SaveChanges();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user