using Management.Data; using Microsoft.AspNetCore.Mvc; using System.Text.Json; namespace Management.Controllers; /// /// Public help content endpoint — anonymous, no auth required. /// Returns 200 with default message when key not found, so client /// never needs to handle a 404. /// [ApiController] [Route("api/help")] public class HelpController : ControllerBase { private readonly SqlService _sql; private readonly ILogger _logger; public HelpController(SqlService sql, ILogger logger) { _sql = sql; _logger = logger; } /// /// GET /api/help/{key} /// Returns active help content for the given key, or a friendly /// default if no content has been authored yet. /// [HttpGet("{key}")] public async Task GetHelp(string key, CancellationToken ct) { try { var rqst = JsonSerializer.Serialize(new { helpKey = key }); var resp = await _sql.ExecProcAsync("dbo.spHelp", "get", rqst, ct: ct); if (!string.IsNullOrWhiteSpace(resp)) { using var doc = JsonDocument.Parse(resp); var root = doc.RootElement; if (root.TryGetProperty("ok", out var ok) && ok.GetBoolean()) return Content(resp, "application/json"); } // Key not found — return 200 with friendly default so clients // don't need special 404 handling return Ok(new { ok = true, title = "Help", body = "

No information available for this topic yet.

" }); } catch (Exception ex) { _logger.LogError(ex, "Error retrieving help content for key: {Key}", key); return Ok(new { ok = true, title = "Help", body = "

No information available for this topic yet.

" }); } } }