From f2a54d310d73b19d13fc16e203b5e9db295b1fbf Mon Sep 17 00:00:00 2001 From: vladdy Date: Thu, 23 May 2024 22:38:19 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20API=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B3?= =?UTF-8?q?=D0=BD=D0=BE=D0=B7=D0=B0=20=D0=BF=D0=BE=D0=B3=D0=BE=D0=B4=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UlstuGosApi/Program.cs | 4 ++ UlstuGosApi/WeatherRouteGroup.cs | 65 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 UlstuGosApi/WeatherRouteGroup.cs 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);