1
0
mirror of synced 2024-12-18 09:15:54 +01:00
GC-local-server-rewrite/WebUI/Services/DataService.cs

85 lines
2.4 KiB
C#
Raw Normal View History

2023-02-17 18:29:20 +01:00
using System.Collections.ObjectModel;
using System.Net.Http.Json;
2023-02-21 15:45:51 +01:00
using System.Text.Json.Serialization;
2023-02-17 18:29:20 +01:00
using Throw;
using WebUI.Common.Models;
namespace WebUI.Services;
public class DataService : IDataService
{
private Dictionary<uint, Avatar> avatars = new();
private Dictionary<uint, Navigator> navigators = new();
private Dictionary<uint, Title> titles = new();
private List<Avatar> sortedAvatarList = new();
private List<Navigator> sortedNavigatorList = new();
private List<Title> sortedTitleList = new();
2023-02-17 18:29:20 +01:00
private readonly HttpClient client;
public DataService(HttpClient client)
{
this.client = client;
}
public async Task InitializeAsync()
{
var avatarList = await client.GetFromJsonAsync("data/Avatars.json", SourceGenerationContext.Default.ListAvatar);
avatarList.ThrowIfNull();
avatars = avatarList.ToDictionary(avatar => avatar.AvatarId);
sortedAvatarList = avatarList.OrderBy(avatar => avatar.AvatarId).ToList();
2023-02-17 18:29:20 +01:00
var navigatorList = await client.GetFromJsonAsync("data/Navigators.json", SourceGenerationContext.Default.ListNavigator);
navigatorList.ThrowIfNull();
navigators = navigatorList.ToDictionary(navigator => navigator.Id);
sortedNavigatorList = navigatorList.OrderBy(navigator => navigator.Id).ToList();
2023-02-17 18:29:20 +01:00
var titleList = await client.GetFromJsonAsync("data/Titles.json", SourceGenerationContext.Default.ListTitle);
titleList.ThrowIfNull();
titles = titleList.ToDictionary(title => title.Id);
sortedTitleList = titleList.OrderBy(title => title.Id).ToList();
}
public IReadOnlyList<Avatar> GetAvatarsSortedById()
{
return sortedAvatarList;
}
public IReadOnlyList<Navigator> GetNavigatorsSortedById()
{
return sortedNavigatorList;
}
public IReadOnlyList<Title> GetTitlesSortedById()
{
return sortedTitleList;
2023-02-17 18:29:20 +01:00
}
public Avatar? GetAvatarById(uint id)
2023-02-17 18:29:20 +01:00
{
return avatars.GetValueOrDefault(id);
2023-02-17 18:29:20 +01:00
}
public Title? GetTitleById(uint id)
2023-02-17 18:29:20 +01:00
{
return titles.GetValueOrDefault(id);
2023-02-17 18:29:20 +01:00
}
public Navigator? GetNavigatorById(uint id)
2023-02-17 18:29:20 +01:00
{
return navigators.GetValueOrDefault(id);
2023-02-17 18:29:20 +01:00
}
2023-02-21 15:45:51 +01:00
}
[JsonSerializable(typeof(List<Avatar>))]
[JsonSerializable(typeof(List<Navigator>))]
[JsonSerializable(typeof(List<Title>))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
2023-02-17 18:29:20 +01:00
}