Пагинация запросов и исправление отображений
This commit is contained in:
@@ -4,7 +4,9 @@ using Newtonsoft.Json.Linq;
|
||||
using Pf2eModel;
|
||||
using Pf2eModel.Model;
|
||||
using Pf2eModel.Model.Base;
|
||||
using Pf2eModel.Model.Base.Source;
|
||||
using Pf2eModel.Model.Structure;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Pf2eViewer;
|
||||
internal class EntityConverter : JsonConverter
|
||||
@@ -16,44 +18,62 @@ internal class EntityConverter : JsonConverter
|
||||
return objectType == typeof(Entity) || objectType.IsAssignableTo(typeof(IEntity));
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
private Dictionary<string, object?> DictionarySerialization(object source)
|
||||
{
|
||||
JToken token = JToken.FromObject(value!);
|
||||
Dictionary<string, object?> dictionary = [];
|
||||
|
||||
if (token.Type != JTokenType.Object)
|
||||
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
|
||||
{
|
||||
token.WriteTo(writer);
|
||||
return;
|
||||
object? v = property.GetValue(source);
|
||||
dictionary.Add(property.Name, v);
|
||||
}
|
||||
|
||||
JObject obj = (JObject) token;
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
Dictionary<string, object?> dictionary = DictionarySerialization(value!);
|
||||
|
||||
if (value.GetType() != typeof(Entity))
|
||||
{
|
||||
dictionary.Remove("Entity");
|
||||
}
|
||||
|
||||
switch (value)
|
||||
{
|
||||
case Entity e:
|
||||
obj = ProcessEntity(obj, e);
|
||||
case Entity entity:
|
||||
ProcessEntity(dictionary, entity);
|
||||
break;
|
||||
|
||||
case Trait trait:
|
||||
ProcessTrait(dictionary, trait);
|
||||
break;
|
||||
}
|
||||
|
||||
obj.WriteTo(writer);
|
||||
}
|
||||
writer.WriteStartObject();
|
||||
|
||||
private JObject ProcessEntity(JObject obj, Entity e)
|
||||
{
|
||||
List<int> traitIds = e.Traits.Select(t => t.Id).ToList();
|
||||
List<Trait> traits = _context.Traits.Where(t => traitIds.Contains(t.Id)).Include(t => t.Entity).ToList();
|
||||
|
||||
foreach (JToken item in (JArray) obj.SelectToken("Traits")!)
|
||||
foreach (KeyValuePair<string, object> keyValuePair in dictionary)
|
||||
{
|
||||
JObject o = (JObject) item;
|
||||
int id = o.SelectToken("Id")!.ToObject<int>();
|
||||
|
||||
o.Add("Name", traits.FirstOrDefault(t => t.Id == id)!.Entity.Name);
|
||||
writer.WritePropertyName(keyValuePair.Key, true);
|
||||
serializer.Serialize(writer, keyValuePair.Value);
|
||||
}
|
||||
|
||||
obj.Remove("Description");
|
||||
|
||||
return obj;
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
private void ProcessTrait(Dictionary<string, object?> dictionary, Trait trait)
|
||||
{
|
||||
string name = trait.Entity?.Name ?? _context.Entities.Where(e => e.Id == trait.Id).Select(e => e.Name).FirstOrDefault()!;
|
||||
|
||||
dictionary.Remove("Entity");
|
||||
dictionary.Remove("Entities");
|
||||
dictionary.Add("Name", name);
|
||||
}
|
||||
|
||||
private void ProcessEntity(Dictionary<string, object?> dictionary, Entity entity)
|
||||
{
|
||||
dictionary.Remove("Description");
|
||||
}
|
||||
|
||||
public override bool CanRead => false;
|
||||
|
||||
Reference in New Issue
Block a user