-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnosticsController.cs
More file actions
85 lines (76 loc) · 2.38 KB
/
DiagnosticsController.cs
File metadata and controls
85 lines (76 loc) · 2.38 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Microsoft.AspNetCore.Mvc;
using OrderMonitor.Core.Interfaces;
namespace OrderMonitor.Api.Controllers;
/// <summary>
/// Diagnostic endpoints for debugging and schema discovery.
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Produces("application/json")]
public class DiagnosticsController : ControllerBase
{
private readonly IDiagnosticsService _diagnosticsService;
private readonly ILogger<DiagnosticsController> _logger;
public DiagnosticsController(IDiagnosticsService diagnosticsService, ILogger<DiagnosticsController> logger)
{
_diagnosticsService = diagnosticsService;
_logger = logger;
}
/// <summary>
/// Gets tables containing 'Order' in their name.
/// </summary>
[HttpGet("tables")]
public async Task<IActionResult> GetOrderTables()
{
try
{
var tables = await _diagnosticsService.GetTablesAsync("Order");
return Ok(tables);
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
/// <summary>
/// Gets columns for a specific table.
/// </summary>
[HttpGet("columns/{tableName}")]
public async Task<IActionResult> GetTableColumns(string tableName)
{
try
{
var columns = await _diagnosticsService.GetColumnsAsync(tableName);
return Ok(columns);
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
/// <summary>
/// Runs a custom SQL query (SELECT only).
/// </summary>
[HttpPost("query")]
public async Task<IActionResult> RunQuery([FromBody] QueryRequest request)
{
if (string.IsNullOrWhiteSpace(request?.Sql))
return BadRequest(new { error = "SQL query is required" });
// Safety check - only allow SELECT
if (!request.Sql.Trim().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
return BadRequest(new { error = "Only SELECT queries are allowed" });
try
{
var results = await _diagnosticsService.ExecuteQueryAsync(request.Sql);
return Ok(results);
}
catch (Exception ex)
{
return StatusCode(500, new { error = ex.Message });
}
}
}
public class QueryRequest
{
public string Sql { get; set; } = string.Empty;
}