-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeController.cs
More file actions
61 lines (51 loc) · 1.51 KB
/
HomeController.cs
File metadata and controls
61 lines (51 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;
using WeatherTest.WebApp.Models;
using WeatherTest.WebApp.Services;
namespace WeatherTest.WebApp.Controllers
{
public class HomeController : Controller
{
readonly IUnitOfMeasurementsService measurements;
readonly IWeatherChecker weatherChecker;
public HomeController(
IUnitOfMeasurementsService measurements,
IWeatherChecker weatherChecker)
{
this.measurements = measurements
?? throw new ArgumentNullException(nameof(measurements));
this.weatherChecker = weatherChecker
?? throw new ArgumentNullException(nameof(weatherChecker));
}
[HttpGet("")]
public async Task<IActionResult> Index()
{
var vm = new WeatherViewModel
{
NewLocation = "bournemouth",
TemperatureUnit = measurements.TemperatureUnits.ElementAt(0),
WindSpeedUnit = measurements.WindSpeedUnits.ElementAt(0)
};
await CheckWeather(vm);
return View(vm);
}
[HttpPost("")]
public async Task<IActionResult> Index(WeatherViewModel vm)
{
if (ModelState.IsValid)
await CheckWeather(vm);
return View(vm);
}
async Task CheckWeather(WeatherViewModel vm)
{
vm.TemperatureUnit = measurements.TemperatureUnits.First(t => t.Id == vm.TemperatureUnit.Id);
vm.WindSpeedUnit = measurements.WindSpeedUnits.First(t => t.Id == vm.WindSpeedUnit.Id);
vm.Responses = await weatherChecker.CheckAsync(vm.NewLocation);
await vm.RefreshValuesAsync();
}
public IActionResult Error() =>
View();
}
}