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

74 lines
2.1 KiB
C#
Raw Normal View History

 using Throw;
2023-02-17 18:29:20 +01:00
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<List<Avatar>>("data/Avatars.json");
2023-02-17 18:29:20 +01:00
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<List<Navigator>>("data/Navigators.json");
2023-02-17 18:29:20 +01:00
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<List<Title>>("data/Titles.json");
2023-02-17 18:29:20 +01:00
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
}
}