using DbController; using DbController.SqlServer; using Microsoft.Extensions.Configuration; using System.Globalization; using Tabletop.Core.Interfaces; using Tabletop.Core.Models; namespace Tabletop.Core.Services { public static class AppdataService { public static string[] SupportedCultureCodes => [.. SupportedCultures.Select(x => x.Name)]; public static CultureInfo[] SupportedCultures => [ new ("en"), new ("de") ]; public static bool FirstUserExists { get; set; } = false; public static List Permissions { get; set; } = []; public static List Units { get; set; } = []; public static List Weapons { get; set; } = []; public static List Fractions { get; set; } = []; public static List Gamemodes { get; set; } = []; public static List Classes { get; set; } = []; public static List Abilities { get; set; } = []; public static List Layouts { get; set; } = []; public static List Surfaces { get; set; } = []; public static readonly Dictionary Games = []; private static IConfiguration? _configuration; public static async Task InitAsync(IConfiguration configuration) { _configuration = configuration; using IDbController dbController = new SqlController(ConnectionString); Permissions = await PermissionService.GetAllAsync(dbController); FirstUserExists = await UserService.FirstUserExistsAsync(dbController); Units = await UnitService.GetAllAsync(dbController); Weapons = await WeaponService.GetAllAsync(dbController); Fractions = await FractionService.GetAllAsync(dbController); Gamemodes = await GamemodeService.GetAllAsync(dbController); Classes = await ClassService.GetAllAsync(dbController); Abilities = await AbilityService.GetAllAsync(dbController); Layouts = await LayoutService.GetAllAsync(dbController); Surfaces = await SurfaceService.GetAllAsync(dbController); } /// /// Creates or updates an object in the corresponding list fro the type /// /// /// public static void UpdateRecord(T input) where T : class, IDbModel, new() { List list = GetList(); T? item = list.FirstOrDefault(x => x.GetIdentifier() == input.GetIdentifier()); if (item is null) { list.Add(input); } else { int index = list.IndexOf(item); list[index] = input; } } /// /// Deletes an item from the corresponding object list. /// /// /// public static void DeleteRecord(T input) where T : class, IDbModel, new() { List list = GetList(); list.Remove(input); } /// /// Gets the corresponding list for the type /// /// /// This method never returns null. When no list for is specified, it returns a new empty list public static List GetList() where T : class, IDbModel, new() { var input = new T(); object tmp = input switch { Permission => Permissions, _ => new List() }; List list = (List)tmp; return list; } public static string ConnectionString => _configuration?["Sql:ConnectionString"] ?? string.Empty; public static int PageLimit => _configuration?.GetValue("Filter:PageLimit") ?? 30; public static string ApiKey => _configuration?["IpGeolocation:ApiKey"] ?? string.Empty; public static CultureInfo ToCulture(this ILocalizationHelper helper) { var culture = SupportedCultures.FirstOrDefault(x => x.TwoLetterISOLanguageName.Equals(helper.Code, StringComparison.OrdinalIgnoreCase)); if (culture is null) { return SupportedCultures[0]; } else { return culture; } } } }