using System.Globalization; using Schaad.OpenData.Vehicles.Models; namespace Schaad.OpenData.Vehicles.Source; public static class ThurgauVehicleMapper { public const string OtherBrand = "Andere"; public const string Unknown = "Unbekannt"; public static IReadOnlyList MapStock(IEnumerable records) => records .Select(r => (Record: r, Year: ParseYear(r.Year), Id: Clean(r.MunicipalityId))) .Where(r => r.Year is not null && r.Id is not null && r.Record.Count > 0) .Select(r => new StockEntry( r.Year!.Value, r.Id!, Clean(r.Record.Municipality) ?? r.Id!, Clean(r.Record.District) ?? Unknown, Clean(r.Record.Group) ?? Unknown, Clean(r.Record.Type) ?? Unknown, Clean(r.Record.Brand) ?? OtherBrand, (int)Math.Round(r.Record.Count!.Value))) .ToList(); public static IReadOnlyList MapPopulation(IEnumerable records) => records .Select(r => (Year: ParseYear(r.Year), Id: Clean(r.MunicipalityId), Population: ParseInt(r.Population))) .Where(r => r.Year is not null && r.Id is not null && r.Population > 0) .Select(r => new PopulationEntry(r.Year!.Value, r.Id!, r.Population!.Value)) .ToList(); private static int? ParseYear(string? value) => value is { Length: >= 4 } && int.TryParse(value.AsSpan(0, 4), NumberStyles.None, CultureInfo.InvariantCulture, out var year) ? year : null; private static int? ParseInt(string? value) => double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var number) ? (int)Math.Round(number) : null; private static string? Clean(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim(); }