turing-machine/Implementation/Server/Controllers/ExamplesController.cs

44 lines
1.3 KiB
C#

using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using TuringMachine.Core;
namespace TuringMachine.Server.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ExamplesController : ControllerBase
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
private readonly IWebHostEnvironment _env;
public ExamplesController(IWebHostEnvironment env)
{
_env = env;
}
/// <summary>
/// Reads and returns the example list from a JSON file.
/// The file is read on every request so changes are reflected without server restart.
/// </summary>
[HttpGet]
public async Task<ActionResult<List<ExampleConfig>>> GetExamples()
{
var filePath = Path.Combine(_env.ContentRootPath, "Examples", "Examples.json");
if (!System.IO.File.Exists(filePath))
{
return NotFound("Examples.json not found.");
}
var json = await System.IO.File.ReadAllTextAsync(filePath);
var examples = JsonSerializer.Deserialize<List<ExampleConfig>>(json, JsonOptions);
return Ok(examples ?? new List<ExampleConfig>());
}
}
}