NahidaImpact/NahidaImpact.Common/Data/Excel/ExcelTable.cs
2024-01-04 16:48:39 +03:00

46 lines
1.3 KiB
C#

using System.Collections.Immutable;
using System.Text.Json;
namespace NahidaImpact.Common.Data.Excel;
public class ExcelTable
{
public int Count => _items.Length;
private readonly ImmutableArray<ExcelItem> _items;
public ExcelTable(JsonDocument document, Type type)
{
_items = LoadData(document, type);
}
private static ImmutableArray<ExcelItem> LoadData(JsonDocument document, Type type)
{
ImmutableArray<ExcelItem>.Builder items = ImmutableArray.CreateBuilder<ExcelItem>();
foreach (JsonElement element in document.RootElement.EnumerateArray())
{
if (element.ValueKind != JsonValueKind.Object)
throw new ArgumentException($"ExcelTable::LoadData - expected an object, got {element.ValueKind}");
ExcelItem deserialized = (element.Deserialize(type) as ExcelItem)!;
items.Add(deserialized);
}
return items.ToImmutable();
}
public TExcel GetItemAt<TExcel>(int index) where TExcel : ExcelItem
{
return (_items[index] as TExcel)!;
}
public TExcel? GetItemById<TExcel>(uint id) where TExcel : ExcelItem
{
foreach (ExcelItem item in _items)
{
if (item.ExcelId == id)
return item as TExcel;
}
return null;
}
}