diff --git a/UlstuGosApi/Program.cs b/UlstuGosApi/Program.cs index 4c58b31..62e88e4 100644 --- a/UlstuGosApi/Program.cs +++ b/UlstuGosApi/Program.cs @@ -37,4 +37,8 @@ app.MapGroup("/timetable") .AddTimeTable() .WithTags("TimeTable"); +app.MapGroup("/weather") + .AddWeather() + .WithTags("Weather"); + app.Run(); diff --git a/UlstuGosApi/WeatherRouteGroup.cs b/UlstuGosApi/WeatherRouteGroup.cs new file mode 100644 index 0000000..5594dc8 --- /dev/null +++ b/UlstuGosApi/WeatherRouteGroup.cs @@ -0,0 +1,65 @@ +namespace UlstuGosApi; + +public static class WeatherRouteGroup +{ + public static RouteGroupBuilder AddWeather(this RouteGroupBuilder group) + { + group.MapGet("/", (double lattitude, double longitude) => + { + var location = string.Empty; + + if (Math.Abs(lattitude - 55.75) + Math.Abs(longitude - 37.61) < 1) + { + location = "Москва"; + } + else if (Math.Abs(lattitude - 54.3) + Math.Abs(longitude - 48.4) < 1) + { + location = "Ульяновск"; + } + else + { + return Results.BadRequest("Location not found"); + } + + var result = new List(); + var now = DateTimeOffset.UtcNow; + var currentDate = new DateTimeOffset(DateOnly.FromDateTime(now.Date), TimeOnly.MinValue, now.Offset); + foreach (var idx in Enumerable.Range(0, 10)) + { + result.Add( + new WeatherInfo( + location, + currentDate.AddHours(8 * idx), + Random.Shared.NextDouble() * 10 + 10, + windDirections[Random.Shared.Next(windDirections.Length)], + Random.Shared.NextDouble() * 10, + Random.Shared.NextDouble() * 100 + )); + } + return Results.Ok(result.ToArray()); + }) + .Produces() + .WithSummary("Прогноз погоды"); + + return group; + } + + private static string[] windDirections = ["С", "СВ", "В", "ЮВ", "Ю", "ЮЗ", "З", "СЗ"]; +} + +/// +/// Информация о погоде. +/// +/// Местоположение. +/// Дата и время. +/// Температура. +/// Направление ветра. +/// Скорость ветра. +/// Влажность. +public sealed record WeatherInfo( + string Location, + DateTimeOffset Date, + double Temperature, + string WindDirection, + double WindSpeed, + double Humidity);