Добавлен API для прогноза погоды

This commit is contained in:
vladdy 2024-05-23 22:38:19 +04:00
parent d88bcf2360
commit f2a54d310d
2 changed files with 69 additions and 0 deletions

View File

@ -37,4 +37,8 @@ app.MapGroup("/timetable")
.AddTimeTable()
.WithTags("TimeTable");
app.MapGroup("/weather")
.AddWeather()
.WithTags("Weather");
app.Run();

View File

@ -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<WeatherInfo>();
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<WeatherInfo[]>()
.WithSummary("Прогноз погоды");
return group;
}
private static string[] windDirections = ["С", "СВ", "В", "ЮВ", "Ю", "ЮЗ", "З", "СЗ"];
}
/// <summary>
/// Информация о погоде.
/// </summary>
/// <param name="Location">Местоположение.</param>
/// <param name="Date">Дата и время.</param>
/// <param name="Temperature">Температура.</param>
/// <param name="WindDirection">Направление ветра.</param>
/// <param name="WindSpeed">Скорость ветра.</param>
/// <param name="Humidity">Влажность.</param>
public sealed record WeatherInfo(
string Location,
DateTimeOffset Date,
double Temperature,
string WindDirection,
double WindSpeed,
double Humidity);