using Microsoft.Extensions.Options; using Schaad.OpenData.Weather; using Schaad.OpenData.Weather.Analysis; using Schaad.OpenData.Weather.Storage; namespace Schaad.OpenData.Server.Weather; public static class WeatherEndpoints { private const int DefaultHistoryDays = 7; public static IEndpointRouteBuilder MapWeatherEndpoints(this IEndpointRouteBuilder endpoints) { var group = endpoints.MapGroup("/api/weather"); group.MapGet("/meta", (WeatherDataStore store, IOptions options) => new WeatherMetaResponse( "Automatische Wetterstationen – Messwerte (SwissMetNet)", "MeteoSchweiz", "https://opendata.swiss/de/dataset/automatische-wetterstationen-messwerte", "CC BY 4.0", options.Value.HistoryDays, store.Current.LastRefresh)); group.MapGet("/stations", (WeatherDataStore store) => WeatherAnalysis.BuildStations(store.Current)); group.MapGet("/stations/{abbr}/history", ( string abbr, int? tage, WeatherDataStore store, IOptions options, TimeProvider timeProvider) => { var snapshot = store.Current; var key = abbr.ToUpperInvariant(); if (snapshot.Stations.All(s => s.Abbr != key)) { return Results.NotFound(); } var days = Math.Clamp(tage ?? DefaultHistoryDays, 1, options.Value.HistoryDays); return Results.Ok(WeatherAnalysis.BuildHistory(snapshot, key, timeProvider.GetUtcNow().AddDays(-days))); }); return endpoints; } }