Skip to content

Commit

Permalink
Read materia into POCO directly (#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
karashiiro authored Jul 21, 2024
1 parent 733df89 commit 86738ae
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions src/Universalis.DbAccess/MarketBoard/ListingStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ ORDER BY unit_price
WorldId = reader.GetInt32(2),
Hq = reader.GetBoolean(3),
OnMannequin = reader.GetBoolean(4),
Materia = ReadMateriaFromReader(reader, query.WorldId, query.ItemId),
Materia = ReadMateriaFromReader(reader),
PricePerUnit = reader.GetInt32(6),
Quantity = reader.GetInt32(7),
DyeId = reader.GetInt32(8),
Expand Down Expand Up @@ -305,7 +305,7 @@ FROM listing t
WorldId = worldId,
Hq = reader.GetBoolean(3),
OnMannequin = reader.GetBoolean(4),
Materia = ReadMateriaFromReader(reader, worldId, itemId),
Materia = ReadMateriaFromReader(reader),
PricePerUnit = reader.GetInt32(6),
Quantity = reader.GetInt32(7),
DyeId = reader.GetInt32(8),
Expand Down Expand Up @@ -449,30 +449,10 @@ private static JArray ConvertMateriaToJArray(IList<Materia> materia)
});
}

private List<Materia> ReadMateriaFromReader(NpgsqlDataReader reader, int worldId, int itemId)
private static List<Materia> ReadMateriaFromReader(NpgsqlDataReader reader)
{
return reader.IsDBNull(5)
? new List<Materia>()
: ConvertMateriaFromJArray(reader.GetFieldValue<JArray>(5), worldId, itemId);
}

private List<Materia> ConvertMateriaFromJArray(IEnumerable<JToken> materia, int worldId, int itemId)
{
return materia
.Select((m, i) =>
{
try
{
return new Materia { SlotId = m["slot_id"].Value<int>(), MateriaId = m["materia_id"].Value<int>() };
}
catch (ArgumentException e)
{
// Unsure why this happens, possibly inserted bad data at some point while debugging
_logger.LogError(e, "Failed to parse materia for world={}, item={}", worldId, itemId);
return null;
}
})
.Where(m => m is not null)
.ToList();
: reader.GetFieldValue<List<Materia>>(5);
}
}

0 comments on commit 86738ae

Please sign in to comment.