You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.4 KiB
45 lines
1.4 KiB
7 days ago
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
|
||
|
namespace CoreAgent.API.Controllers
|
||
|
{
|
||
|
[ApiController]
|
||
|
[ApiVersion("1.0")]
|
||
|
[Route("api/v{version:apiVersion}/[controller]")]
|
||
|
public class WeatherForecastController : ControllerBase
|
||
|
{
|
||
|
private readonly ILogger<WeatherForecastController> _logger;
|
||
|
private static readonly string[] Summaries = new[]
|
||
|
{
|
||
|
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||
|
};
|
||
|
|
||
|
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||
|
{
|
||
|
_logger = logger;
|
||
|
}
|
||
|
|
||
|
[HttpGet]
|
||
|
public IActionResult Get()
|
||
|
{
|
||
|
_logger.LogInformation("Weather forecast requested");
|
||
|
|
||
|
var forecast = Enumerable.Range(1, 5).Select(index =>
|
||
|
new WeatherForecast
|
||
|
(
|
||
|
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||
|
Random.Shared.Next(-20, 55),
|
||
|
Summaries[Random.Shared.Next(Summaries.Length)]
|
||
|
))
|
||
|
.ToArray();
|
||
|
|
||
|
_logger.LogDebug("Generated forecast for {Count} days", forecast.Length);
|
||
|
return Ok(forecast);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||
|
{
|
||
|
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||
|
}
|
||
|
}
|