Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System.Globalization;
|
|
using Schaad.OpenData.Watercraft.Models;
|
|
|
|
namespace Schaad.OpenData.Watercraft.Source;
|
|
|
|
public static class ThurgauVesselMapper
|
|
{
|
|
private const string UnknownLocation = "Unbekannt";
|
|
|
|
public static VesselYear Map(VesselDataset dataset, IEnumerable<ThurgauVesselRecord> records)
|
|
{
|
|
var list = records.ToList();
|
|
var referenceDate = list
|
|
.Select(r => ParseDate(r.ReferenceDate))
|
|
.Where(d => d is not null)
|
|
.Max();
|
|
|
|
var vessels = list.Select(r => new Vessel(
|
|
Clean(r.Location) ?? UnknownLocation,
|
|
Clean(r.Type),
|
|
Clean(r.SpecialUse),
|
|
r.LengthCm,
|
|
r.WidthCm,
|
|
r.SailAreaM2,
|
|
Clean(r.Propulsion),
|
|
r.EngineCount is { } count ? (int)Math.Round(count) : null,
|
|
r.PowerKw,
|
|
Clean(r.Fuel),
|
|
Clean(r.EmissionGroup)))
|
|
.ToList();
|
|
|
|
return new VesselYear(dataset.Year, dataset.DatasetId, dataset.Modified, referenceDate, vessels);
|
|
}
|
|
|
|
private static string? Clean(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
|
|
|
|
private static DateOnly? ParseDate(string? value) =>
|
|
value is { Length: >= 10 }
|
|
&& DateOnly.TryParseExact(value[..10], "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date)
|
|
? date
|
|
: null;
|
|
}
|