|
|
|
@ -3,101 +3,72 @@ using System;
|
|
|
|
|
|
|
|
|
|
namespace API_Rest.Controllers
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class WeatherForecastController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private static List<String> Summaries = new List<String>
|
|
|
|
|
{
|
|
|
|
|
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static List<WeatherForecast> Weathers = new List<WeatherForecast>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
new WeatherForecast
|
|
|
|
|
{
|
|
|
|
|
Id = 1,
|
|
|
|
|
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
|
|
|
|
|
TemperatureC = Random.Shared.Next(-20, 55),
|
|
|
|
|
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
|
|
|
|
|
},
|
|
|
|
|
new WeatherForecast
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Id = 2,
|
|
|
|
|
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
|
|
|
|
|
TemperatureC = Random.Shared.Next(-20, 55),
|
|
|
|
|
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
|
|
|
|
|
},
|
|
|
|
|
new WeatherForecast
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Id = 3,
|
|
|
|
|
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(Random.Shared.Next(0,5))),
|
|
|
|
|
TemperatureC = Random.Shared.Next(-20, 55),
|
|
|
|
|
Summary = Summaries[Random.Shared.Next(Summaries.Count)]
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
public WeatherForecastService _wfs { get; set; }
|
|
|
|
|
|
|
|
|
|
public IEnumerable<WeatherForecast> WeatherForecasts { get; set; }
|
|
|
|
|
public WeatherForecast wf { get; set; }
|
|
|
|
|
private readonly ILogger<WeatherForecastController> _logger;
|
|
|
|
|
|
|
|
|
|
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
|
|
|
|
public WeatherForecastController(ILogger<WeatherForecastController> logger, WeatherForecastService wfs)
|
|
|
|
|
{
|
|
|
|
|
_wfs = wfs;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[HttpGet("GetAll/")]
|
|
|
|
|
|
|
|
|
|
[Route("/api/WeatherForecast/getAll/")]
|
|
|
|
|
public IEnumerable<WeatherForecast> GetAll()
|
|
|
|
|
public async Task<IEnumerable<WeatherForecast>> GetAll()
|
|
|
|
|
{
|
|
|
|
|
return Weathers
|
|
|
|
|
.ToArray();
|
|
|
|
|
WeatherForecasts = _wfs.Get();
|
|
|
|
|
return WeatherForecasts;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/api/WeatherForecast/getOne/{id}")]
|
|
|
|
|
[HttpGet("GetOne/{id}")]
|
|
|
|
|
|
|
|
|
|
public async Task<ActionResult<WeatherForecast>> GetOne(long id)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
foreach (WeatherForecast wf in Weathers)
|
|
|
|
|
wf = _wfs.GetOne(id);
|
|
|
|
|
if (wf != null)
|
|
|
|
|
{
|
|
|
|
|
if (wf.Id == id)
|
|
|
|
|
{
|
|
|
|
|
return Ok(wf);
|
|
|
|
|
}
|
|
|
|
|
return Ok(wf);
|
|
|
|
|
}
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost(Name = "PostWeatherForecast")]
|
|
|
|
|
[HttpPut("Update/{id}")]
|
|
|
|
|
|
|
|
|
|
public async Task<ActionResult<WeatherForecast>> UpdateWeatherForcast(int id, WeatherForecast weatherForcast) {
|
|
|
|
|
_wfs.UpdateWeatherForcast(id, weatherForcast);
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("{WeatherForecast wf}")]
|
|
|
|
|
public async Task<ActionResult<WeatherForecast>> Post(WeatherForecast wf)
|
|
|
|
|
{
|
|
|
|
|
//_context.TodoItems.Add(todoItem);
|
|
|
|
|
//await _context.SaveChangesAsync();
|
|
|
|
|
Weathers.Add(wf);
|
|
|
|
|
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
|
|
|
|
|
_wfs.Post(wf);
|
|
|
|
|
return CreatedAtAction(nameof(Created), new { date = wf.Date }, wf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[Route("/api/WeatherForecast/delete/{id}")]
|
|
|
|
|
public async Task<ActionResult> Delete(long id)
|
|
|
|
|
|
|
|
|
|
[HttpDelete("DeleteOne/{id}")]
|
|
|
|
|
public async Task<ActionResult> DeleteOne(long id)
|
|
|
|
|
{
|
|
|
|
|
foreach (WeatherForecast wf in Weathers)
|
|
|
|
|
if (_wfs.DeleteOne(id))
|
|
|
|
|
{
|
|
|
|
|
if (wf.Id == id)
|
|
|
|
|
{
|
|
|
|
|
Weathers.Remove(wf);
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|