Files
website/Controllers/ApiController.cs
T

46 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Phenix.Services;
namespace Phenix.Controllers;
[Route("/api")]
public class ApiController : Controller
{
private readonly IGameService _games;
public ApiController(IGameService games)
{
_games = games;
}
[HttpGet("results/{year}.json")]
public async Task<IActionResult> ResultsJson(int year)
{
var games = await _games.GetAllGames();
var results = games
.Where(g => g.Year == year)
.Select(g => new Dictionary<string, object?>
{
["id"] = g.Id,
["date"] = g.Date,
["time"] = g.Time,
["home"] = g.Home,
["away"] = g.Away,
["year"] = g.Year,
[g.Home] = g.HomeScore,
[g.Away] = g.AwayScore,
["practice"] = g.Practice,
["rain"] = g.Rain,
});
return Json(results);
}
[HttpGet("available-years.json")]
public async Task<IActionResult> AvailableYearsJson()
{
var games = await _games.GetAllGames();
var years = games.Select(g => g.Year).Distinct().OrderByDescending(y => y);
return Json(years);
}
}