Files

59 lines
1.7 KiB
C#

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using YongeStreet.Models;
using YongeStreet.Services;
namespace YongeStreet.Controllers;
public record MapLocation(double? lon, double? lat, int? zoom);
public record CityModel(
string City,
IList<CityRoadLengthStats> CityRoadLengthStats,
DateOnly Date,
MapLocation mapLocation);
public record IndexModel(List<CityOverview> Cities);
public class HomeController : Controller
{
private readonly DataService _dataService;
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger, DataService dataService)
{
_logger = logger;
_dataService = dataService;
}
public IActionResult City(string city, string date, double? lon, double? lat, int? zoom)
{
var cityRoadLengthStats = _dataService.GetCityRoadLengthStats(city);
var parsedDate = Util.ParseDateOnly(date) ?? new DateOnly(2025, 1, 1);
var mapLocation = new MapLocation(lon, lat, zoom);
return View(new CityModel(city, cityRoadLengthStats, parsedDate, mapLocation));
}
public IActionResult Index()
{
var cities = _dataService.GetCityOverview();
return View(new IndexModel(cities));
}
public IActionResult CityList_Partial(string? search)
{
var cities = _dataService.GetCityOverview(search ?? "");
return View(new IndexModel(cities));
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}